From 1864565977c8936a1d932e5bc2312777a8345ed2 Mon Sep 17 00:00:00 2001 From: Nathan Steel Date: Mon, 1 Aug 2022 10:27:48 -0400 Subject: [PATCH] Add command for self-role management --- commands/roles.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 commands/roles.js diff --git a/commands/roles.js b/commands/roles.js new file mode 100644 index 0000000..c089f65 --- /dev/null +++ b/commands/roles.js @@ -0,0 +1,54 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('role') + .setDescription('Apply a role to yourself, or remove yourself from it. Toggley') + .addStringOption(option => + option.setName("role") + .setDescription('The role you wish to join') + .setRequired(true) + .addChoices( + { name: 'Smite', value: '843092235016208404' }, + { name: 'Factorio', value: '995806023094894642' }, + { name: 'Minecraft', value: '854460008292286494' }, + )), + async execute(interaction) { + + // Get role id from the choice + let roleId = interaction.options.getString('role'); + // Then get the role itself + let role = interaction.guild.roles.cache.get(roleId); + + // If the role doesn't exist, then exit + if (!role){ + await interaction.reply('Role not found'); + console.log('Role not found ' + roleId); + return; + } + + // TODO: + // Check if the user is already in role. + // If they are we want to remove them + if (interaction.member.roles.cache.has(roleId)) { + console.log('in role already'); + var reply = 'You\'ve been removed from the role ' + interaction.options.getString('role'); + // Remove user from role + interaction.member.roles.remove(role); + } + else { + var reply = 'You\'ve been added to the role ' + interaction.options.getString('role'); + // Add user to role + interaction.member.roles.add(role); + } + + // Return message to the user in Discord channel + // Ephemeral only shows to the person that called the command + await interaction.reply({ + content: reply, + ephemeral: true + }); + + }, +}; +