125 lines
4.0 KiB
JavaScript
125 lines
4.0 KiB
JavaScript
|
const { SlashCommandBuilder } = require('@discordjs/builders');
|
||
|
const { MessageActionRow, MessageButton, MessageSelectMenu, Collection, MessageEmbed } = require('discord.js');
|
||
|
var selectRole = null;
|
||
|
const commandUser = new Collection();
|
||
|
|
||
|
module.exports = {
|
||
|
data: new SlashCommandBuilder()
|
||
|
.setName('setrole')
|
||
|
.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 = 'Edition de rôles - Utilisable pendant 3 minutes\n\nVoici la liste des rôles que vous pouvez vous ajouter ou supprimer';
|
||
|
|
||
|
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: 5 * 1000,
|
||
|
});
|
||
|
|
||
|
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 = roles.get(i.values[0]);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (i.customId === 'btnAdd') {
|
||
|
//i.member.roles.add(roles.get(selectRole));
|
||
|
if (selectRole != null) {
|
||
|
if (interaction.member.roles.cache.has(selectRole.id)) {
|
||
|
interaction.editReply(messageContent + ' - *Le rôle "' + selectRole.name + '" vous est déjà attribué*');
|
||
|
|
||
|
} else {
|
||
|
interaction.member.roles.add(selectRole);
|
||
|
interaction.editReply(messageContent + ' - *Le rôle "' + selectRole.name + '" a été ajouté*');
|
||
|
selectRole = null;
|
||
|
}
|
||
|
} else {
|
||
|
interaction.editReply(messageContent + ' - *Veuillez sélectionner un rôle pour l\'ajouter*');
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (i.customId === 'btnDel') {
|
||
|
if (selectRole != null) {
|
||
|
if (!interaction.member.roles.cache.has(selectRole.id)) {
|
||
|
interaction.editReply(messageContent + ' - *Le rôle "' + selectRole.name + '" ne vous est pas attribué*');
|
||
|
} else {
|
||
|
interaction.member.roles.remove(selectRole);
|
||
|
interaction.editReply(messageContent + ' - *Le rôle "' + selectRole.name + '" a été enlevé*');
|
||
|
selectRole = null;
|
||
|
}
|
||
|
} else {
|
||
|
interaction.editReply(messageContent + ' - *Veuillez sélectionner un rôle pour le supprimer*');
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
collector.on('end', () => {
|
||
|
commandUser.delete(interaction.user.id);
|
||
|
interaction.editReply({content: 'Le délai de 3 minutes est passé. Pour modifier à nouveau les rôles, merci de relancer la commande.', components: [], ephemeral: true});
|
||
|
});
|
||
|
})
|
||
|
|
||
|
}
|
||
|
};
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|