From 01519df0cf66c743a55933459ac0dc1d6395eae6 Mon Sep 17 00:00:00 2001 From: Isaac Date: Sat, 3 Apr 2021 00:02:00 +0100 Subject: [PATCH] tickets --- src/database/index.js | 6 +++- src/listeners/message.js | 4 +-- src/locales/en-GB.json | 17 +++++++++++ src/modules/commands/command.js | 10 +++++- src/modules/tickets/manager.js | 54 ++++++++++++++++++++++++++++++--- 5 files changed, 81 insertions(+), 10 deletions(-) diff --git a/src/database/index.js b/src/database/index.js index 0528770..4cdcc01 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -167,6 +167,10 @@ module.exports = async (log) => { open: { type: DataTypes.BOOLEAN, defaultValue: true + }, + closed_by: { + type: DataTypes.CHAR(18), + allowNull: true, } }, { tableName: DB_TABLE_PREFIX + 'tickets' @@ -226,7 +230,7 @@ module.exports = async (log) => { username: DataTypes.STRING, discriminator: DataTypes.STRING, display_name: DataTypes.STRING, - colour: DataTypes.INTEGER, + colour: DataTypes.CHAR(6), bot: DataTypes.BOOLEAN }, { tableName: DB_TABLE_PREFIX + 'user_entities' diff --git a/src/listeners/message.js b/src/listeners/message.js index 9773eb5..032a003 100644 --- a/src/listeners/message.js +++ b/src/listeners/message.js @@ -4,10 +4,8 @@ module.exports = { let settings = await message.guild?.settings; - if (settings?.log_messages) { - if (message.system) return; + if (settings?.log_messages && !message.system) client.tickets.archives.addMessage(message); - } client.commands.handle(message); } diff --git a/src/locales/en-GB.json b/src/locales/en-GB.json index 5f25ab1..0db1215 100644 --- a/src/locales/en-GB.json +++ b/src/locales/en-GB.json @@ -12,6 +12,19 @@ "title": "`%s` command usage" }, "commands": { + "close": { + "aliases": { + "delete": "delete" + }, + "description": "Close a ticket channel", + "name": "close", + "response": { + "closed": { + "title": "✅ Ticket closed", + "description": "This ticket has been closed by %s.\nThe channel will be deleted in 5 seconds." + } + } + }, "new": { "aliases": { "create": "create", @@ -31,6 +44,10 @@ "description": "Create a new ticket", "name": "new", "response": { + "created": { + "title": "✅ Ticket created", + "description": "" + }, "no_categories": { "title": "❌ Can't create ticket", "description": "A server administrator must create at least one ticket category before a new ticket can be opened." diff --git a/src/modules/commands/command.js b/src/modules/commands/command.js index a5001df..616926d 100644 --- a/src/modules/commands/command.js +++ b/src/modules/commands/command.js @@ -2,6 +2,14 @@ * A command */ module.exports = class Command { + /** + * + * @typedef CommandArgument + * @property {string} name - The argument's name + * @property {string} description - The argument's description + * @property {string} example - An example value + * @property {boolean?} required - Is this arg required? Defaults to `false` + */ /** * Create a new Command * @param {Client} client - The Discord Client @@ -69,7 +77,7 @@ module.exports = class Command { * The command options * @type {CommandArgument[]} */ - this.args = data.args; + this.args = data.args || []; for (let arg in this.args) { if (!this.args[arg].example) diff --git a/src/modules/tickets/manager.js b/src/modules/tickets/manager.js index f14807b..ecaa557 100644 --- a/src/modules/tickets/manager.js +++ b/src/modules/tickets/manager.js @@ -1,5 +1,7 @@ const EventEmitter = require('events'); const TicketArchives = require('./archives'); +const { MessageEmbed } = require('discord.js'); +const { int2hex } = require('../../utils'); /** Manages tickets */ module.exports = class TicketManager extends EventEmitter { @@ -47,13 +49,13 @@ module.exports = class TicketManager extends EventEmitter { let member = await guild.members.fetch(creator_id); let name = cat_row.name_format .replace(/{+\s?(user)?name\s?}+/gi, member.displayName) - .replace(/{+\s?number\s?}+/gi, number); + .replace(/{+\s?num(ber)?\s?}+/gi, number); let t_channel = await guild.channels.create(name, { type: 'text', topic: `${member}${topic.length > 0 ? ` | ${topic}` : ''}`, parent: category_id, - reason: `${member.tag} requested a new ticket channel` + reason: `${member.user.tag} requested a new ticket channel` }); await t_channel.updateOverwrite(creator_id, { @@ -95,11 +97,53 @@ module.exports = class TicketManager extends EventEmitter { if (!t_row) throw new Error(`Could not find a ticket with ID ${ticket_id}`); ticket_id = t_row.id; - this.emit('beforeClose', ticket_id, closer_id); + this.emit('beforeClose', ticket_id); - // ... + let u_model_data = { + user: closer_id, + ticket: ticket_id + }; + let [u_row] = await this.client.db.models.UserEntity.findOrCreate({ + where: u_model_data, + defaults: u_model_data + }); - this.emit('close', ticket_id, closer_id); + let guild = this.client.guilds.cache.get(t_row.guild); + let member = await guild.members.fetch(closer_id); + + await u_row.update({ + avatar: member.user.displayAvatarURL(), + username: member.user.username, + discriminator: member.user.discriminator, + display_name: member.displayName, + colour: member.displayColor === 0 ? null : int2hex(member.displayColor), + bot: member.user.bot + }); + + await t_row.update({ + open: false, + closed_by: closer_id + }); + + let channel = await this.client.channels.fetch(t_row.channel); + + if (channel) { + let settings = await guild.settings; + const i18n = this.client.i18n.get(settings.locale); + + await channel.send( + new MessageEmbed() + .setColor(settings.success_colour) + .setTitle(i18n('commands.close.response.closed.title')) + .setDescription(i18n('commands.close.response.closed.description', member.user.toString())) + ); + + setTimeout(async () => { + await channel.delete(`Ticket channel closed by ${member.user.tag}`); + }, 5000); + } + + this.emit('close', ticket_id); } /**