112 lines
3.2 KiB
JavaScript
112 lines
3.2 KiB
JavaScript
|
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;
|
||
|
}
|
||
|
}
|