From 9ebdf557df89258547716f4c6295ac0432212585 Mon Sep 17 00:00:00 2001 From: Isaac Date: Mon, 10 May 2021 16:28:47 +0100 Subject: [PATCH] Add ticket claiming --- src/listeners/messageReactionAdd.js | 54 +++++++++++++++++++++++------ src/locales/en-GB.json | 4 +++ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/listeners/messageReactionAdd.js b/src/listeners/messageReactionAdd.js index 9ef50ee..d41fee2 100644 --- a/src/listeners/messageReactionAdd.js +++ b/src/listeners/messageReactionAdd.js @@ -1,3 +1,5 @@ +const { MessageEmbed } = require('discord.js'); + module.exports = { event: 'messageReactionAdd', execute: async (client, r, u) => { @@ -12,33 +14,63 @@ module.exports = { let settings = await guild.settings; if (!settings) settings = await guild.createSettings(); + const i18n = client.i18n.getLocale(settings.locale); - let member = await guild.members.fetch(u.id); + const channel = r.message.channel; + const member = await guild.members.fetch(u.id); if (settings.blacklist.includes(u.id)) { return client.log.info(`Ignoring blacklisted member ${u.tag}`); } else { - let member = await guild.members.fetch(u.id); settings.blacklist.forEach(element => { if (guild.roles.cache.has(element) && member.roles.cache.has(element)) { return client.log.info(`Ignoring member ${u.tag} with blacklisted role`); } }); } + let t_row = await client.db.models.Ticket.findOne({ where: { - id: r.message.channel.id + id: channel.id } }); - client.log.info('got t row') - if (t_row) { - if ( - t_row.opening_message === r.message.id - && r.emoji.name === '🙌' - && await member.isStaff() - ) { + + if (t_row && t_row.opening_message === r.message.id) { + if (r.emoji.name === '🙌' && await member.isStaff()) { // ticket claiming - client.log.info('claimed') + + await t_row.update({ + claimed_by: member.user.id + }); + + await channel.updateOverwrite(member.user.id, { + VIEW_CHANNEL: true, + }, `Ticket claimed by ${member.user.tag}`); + + let cat_row = await client.db.models.Category.findOne({ + where: { + id: t_row.category + } + }); + + for (let role of cat_row.roles) { + await channel.updateOverwrite(role, { + VIEW_CHANNEL: false, + }, `Ticket claimed by ${member.user.tag}`); + } + + client.log.info(`${member.user.tag} has claimed "${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.claimed.title')) + .setDescription(i18n('commands.new.response.claimed.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 0055ad5..118fd9d 100644 --- a/src/locales/en-GB.json +++ b/src/locales/en-GB.json @@ -113,6 +113,10 @@ }, "questions": "Please answer the following questions:\n\n%s", "response": { + "claimed": { + "title":"✅ Ticket claimed", + "description": "%s has claimed this ticket." + }, "created": { "title": "✅ Ticket created", "description": "Your ticket has been created: %s."