From b186ea260c289036b12438955d8fb518555d8a82 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Fri, 19 Jan 2024 12:25:01 +1100 Subject: [PATCH] Revamp interactionCreate file --- src/events/interactionCreate.ts | 91 +++++++++++++++++++-------------- src/models/tagSystem.ts | 3 +- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index c2ed92f..80bc9c1 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -2,49 +2,62 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import Logger from '../helpers/Logger.js'; import MessageTool from '../helpers/MessageTool.js'; +const logPrefix = 'Interaction'; export default class InteractionCreate { static async run(client:TClient, interaction:Discord.BaseInteraction){ if (!interaction.inGuild() || !interaction.inCachedGuild()) return; - const logPrefix = 'Interaction'; - - if (interaction.isChatInputCommand()){ - const commandFile = client.commands.get(interaction.commandName); - Logger.console('log', logPrefix, `${interaction.user.username} used /${interaction.commandName} ${interaction.options.getSubcommandGroup(false) ?? ''} ${interaction.options.getSubcommand(false) ?? ''} in #${interaction.channel.name}`.replace(/\s\s+/g, ' ').trim()); - if (!client.config.botSwitches.commands && !client.config.whitelist.includes(interaction.user.id)) return interaction.reply({content: `I am currently operating in development mode.\nPlease notify <@${client.config.whitelist[0]}> if this is a mistake.`, ephemeral: true}); - if (commandFile){ - try{ - commandFile.command.run(client, interaction); - commandFile.command.autocomplete ? commandFile.command.autocomplete(interaction) : undefined; - commandFile.uses ? commandFile.uses++ : commandFile.uses = 1; - } catch (error){ - console.log(`An error occurred while running command "${interaction.commandName} ${interaction.options.getSubcommandGroup(false) ?? ''} ${interaction.options.getSubcommand(false) ?? ''}"`, error, error.stack); - return interaction.reply('An error occurred while running that command.'); - } - } - } else if (interaction.isAutocomplete()){ - try { - await client.commands.get(interaction.commandName).command.autocomplete(client, interaction); - } catch (error){ - return console.log('An error occurred while running autocomplete:\n', error) - } - } else if (interaction.isButton()){ - if (interaction.customId.startsWith('reaction-') && client.config.botSwitches.buttonRoles){ - const RoleID = interaction.customId.replace('reaction-',''); - - const MFFarm1 = '1149139369433776269'; - const MFFarm2 = '1149139583729160325'; - - if (interaction.member.roles.cache.has(RoleID)) interaction.member.roles.remove(RoleID, 'Button Role').then(()=>interaction.reply({content: `You have been removed from <@&${RoleID}>`, ephemeral: true})); - else interaction.member.roles.add(RoleID, 'Button Role').then(()=>{ - this.roleConflictHandler(interaction, MFFarm1, MFFarm2, RoleID); - interaction.reply({content: `You have been added to <@&${RoleID}>`, ephemeral: true, fetchReply: true}) - }); - } else if (interaction.customId.includes('deleteEvalEmbed')) { - if (!client.config.whitelist.includes(interaction.user.id)) return interaction.reply({content: 'You are not whitelisted, therefore you cannot delete this message.', ephemeral: true}); - interaction.message.delete(); - Logger.console('log', logPrefix, `Eval embed has been deleted in #${interaction.message.channel.name} by ${interaction.member.displayName}`); - } else Logger.console('log', logPrefix, `Button has been pressed at ${interaction.message.url}`); + const handlers = { + isChatInputCommand: this.handleChatInput, + isAutocomplete: this.handleAutocomplete, + isButton: this.handleButton } + + for (const [intType, handler] of Object.entries(handlers)) { + if (interaction[intType]()) { + handler.call(this, client, interaction); + break; + } + } + } + static handleChatInput(client:TClient, interaction:Discord.ChatInputCommandInteraction) { + const commandFile = client.commands.get(interaction.commandName); + Logger.console('log', logPrefix, `${interaction.user.username} used /${interaction.commandName} ${interaction.options.getSubcommandGroup(false) ?? ''} ${interaction.options.getSubcommand(false) ?? ''} in #${interaction.channel.name}`.replace(/\s\s+/g, ' ').trim()); + if (!client.config.botSwitches.commands && !client.config.whitelist.includes(interaction.user.id)) return interaction.reply({content: `I am currently operating in development mode.\nPlease notify <@${client.config.whitelist[0]}> if this is a mistake.`, ephemeral: true}); + if (commandFile){ + try{ + commandFile.command.run(client, interaction); + commandFile.command.autocomplete ? commandFile.command.autocomplete(interaction) : undefined; + commandFile.uses ? commandFile.uses++ : commandFile.uses = 1; + } catch (error){ + console.log(`An error occurred while running command "${interaction.commandName} ${interaction.options.getSubcommandGroup(false) ?? ''} ${interaction.options.getSubcommand(false) ?? ''}"`, error, error.stack); + return interaction.reply('An error occurred while running that command.'); + } + } + } + static async handleAutocomplete(client:TClient, interaction:Discord.AutocompleteInteraction) { + try { + await client.commands.get(interaction.commandName).command.autocomplete(client, interaction); + } catch (error){ + return console.log('An error occurred while running autocomplete:\n', error) + } + } + static handleButton(client:TClient, interaction:Discord.ButtonInteraction<'cached'>) { + if (interaction.customId.startsWith('reaction-') && client.config.botSwitches.buttonRoles){ + const RoleID = interaction.customId.replace('reaction-',''); + + const MFFarm1 = '1149139369433776269'; + const MFFarm2 = '1149139583729160325'; + + if (interaction.member.roles.cache.has(RoleID)) interaction.member.roles.remove(RoleID, 'Button Role').then(()=>interaction.reply({content: `You have been removed from <@&${RoleID}>`, ephemeral: true})); + else interaction.member.roles.add(RoleID, 'Button Role').then(()=>{ + this.roleConflictHandler(interaction, MFFarm1, MFFarm2, RoleID); + interaction.reply({content: `You have been added to <@&${RoleID}>`, ephemeral: true, fetchReply: true}) + }); + } else if (interaction.customId.includes('deleteEvalEmbed')) { + if (!client.config.whitelist.includes(interaction.user.id)) return interaction.reply({content: 'You are not whitelisted, therefore you cannot delete this message.', ephemeral: true}); + interaction.message.delete(); + Logger.console('log', logPrefix, `Eval embed has been deleted in #${interaction.message.channel.name} by ${interaction.member.displayName}`); + } else Logger.console('log', logPrefix, `Button has been pressed at ${interaction.message.url}`); } static roleConflictHandler(interaction:Discord.ButtonInteraction<'cached'>, role1:Discord.Snowflake, role2:Discord.Snowflake, newRole:Discord.Snowflake) { if (interaction.member.roles.cache.has(role1) && interaction.member.roles.cache.has(role2)) { diff --git a/src/models/tagSystem.ts b/src/models/tagSystem.ts index 929905b..d072523 100644 --- a/src/models/tagSystem.ts +++ b/src/models/tagSystem.ts @@ -61,6 +61,7 @@ export class TagSystemSvc { async sendTag(interaction:ChatInputCommandInteraction, tagName:string, targetId:Snowflake) { const getTag = await this.model.findOne({where: {tagname: tagName}}); const targetMsg = targetId ? `*This tag is directed at ${MessageTool.formatMention(targetId, 'user')}*` : ''; + const fetchUser = await interaction.guild?.members.fetch(getTag.dataValues.userid); const ic = interaction.client as TClient; const embedFormat = [ new ic.embed().setTitle(tagName).setColor(ic.config.embedColor) @@ -68,7 +69,7 @@ export class TagSystemSvc { .setDescription(getTag.dataValues.message) ]; if (getTag.dataValues.embedFlag) return await interaction.reply({content: targetMsg, embeds: embedFormat, allowedMentions: {parse: ['users']}}); - else return await interaction.reply({content: targetMsg+`\n**${getTag.dataValues.message}**`, allowedMentions: {parse: ['users']}}); + else return await interaction.reply({content: targetMsg+`\n**${getTag.dataValues.message}**\n╰ **${fetchUser.displayName}**`, allowedMentions: {parse: ['users']}}); } async modifyTag(tagname:string, message:string) { CacheServer.delete('tags');