From 7172cb0c04f2e55127631860e72db18b8368a621 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 13 Sep 2023 16:11:45 -0400 Subject: [PATCH] Mae the reaction buttons actually do something --- index.js | 74 ++++++++++++++++++++---------------- utils/interaction-trigger.js | 39 +++++++++++++++++++ 2 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 utils/interaction-trigger.js diff --git a/index.js b/index.js index b2961df..fb29ce5 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,9 @@ const metrics = require('./utils/pm2-metrics.js'); // Use a custom logging script const logger = require('./utils/logging.js'); +// Listen for (Semi-)Permenant Interactions +const interactionListener = require('./utils/interaction-trigger.js'); + // Require the necessary discord.js classes const { Client, Collection, Events, GatewayIntentBits } = require('discord.js'); const { token, botOwner } = require('./config.json'); @@ -60,47 +63,52 @@ client.once(Events.ClientReady, c => { // Client "on" Events // Someone used an interaction client.on(Events.InteractionCreate, async interaction => { - if (!interaction.isChatInputCommand()) return; + if (interaction.isChatInputCommand()) { - const command = interaction.client.commands.get(interaction.commandName); + const command = interaction.client.commands.get(interaction.commandName); - if (!command) { - logger.log(logger.logLevels.ERROR, `No command matching ${interaction.commandName} was found.`); - await interaction.reply({ content: `This command no longer exists! Please contact ${botOwner} to report that this is happening!`, ephemeral: true }); + if (!command) { + logger.log(logger.logLevels.ERROR, `No command matching ${interaction.commandName} was found.`); + await interaction.reply({ content: `This command no longer exists! Please contact ${botOwner} to report that this is happening!`, ephemeral: true }); - // Report error to PM2 dashboard - metrics.interactionErrors.inc(); - metrics.io.notifyError(new Error('Interaction doesn\'t exist'), { - custom: { - interactionCommand: interaction.commandName, - }, - }); + // Report error to PM2 dashboard + metrics.interactionErrors.inc(); + metrics.io.notifyError(new Error('Interaction doesn\'t exist'), { + custom: { + interactionCommand: interaction.commandName, + }, + }); - return; - } + return; + } - try { - await command.execute(interaction); - } - catch (error) { - logger.log(logger.logLevels.ERROR, error); - await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + try { + await command.execute(interaction); + } + catch (error) { + logger.log(logger.logLevels.ERROR, error); + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); - // Report error to PM2 dashboard - metrics.interactionErrors.inc(); - metrics.io.notifyError(new Error('Error executing interaction'), { - custom: { - interactionCommand: interaction.commandName, - error: error, - }, - }); + // Report error to PM2 dashboard + metrics.interactionErrors.inc(); + metrics.io.notifyError(new Error('Error executing interaction'), { + custom: { + interactionCommand: interaction.commandName, + error: error, + }, + }); + } + // Successful Execution, report as a PM2 metric + // If the bot gets a lot of use, consider removing this for performance + metrics.interactionSuccess(); } - - // Successful Execution, report as a PM2 metric - // If the bot gets a lot of use, consider removing this for performance - metrics.interactionSuccess(); - + else if (interaction.isButton()) { + interactionListener.buttonInteraction(interaction); + } + else if (interaction.isStringSelectMenu()) { + // respond to the select menu + } }); // Joined a server diff --git a/utils/interaction-trigger.js b/utils/interaction-trigger.js new file mode 100644 index 0000000..8a64153 --- /dev/null +++ b/utils/interaction-trigger.js @@ -0,0 +1,39 @@ +const { Events } = require('discord.js'); + +const buttonInteraction = function(interaction) { + const splitInteraction = interaction.customId.split('-'); + + (async () => { + if (splitInteraction[0] === 'role') { + const client = interaction.client; + const guild = await client.guilds.fetch(interaction.guildId); + const member = interaction.member; + const role = await guild.roles.fetch(splitInteraction[1]); + + if (member.roles.cache.find(r => r.id === splitInteraction[1])) { + try { + member.roles.remove(splitInteraction[1]); + await interaction.reply({ content: `Removed role ${role} from ${interaction.user}!`, ephemeral: true }); + } + catch { + await interaction.reply({ content: 'An error has occurred and the role was not removed. Likely I don\'t have the needed permissions!', ephemeral: true }); + } + } + else { + try { + member.roles.add(splitInteraction[1]); + await interaction.reply({ content: `Added role ${role} to ${interaction.user}!`, ephemeral: true }); + } + catch { + await interaction.reply({ content: 'An error has occurred and the role was not added. Likely I don\'t have the needed permissions!', ephemeral: true }); + } + } + } + })(); + return; +}; + +module.exports = { + name: Events.InteractionCreate, + buttonInteraction, +}; \ No newline at end of file