From 89f754b968551819cd1f69d6cb672967fe319320 Mon Sep 17 00:00:00 2001 From: Isaac Date: Wed, 12 May 2021 17:32:26 +0100 Subject: [PATCH] Make reaction panels work --- src/commands/panel.js | 11 ++-- src/listeners/messageReactionAdd.js | 97 ++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/src/commands/panel.js b/src/commands/panel.js index bc6e635..984cb24 100644 --- a/src/commands/panel.js +++ b/src/commands/panel.js @@ -94,7 +94,7 @@ module.exports = class PanelCommand extends Command { let panel_channel, panel_message; - let emoji_map = args[arg_categories]; + let categories_map = args[arg_categories][0]; let embed = new MessageEmbed() .setColor(settings.colour) @@ -157,17 +157,18 @@ module.exports = class PanelCommand extends Command { if (args[arg_emoji].length === 1) { // single category + categories_map = {}; + categories_map[args[arg_emoji][0]] = args[arg_categories][0]; embed.setDescription(args[arg_description]); panel_message = await panel_channel.send(embed); await panel_message.react(args[arg_emoji][0]); } else { // multi category let description = ''; - - emoji_map = {}; + categories_map = {}; for (let i in args[arg_emoji]) { - emoji_map[args[arg_emoji][i]] = args[arg_categories][i]; + categories_map[args[arg_emoji][i]] = args[arg_categories][i]; let cat_row = await this.client.db.models.Category.findOne({ where: { id: args[arg_categories][i], @@ -194,7 +195,7 @@ module.exports = class PanelCommand extends Command { message.channel.send(`✅ ${panel_channel}`); await this.client.db.models.Panel.create({ - categories: emoji_map, + categories: categories_map, channel: panel_channel.id, guild: message.guild.id, message: panel_message.id, diff --git a/src/listeners/messageReactionAdd.js b/src/listeners/messageReactionAdd.js index d41fee2..119806e 100644 --- a/src/listeners/messageReactionAdd.js +++ b/src/listeners/messageReactionAdd.js @@ -1,11 +1,13 @@ const { MessageEmbed } = require('discord.js'); +const { footer } = require('../utils/discord'); + module.exports = { event: 'messageReactionAdd', execute: async (client, r, u) => { - if (r.partial) { - r = await r.fetch(); - } + if (r.partial) r = await r.fetch(); + + if (u.partial) u = await u.fetch(); if (u.id === client.user.id) return; @@ -72,6 +74,95 @@ module.exports = { } else { await r.users.remove(u.id); } + } else { + let p_row = await client.db.models.Panel.findOne({ + where: { + message: r.message.id + } + }); + + if (p_row && typeof p_row.categories !== 'string') { + // panels + await r.users.remove(u.id); + + let category_id = p_row.categories[r.emoji.name]; + if (!category_id) return; + + let cat_row = await client.db.models.Category.findOne({ + where: { + id: category_id + } + }); + + let tickets = await client.db.models.Ticket.findAndCountAll({ + where: { + category: cat_row.id, + creator: u.id, + open: true + } + }); + + let response; + + if (tickets.count >= cat_row.max_per_member) { + if (cat_row.max_per_member === 1) { + const embed = new MessageEmbed() + .setColor(settings.error_colour) + .setAuthor(u.username, u.displayAvatarURL()) + .setTitle(i18n('commands.new.response.has_a_ticket.title')) + .setDescription(i18n('commands.new.response.has_a_ticket.description', tickets.rows[0].id)) + .setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), guild.iconURL()); + try { + response = await u.send(embed); + } catch { + response = await channel.send(embed); + } + } else { + let list = tickets.rows.map(row => { + if (row.topic) { + let description = row.topic.substring(0, 30); + let ellipses = row.topic.length > 30 ? '...' : ''; + return `<#${row.id}>: \`${description}${ellipses}\``; + } else { + return `<#${row.id}>`; + } + }); + const embed = new MessageEmbed() + .setColor(settings.error_colour) + .setAuthor(u.username, u.displayAvatarURL()) + .setTitle(i18n('commands.new.response.max_tickets.title', tickets.count)) + .setDescription(i18n('commands.new.response.max_tickets.description', settings.command_prefix, list.join('\n'))) + .setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), u.iconURL()); + try { + response = await u.send(embed); + } catch { + response = await channel.send(embed); + } + } + } else { + try { + await client.tickets.create(guild.id, u.id, cat_row.id); + } catch (error) { + const embed = new MessageEmbed() + .setColor(settings.error_colour) + .setAuthor(u.username, u.displayAvatarURL()) + .setTitle(i18n('commands.new.response.error.title')) + .setDescription(error.message) + .setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), guild.iconURL()); + try { + response = await u.send(embed); + } catch { + response = await channel.send(embed); + } + } + } + + if (response) { + setTimeout(async() => { + await response.delete(); + }, 15000); + } + } } }