From eab0c033178fc64fc9c8dba2b8d561e920d5aab4 Mon Sep 17 00:00:00 2001 From: Nemo Date: Sat, 13 Nov 2021 13:19:04 +0100 Subject: [PATCH] temp add roll command --- commands/role.js | 112 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 commands/role.js diff --git a/commands/role.js b/commands/role.js new file mode 100644 index 0000000..622b92f --- /dev/null +++ b/commands/role.js @@ -0,0 +1,112 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const { MessageActionRow, MessageButton, MessageSelectMenu, Collection } = require('discord.js'); +var selectRole = null; +const commandUser = new Collection(); + +module.exports = { + data: new SlashCommandBuilder() + .setName('role') + .setDescription('Gère les rôles de votre profil'), + async execute(interaction) { + + if (commandUser.has(interaction.user.id)) { + interaction.reply({ content: 'Vous êtes déjà en train d\'utiliser cette commande', ephemeral: true }); + return; + } + + const selectMenu = new MessageSelectMenu() + .setCustomId('select') + .setPlaceholder('Nothing selected'); + const btnAdd = new MessageButton() + .setCustomId('btnAdd') + .setLabel('Ajouter') + .setStyle('SUCCESS'); + const btnDel = new MessageButton() + .setCustomId('btnDel') + .setLabel('Supprimer') + .setStyle('DANGER'); + + const roles = await interaction.guild.roles.fetch(); + + var botRoles = interaction.guild.me.roles.cache; + + var maxPos = 0; + botRoles.forEach(role => { + if (role.rawPosition > maxPos) maxPos = role.rawPosition; + }); + + roles.forEach(element => { + if (element.managed != true && element.rawPosition < maxPos) { + selectMenu.addOptions([ + { + label: element.name, + description: element.reason, + value: element.id + } + ]); + } + }); + + const row1 = new MessageActionRow().addComponents(selectMenu); + const row2 = new MessageActionRow().addComponents(btnAdd, btnDel); + const messageContent = 'Voici la liste des rôles que vous pouvez vous ajouter ou enlever'; + + await interaction.reply({ content: messageContent, components: [row1, row2], ephemeral: true, fetchReply: true }) + .then((message) => { + const filter = (btnInt) => btnInt.customId === 'select' || btnInt.customId === 'btnAdd' || btnInt.customId === 'btnDel'; + + const collector = interaction.channel.createMessageComponentCollector({ + filter: filter, + time: 3 * 60000, + }); + + commandUser.set(interaction.user.id, interaction); + var msg = undefined; + + collector.on('collect', async i => { + i.deferUpdate(); + + if (i.user.id === interaction.user.id) { + if (i.customId === 'select') { + selectRole = i.values[0]; + return; + } + + if (i.customId === 'btnAdd') { + //i.member.roles.add(roles.get(selectRole)); + if(selectRole != null){ + interaction.editReply(messageContent + ' - *Le rôle ' + roles.get(selectRole).name + ' a été ajouté*'); + }else{ + interaction.editReply(messageContent + ' - *Veuillez sélectionner un rôle pour l\'ajouter*'); + } + return; + } + + if (i.customId === 'btnDel') { + if(selectRole != null){ + interaction.editReply(messageContent + ' - *Le rôle a été enlevé*'); + }else{ + interaction.editReply(messageContent + ' - *Veuillez sélectionner un rôle pour l\'enlever*'); + } + return; + } + } + }); + + collector.on('end', () => { + commandUser.delete(interaction.user.id); + }); + + }) + + } +}; + +async function Reply(str, i, msg) { + if (msg === undefined) { + return await i.channel.send({ content: str, ephemeral: true, fetchReply: true }); + } else { + msg.edit(str); + return msg; + } +} \ No newline at end of file