From 7cf039b8eb02cb56397861b8c122335dfd23eb5c Mon Sep 17 00:00:00 2001 From: Isaac Date: Thu, 13 May 2021 16:01:09 +0100 Subject: [PATCH] Ticket releasing --- src/commands/close.js | 3 + src/commands/transfer.js | 30 --------- src/index.js | 2 +- src/listeners/messageReactionAdd.js | 17 ++++- src/listeners/messageReactionRemove.js | 88 ++++++++++++++++++++++++++ src/locales/en-GB.json | 17 ++--- 6 files changed, 111 insertions(+), 46 deletions(-) delete mode 100644 src/commands/transfer.js create mode 100644 src/listeners/messageReactionRemove.js diff --git a/src/commands/close.js b/src/commands/close.js index e21f252..2dbe466 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -27,5 +27,8 @@ module.exports = class CloseCommand extends Command { async execute(message, args) { let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); + + this.client.log.info(args) + message.channel.send(args[0]) } }; \ No newline at end of file diff --git a/src/commands/transfer.js b/src/commands/transfer.js deleted file mode 100644 index 82206fb..0000000 --- a/src/commands/transfer.js +++ /dev/null @@ -1,30 +0,0 @@ -const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); -const { footer } = require('../utils/discord'); - -module.exports = class TransferCommand extends Command { - constructor(client) { - const i18n = client.i18n.getLocale(client.config.locale); - super(client, { - internal: true, - name: i18n('commands.transfer.name'), - description: i18n('commands.transfer.description'), - aliases: [], - process_args: false, - args: [ - { - name: i18n('commands.transfer.args.member.name'), - description: i18n('commands.transfer.args.member.description'), - example: i18n('commands.transfer.args.member.example'), - required: false, - } - ], - staff_only: true - }); - } - - async execute(message, args) { - let settings = await message.guild.settings; - const i18n = this.client.i18n.getLocale(settings.locale); - } -}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 1109e37..2486a5c 100644 --- a/src/index.js +++ b/src/index.js @@ -111,8 +111,8 @@ class Bot extends Client { constructor() { super({ partials: [ - 'MESSAGE', 'CHANNEL', + 'MESSAGE', 'REACTION' ], presence: selectPresence(), diff --git a/src/listeners/messageReactionAdd.js b/src/listeners/messageReactionAdd.js index 098cdbf..1693728 100644 --- a/src/listeners/messageReactionAdd.js +++ b/src/listeners/messageReactionAdd.js @@ -11,9 +11,22 @@ module.exports = class MessageReactionAddEventListener extends EventListener { } async execute(r, u) { - if (r.partial) r = await r.fetch(); - if (u.partial) u = await u.fetch(); + if (r.partial) { + try { + await r.fetch(); + } catch (err) { + return this.client.log.error(err); + } + } + + if (u.partial) { + try { + await u.fetch(); + } catch (err) { + return this.client.log.error(err); + } + } if (u.id === this.client.user.id) return; diff --git a/src/listeners/messageReactionRemove.js b/src/listeners/messageReactionRemove.js new file mode 100644 index 0000000..19d4d95 --- /dev/null +++ b/src/listeners/messageReactionRemove.js @@ -0,0 +1,88 @@ +const EventListener = require('../modules/listeners/listener'); + +const { MessageEmbed } = require('discord.js'); +const { footer } = require('../utils/discord'); + +module.exports = class MessageReactionRemoveEventListener extends EventListener { + constructor(client) { + super(client, { + event: 'messageReactionRemove' + }); + } + + async execute(r, u) { + // release (unclaim) ticket + if (r.partial) { + try { + await r.fetch(); + } catch (err) { + return this.client.log.error(err); + } + } + + if (u.partial) { + try { + await u.fetch(); + } catch (err) { + return this.client.log.error(err); + } + } + + if (u.id === this.client.user.id) return; + + const guild = r.message.guild; + if (!guild) return; + + let settings = await guild.settings; + if (!settings) settings = await guild.createSettings(); + const i18n = this.client.i18n.getLocale(settings.locale); + + const channel = r.message.channel; + const member = await guild.members.fetch(u.id); + + let t_row = await this.client.db.models.Ticket.findOne({ + where: { + id: channel.id + } + }); + + if (t_row && t_row.opening_message === r.message.id) { + if (r.emoji.name === '🙌' && await member.isStaff()) { + // ticket claiming + + await t_row.update({ + claimed_by: null + }); + + await channel.permissionOverwrites + .get(member.user.id) + .delete(`Ticket released by ${member.user.tag}`); + + let cat_row = await this.client.db.models.Category.findOne({ + where: { + id: t_row.category + } + }); + + for (let role of cat_row.roles) { + await channel.updateOverwrite(role, { + VIEW_CHANNEL: true, + }, `Ticket released by ${member.user.tag}`); + } + + this.client.log.info(`${member.user.tag} has released "${channel.name}" in "${guild.name}"`); + + await channel.send( + new MessageEmbed() + .setColor(settings.colour) + .setAuthor(member.user.username, member.user.displayAvatarURL()) + .setTitle(i18n('commands.new.response.released.title')) + .setDescription(i18n('commands.new.response.released.description', member.toString())) + .setFooter(settings.footer, guild.iconURL()) + ); + } else { + await r.users.remove(u.id); + } + } + } +}; diff --git a/src/locales/en-GB.json b/src/locales/en-GB.json index a574326..5940499 100644 --- a/src/locales/en-GB.json +++ b/src/locales/en-GB.json @@ -140,6 +140,10 @@ "description": "A server administrator must create at least one ticket category before a new ticket can be opened.", "title": "❌ Can't create ticket" }, + "released": { + "description": "%s has released this ticket.", + "title":"✅ Ticket released" + }, "select_category": { "description": "Select the category most relevant to your ticket's topic:\n\n%s", "title": "🔤 Please select the ticket category" @@ -204,19 +208,6 @@ "response": { "updated": "✅ Settings have been updated." } - }, - "transfer": { - "aliases": {}, - "args": { - "member": { - "description": "A member mention or the ID of the member to transfer the ticket to", - "example": "@StaffMember", - "name": "member" - } - }, - "description": "Transfer a ticket to another staff member", - "name": "transfer", - "response": {} } }, "command_execution_error": {