Mae the reaction buttons actually do something

This commit is contained in:
Michael 2023-09-13 16:11:45 -04:00
parent 217da0a524
commit 7172cb0c04
2 changed files with 80 additions and 33 deletions

View file

@ -13,6 +13,9 @@ const metrics = require('./utils/pm2-metrics.js');
// Use a custom logging script // Use a custom logging script
const logger = require('./utils/logging.js'); const logger = require('./utils/logging.js');
// Listen for (Semi-)Permenant Interactions
const interactionListener = require('./utils/interaction-trigger.js');
// Require the necessary discord.js classes // Require the necessary discord.js classes
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js'); const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { token, botOwner } = require('./config.json'); const { token, botOwner } = require('./config.json');
@ -60,47 +63,52 @@ client.once(Events.ClientReady, c => {
// Client "on" Events // Client "on" Events
// Someone used an interaction // Someone used an interaction
client.on(Events.InteractionCreate, async 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) { if (!command) {
logger.log(logger.logLevels.ERROR, `No command matching ${interaction.commandName} was found.`); 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 }); 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 // Report error to PM2 dashboard
metrics.interactionErrors.inc(); metrics.interactionErrors.inc();
metrics.io.notifyError(new Error('Interaction doesn\'t exist'), { metrics.io.notifyError(new Error('Interaction doesn\'t exist'), {
custom: { custom: {
interactionCommand: interaction.commandName, interactionCommand: interaction.commandName,
}, },
}); });
return; return;
} }
try { try {
await command.execute(interaction); await command.execute(interaction);
} }
catch (error) { catch (error) {
logger.log(logger.logLevels.ERROR, error); logger.log(logger.logLevels.ERROR, error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
// Report error to PM2 dashboard // Report error to PM2 dashboard
metrics.interactionErrors.inc(); metrics.interactionErrors.inc();
metrics.io.notifyError(new Error('Error executing interaction'), { metrics.io.notifyError(new Error('Error executing interaction'), {
custom: { custom: {
interactionCommand: interaction.commandName, interactionCommand: interaction.commandName,
error: error, error: error,
}, },
}); });
}
// 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()) {
// Successful Execution, report as a PM2 metric interactionListener.buttonInteraction(interaction);
// If the bot gets a lot of use, consider removing this for performance }
metrics.interactionSuccess(); else if (interaction.isStringSelectMenu()) {
// respond to the select menu
}
}); });
// Joined a server // Joined a server

View file

@ -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,
};