From 6506a4e876c5458df29180173c08300fc4e15975 Mon Sep 17 00:00:00 2001 From: Isaac Date: Wed, 12 May 2021 17:43:52 +0100 Subject: [PATCH] Make reaction-less panels work --- src/listeners/message.js | 83 ++++++++++++++++++++++++++++- src/listeners/messageReactionAdd.js | 1 - 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/listeners/message.js b/src/listeners/message.js index b3653fa..f8e1c0c 100644 --- a/src/listeners/message.js +++ b/src/listeners/message.js @@ -1,3 +1,6 @@ +const { MessageEmbed } = require('discord.js'); +const { footer } = require('../utils/discord'); + module.exports = { event: 'message', execute: async (client, message) => { @@ -5,6 +8,7 @@ module.exports = { let settings = await message.guild.settings; if (!settings) settings = await message.guild.createSettings(); + const i18n = client.i18n.getLocale(settings.locale); let t_row = await client.db.models.Ticket.findOne({ where: { @@ -30,8 +34,85 @@ module.exports = { } }); - if (p_row) { + if (p_row && typeof p_row.categories === 'string') { // handle reaction-less panel + + await message.delete(); + + let cat_row = await client.db.models.Category.findOne({ + where: { + id: p_row.categories + } + }); + + let tickets = await client.db.models.Ticket.findAndCountAll({ + where: { + category: cat_row.id, + creator: message.author.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(message.author.username, message.author.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)), message.guild.iconURL()); + try { + response = await message.author.send(embed); + } catch { + response = await message.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(message.author.username, message.author.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)), message.author.iconURL()); + try { + response = await message.author.send(embed); + } catch { + response = await message.channel.send(embed); + } + } + } else { + try { + await client.tickets.create(message.guild.id, message.author.id, cat_row.id, message.cleanContent); + } catch (error) { + const embed = new MessageEmbed() + .setColor(settings.error_colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle(i18n('commands.new.response.error.title')) + .setDescription(error.message) + .setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), message.guild.iconURL()); + try { + response = await message.author.send(embed); + } catch { + response = await message.channel.send(embed); + } + } + } + + if (response) { + setTimeout(async () => { + await response.delete(); + }, 15000); + } } } diff --git a/src/listeners/messageReactionAdd.js b/src/listeners/messageReactionAdd.js index 119806e..bc781c2 100644 --- a/src/listeners/messageReactionAdd.js +++ b/src/listeners/messageReactionAdd.js @@ -1,5 +1,4 @@ const { MessageEmbed } = require('discord.js'); - const { footer } = require('../utils/discord'); module.exports = {