From 0a4181f583e9ba47323162696ce4287692f36101 Mon Sep 17 00:00:00 2001 From: Taken Date: Tue, 18 Apr 2023 17:25:17 +0200 Subject: [PATCH] Added first context menu commands --- commands-contextmenu/resetnick.js | 34 ++++++++++++++++++++++++++++++ index.js | 35 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 commands-contextmenu/resetnick.js diff --git a/commands-contextmenu/resetnick.js b/commands-contextmenu/resetnick.js new file mode 100644 index 0000000..676dddd --- /dev/null +++ b/commands-contextmenu/resetnick.js @@ -0,0 +1,34 @@ +const { ContextMenuCommandBuilder, ApplicationCommandType, PermissionFlagsBits } = require('discord.js'); + +module.exports = { + name: 'Reset Nickname', + description: 'Reset your nickname.', + type: 'contextmenu', + + data: new ContextMenuCommandBuilder() + .setName('resetnick') + .setType(ApplicationCommandType.User) + .setDefaultMemberPermissions(PermissionFlagsBits.ManageNicknames), + + async execute(interaction) { + + const { targetId } = interaction + const target = await interaction.guild.members.fetch(targetId); + + if (!target) { + return interaction.reply({ content: 'That user does not exist.', ephemeral: true }); + } + + if (target.id === interaction.user.id) { + return interaction.reply({ content: 'You can\'t reset your own nickname.', ephemeral: true }); + } + + if (!target.manageable) { + return interaction.reply({ content: 'I cannot reset that user\'s nickname.', ephemeral: true }); + } + + await target.setNickname(target.user.username, 'Reset by ' + interaction.user.username + "#" + interaction.user.discriminator); + + return interaction.reply({ content: `Reset ${target.user.username}'s nickname.`, ephemeral: true }); + } +}; \ No newline at end of file diff --git a/index.js b/index.js index 2acfe00..2e2b3f4 100644 --- a/index.js +++ b/index.js @@ -77,6 +77,41 @@ client.on(Events.InteractionCreate, async interaction => { } }); +//! commands +const contextMenuPath = path.join(__dirname, 'commands-contextmenu'); +const contextMenuFiles = fs.readdirSync(contextMenuPath).filter(file => file.endsWith('.js')); + +for (const file of contextMenuFiles) { + + const filePath = path.join(contextMenuPath, file); + const cmd = require(filePath); + + if ('data' in cmd && 'execute' in cmd && cmd.type === 'contextmenu') { + client.commands.set(cmd.data.name, cmd); + } else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data", "execute" or "type" property.`); + } +} + +//! context menu command handler +client.on(Events.InteractionCreate, async interaction => { + if(!interaction.isContextMenuCommand()) return; + + const command = interaction.client.commands.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; + } + + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }) + } +}); + //! button events const btnPath = path.join(__dirname, 'events', 'buttons'); const btnFiles = fs.readdirSync(btnPath).filter(file => file.endsWith('.js'));