From d8a697e5832558238975a59047ae95c1a8716065 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 17 Jun 2023 21:49:46 -0400 Subject: [PATCH] #13 - Added command to allow users to give other users their group - Shows all groups as options but will not let user add another user if the caller doesn't have the role --- Server/commands/giveRole.js | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Server/commands/giveRole.js diff --git a/Server/commands/giveRole.js b/Server/commands/giveRole.js new file mode 100644 index 0000000..7a54bb7 --- /dev/null +++ b/Server/commands/giveRole.js @@ -0,0 +1,52 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { DebugBuilder } = require("../utilities/debugBuilder"); +const log = new DebugBuilder("server", "give-role"); + +module.exports = { + data: new SlashCommandBuilder() + .setName('give-role') + .setDescription('Use this command to give a role you have to another member.') + .addUserOption(option => + option.setName('user') + .setDescription('The user you wish to give the role to ') + .setRequired(true)) + .addRoleOption(option => + option.setName('role') + .setDescription('The role you wish to give the selected user') + .setRequired(true)), + example: "give-role", + isPrivileged: false, + requiresTokens: false, + defaultTokenUsage: 0, + deferInitialReply: true, + /*async autocomplete(interaction) { + const focusedValue = interaction.options.getFocused(); + },*/ + async execute(interaction) { + try{ + // The role to give to the user + const selectedRole = interaction.options.getRole('role'); + + // The user who should be given the role + var selectedUser = interaction.options.getUser("user"); + selectedUser = interaction.guild.members.cache.get(selectedUser.id); + + + // The user who initiated the command + const initUser = interaction.member; + + log.DEBUG("Give Role DEBUG: ", initUser, selectedRole, selectedUser); + + // Check if the user has the role selected + if (!initUser.roles.cache.find(role => role.name === selectedRole.name)) return await interaction.editReply(`Sorry ${initUser}, you don't have the group ${selectedRole} and thus you cannot give it to ${selectedUser}`); + + // Give the selected user the role and let both the user and the initiator know + await selectedUser.roles.add(selectedRole); + + return await interaction.editReply(`Ok ${initUser}, ${selectedUser} has been given the ${selectedRole} role!`) + }catch(err){ + log.ERROR(err) + //await interaction.reply(err.toString()); + } + } +}; \ No newline at end of file