From 2910a2a201edc5d0dc63022eaeb4071ae0453d02 Mon Sep 17 00:00:00 2001 From: Isaac Date: Sun, 21 Feb 2021 15:58:06 +0000 Subject: [PATCH] progress --- src/commands/new.js | 11 +++-- src/database/index.js | 81 ++++++++++++++++++++++++++++++++- src/listeners/interaction.js | 2 +- src/listeners/message.js | 19 +++++++- src/modules/commands/command.js | 34 ++++++++------ src/modules/commands/manager.js | 13 +++--- src/modules/tickets/index.js | 3 +- src/modules/tickets/manager.js | 26 +++++++++++ src/modules/tickets/ticket.js | 0 src/utils/discord.js | 11 ++++- 10 files changed, 171 insertions(+), 29 deletions(-) create mode 100644 src/modules/tickets/manager.js create mode 100644 src/modules/tickets/ticket.js diff --git a/src/commands/new.js b/src/commands/new.js index 61df97e..4c163e9 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -27,13 +27,16 @@ module.exports = class NewCommand extends Command { }); } - async execute({ guild, member, channel, args}, interaction) { + async execute({ guild, member, channel, args }, interaction) { let settings = await guild.settings; const i18n = this.client.i18n.get(settings.locale); - return new MessageEmbed() - .setColor(settings.colour) - .setTitle(i18n('bot.version', require('../../package.json').version)); + channel.send( + new MessageEmbed() + .setColor(settings.colour) + .setTitle(i18n('bot.version', require('../../package.json').version)) + .setDescription(args.topic) + ); } }; \ No newline at end of file diff --git a/src/database/index.js b/src/database/index.js index 79fd575..d88181d 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -73,7 +73,11 @@ module.exports = async (log) => { locale: { type: DataTypes.STRING, defaultValue: config.defaults.locale - } + }, + log_messages: { + type: DataTypes.BOOLEAN, + defaultValue: config.defaults.log_messages + }, }, { tableName: DB_TABLE_PREFIX + 'guilds' }); @@ -116,11 +120,84 @@ module.exports = async (log) => { model: Ticket, key: 'id' }, - } + }, + author: { + type: DataTypes.CHAR(18), + allowNull: false, + }, + updates: { + type: DataTypes.JSON + }, }, { tableName: DB_TABLE_PREFIX + 'messages' }); + // eslint-disable-next-line no-unused-vars + const Channel = sequelize.define('Channel', { + id: { + type: DataTypes.CHAR(18), + primaryKey: true, + allowNull: false, + }, + name: { + type: DataTypes.STRING, + allowNull: false, + }, + }, { + tableName: DB_TABLE_PREFIX + 'channel_entities' + }); + + // eslint-disable-next-line no-unused-vars + const Role = sequelize.define('Role', { + id: { + type: DataTypes.CHAR(18), + primaryKey: true, + allowNull: false, + }, + name: { + type: DataTypes.STRING, + allowNull: false, + }, + colour: { + type: DataTypes.INTEGER, + defaultValue: 7506394 + }, + }, { + tableName: DB_TABLE_PREFIX + 'role_entities' + }); + + // eslint-disable-next-line no-unused-vars + const Member = sequelize.define('Member', { + id: { + type: DataTypes.CHAR(18), + primaryKey: true, + allowNull: false, + }, + username: { + type: DataTypes.STRING, + allowNull: false, + }, + discriminator: { + type: DataTypes.STRING, + allowNull: false, + }, + nickname: { + type: DataTypes.STRING, + allowNull: false, + }, + avatar: { + type: DataTypes.STRING, + }, + colour: { + type: DataTypes.INTEGER, + }, + bot: { + type: DataTypes.BOOLEAN, + }, + }, { + tableName: DB_TABLE_PREFIX + 'member_entities' + }); + sequelize.sync(); return sequelize; diff --git a/src/listeners/interaction.js b/src/listeners/interaction.js index 97bff22..a327fe1 100644 --- a/src/listeners/interaction.js +++ b/src/listeners/interaction.js @@ -13,7 +13,7 @@ module.exports = { }); break; case 2: - client.commands.execute(interaction.data.name, interaction); + client.commands.handle(interaction); break; } diff --git a/src/listeners/message.js b/src/listeners/message.js index 14d9aef..4a19548 100644 --- a/src/listeners/message.js +++ b/src/listeners/message.js @@ -1,5 +1,22 @@ module.exports = { event: 'message', - execute: (client, message) => { + execute: async (client, message) => { + + let settings = await message.guild.settings; + + if (settings.log_messages) { + if (message.type !== 'DEFAULT') return; + + let ticket = await client.tickets.get(message.channel.id); + + if (ticket) { + client.db.models.Message.create({ + id: message.id, + ticket: ticket.id, + author: message.author.id + }); + } + } + } }; \ No newline at end of file diff --git a/src/modules/commands/command.js b/src/modules/commands/command.js index 9ebb45e..94416c1 100644 --- a/src/modules/commands/command.js +++ b/src/modules/commands/command.js @@ -35,9 +35,10 @@ module.exports = class Command { * @param {Object} data - Command data * @param {string} data.name - The name of the command (3-32) * @param {string} data.description - The description of the command (1-100) - * @param {boolean} staff_only - Only allow staff to use this command? - * @param {string[]} permissions - Array of permissions needed for a user to use this command - * @param {CommandOption[]} data.options - The command options, max of 10 + * @param {boolean} [data.staff_only] - Only allow staff to use this command? + * @param {string[]} [data.permissions] - Array of permissions needed for a user to use this command + * @param {boolean} [data.global] - Create a global command? + * @param {CommandOption[]} [data.options] - The command options (parameters), max of 10 */ constructor(client, data) { @@ -75,6 +76,13 @@ module.exports = class Command { */ this.permissions = data.permissions; + /** + * Is this a global command? + * @type {boolean} + * @default true + */ + this.global = data.global === false ? false : true; + /** * The command options * @type {CommandOption[]} @@ -103,7 +111,8 @@ module.exports = class Command { return this.client.log.error(e); } - this.client.api.applications(this.client.user.id).commands.post({ data }); // post command to Discord + if (this.global) + this.client.api.applications(this.client.user.id).commands.post({ data }); // post command to Discord let internal = this.internal ? 'internal ' : ''; this.client.log.commands(`Loaded ${internal}"${this.name}" command`); @@ -124,9 +133,9 @@ module.exports = class Command { * @property {string} interaction.id - ID of the interaction * @property {number} interaction.type - Type of interaction * @property {ApplicationCommandInteractionData} interaction.data - Interaction data - * @property {Guild} interaction.guild- The guild object - * @property {Channel} interaction.channel- The channel object - * @property {GuildMember} interaction.member - The member object + * @property {Object} interaction.guild- The guild object + * @property {Object} interaction.channel- The channel object + * @property {Object} interaction.member - The member object * @property {string} interaction.token - The token used to respond to the interaction */ @@ -138,7 +147,6 @@ module.exports = class Command { * @param {Channel} data.channel- The channel object * @param {Guild} data.guild- The guild object * @param {GuildMember} data.member - The member object - * @param {string} data.token - The token used to respond to the interaction * @param {Interaction} interaction - Interaction object */ async execute(data, interaction) { } @@ -146,15 +154,15 @@ module.exports = class Command { /** * Defer the response to respond later * @param {Interaction} interaction - Interaction object - * @param {boolean} secret - Ephemeral message? **NOTE: EMBEDS AND ATTACHMENTS DO NOT RENDER IF TRUE** + * @param {boolean} secret - Ephemeral? */ - async deferResponse(interaction, secret) { + async acknowledge(interaction, secret) { this.client.api.interactions(interaction.id, interaction.token).callback.post({ data: { type: 5, - data: { - flags: flags(secret) - }, + // data: { + // flags: flags(secret) + // }, } }); } diff --git a/src/modules/commands/manager.js b/src/modules/commands/manager.js index a133563..bd54e73 100644 --- a/src/modules/commands/manager.js +++ b/src/modules/commands/manager.js @@ -139,10 +139,11 @@ module.exports = class CommandManager { /** * Execute a command - * @param {string} cmd_name - Name of the command * @param {Interaction} interaction - Command interaction */ - async execute(cmd_name, interaction) { + async handle(interaction) { + const cmd_name = interaction.data.name; + if (!this.commands.has(cmd_name)) throw new Error(`Received "${cmd_name}" command invocation, but the command manager does not have a "${cmd_name}" command`); @@ -175,11 +176,11 @@ module.exports = class CommandManager { } try { - await cmd.deferResponse(interaction, true); + await cmd.acknowledge(interaction, true); // respond to discord this.client.log.commands(`Executing "${cmd_name}" command (invoked by ${data.member.user.tag})`); - let res = await cmd.execute(data, interaction); // run the command - if (typeof res === 'object' || typeof res === 'string') - cmd.sendResponse(interaction, res, res.secret); + /* let res = */await cmd.execute(data, interaction); // run the command + // if (typeof res === 'object' || typeof res === 'string') + // cmd.sendResponse(interaction, res, res.secret); } catch (e) { this.client.log.warn(`[COMMANDS] An error occurred whilst executed the ${cmd} command`); this.client.log.error(e); diff --git a/src/modules/tickets/index.js b/src/modules/tickets/index.js index 98f77e0..2a2ca20 100644 --- a/src/modules/tickets/index.js +++ b/src/modules/tickets/index.js @@ -1,3 +1,4 @@ module.exports = { - + TicketManager: require('./manager'), + Ticket: require('./ticket'), }; \ No newline at end of file diff --git a/src/modules/tickets/manager.js b/src/modules/tickets/manager.js new file mode 100644 index 0000000..17bf2a0 --- /dev/null +++ b/src/modules/tickets/manager.js @@ -0,0 +1,26 @@ +const EventEmitter = require('events'); +// eslint-disable-next-line no-unused-vars +const { Client } = require('discord.js'); + +/** Manages tickets */ +module.exports = class extends EventEmitter { + /** + * Create a TicketManager instance + * @param {Client} client + */ + constructor(client) { + super(); + + /** The Discord Client */ + this.client = client; + + this.setMaxListeners(this.client.config.max_listeners); + } + + /** + * Create a new ticket + */ + async create() { + + } +}; \ No newline at end of file diff --git a/src/modules/tickets/ticket.js b/src/modules/tickets/ticket.js new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/discord.js b/src/utils/discord.js index d6f206b..6615540 100644 --- a/src/utils/discord.js +++ b/src/utils/discord.js @@ -7,7 +7,7 @@ let current_presence = -1; module.exports = { /** * Resolves data and files so embeds can be sent as a response to a slash command - * @param {Discord.Client} channel_id - Text channel ID + * @param {Discord.Client} client - The Discord Client * @param {string} channel_id - Text channel ID * @param {*} content - Message content * @returns {Object} @@ -25,6 +25,15 @@ module.exports = { */ flags: (secret) => secret ? 1 << 64 : undefined, + /** + * Set message entities + * @param {Discord.Message} message - The message to set entities for + */ + + messageEntities(message) { + const { client } = message; + }, + /** * Select a presence from the config * @returns {Discord.PresenceData}