From fe51404124970b62dc681e34b1b922d8fd6c14f3 Mon Sep 17 00:00:00 2001 From: Dhafit Farenza Date: Tue, 30 Jan 2024 22:51:25 +0700 Subject: [PATCH] add react command --- commands/react.command.js | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 commands/react.command.js diff --git a/commands/react.command.js b/commands/react.command.js new file mode 100644 index 0000000..3b37df9 --- /dev/null +++ b/commands/react.command.js @@ -0,0 +1,50 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { embedError } = require('../modules/utility'); + +const KETUA_KELAS_ID = process.env.ROLE_KETUA; + +exports.command = new SlashCommandBuilder() + .setName('react') + .setDescription('React to a message!') + .addStringOption((option) => + option.setName('message-id').setDescription('The message you want to react').setRequired(true), + ) + .addStringOption((option) => + option.setName('emoji').setDescription('The emoji you want to react').setRequired(true), + ); + +exports.permissions = [ + { + id: KETUA_KELAS_ID, + type: 'ROLE', + permission: true, + }, +]; + +exports.execute = async (interaction) => { + try { + await interaction.deferReply({ ephemeral: true, fetchReply: true }); + + const messageId = interaction.options.getString('message-id'); + const emojiOptions = interaction.options.getString('emoji'); + const message = await interaction.channel.messages.fetch(messageId); + + const emojis = emojiOptions.split(' '); + for (const emoji of emojis) { + await message.react(emoji); + } + + return interaction.editReply({ content: 'Reacted!' }); + } catch (error) { + console.error(error); + let errorMessage = error.message ?? 'Something went wrong!'; + + // Reference: https://discord.com/developers/docs/topics/opcodes-and-status-codes#:~:text=Unknown%20member-,10008,-Unknown%20message + if (error.code === 10008) { + errorMessage = 'Message not found!\nMake sure you are on the same channel with the message!'; + } + + const errorEmbed = embedError(errorMessage); + return interaction.editReply({ embeds: [errorEmbed], ephemeral: true }); + } +};