From 2b9c5efa3356ce4121b70bb084ae15da34d62ff7 Mon Sep 17 00:00:00 2001 From: VALOU3336 Date: Fri, 1 Mar 2024 12:05:29 +0100 Subject: [PATCH] add rolebutton --- commands/antiraid/rolelimit.js | 2 +- commands/gestion/autoreact.js | 2 +- commands/gestion/buttonsrole.js | 126 +++++++++++++++++++++++++++++++ commands/gestion/confession.js | 2 +- commands/gestion/rvocal.js | 2 +- commands/gestion/sync.js | 2 +- commands/logs/alerte.js | 2 +- commands/logs/logs.js | 2 +- commands/logs/messagelog.js | 2 +- commands/logs/modlog.js | 2 +- commands/logs/raidlog.js | 2 +- commands/logs/raidping.js | 2 +- commands/logs/rolelog.js | 2 +- commands/logs/vocallog.js | 2 +- commands/moderation/prison.js | 2 +- commands/moderation/slownmode.js | 2 +- commands/utils/botconfig.js | 2 +- commands/utils/help.js | 2 +- commands/utils/weather.js | 29 +++---- events/auto/buttonrolevent.js | 30 ++++++++ 20 files changed, 189 insertions(+), 30 deletions(-) create mode 100644 commands/gestion/buttonsrole.js create mode 100644 events/auto/buttonrolevent.js diff --git a/commands/antiraid/rolelimit.js b/commands/antiraid/rolelimit.js index 9401a94..89bb7e8 100644 --- a/commands/antiraid/rolelimit.js +++ b/commands/antiraid/rolelimit.js @@ -6,7 +6,7 @@ module.exports = { name: 'rolelimit', description: 'Affiche et gère les rôles limités', emote: '🔒', - utilisation: '+rolelimit', + utilisation: 'rolelimit', category: 'antiraid', async execute(interaction) { diff --git a/commands/gestion/autoreact.js b/commands/gestion/autoreact.js index 9278783..f3f62e7 100644 --- a/commands/gestion/autoreact.js +++ b/commands/gestion/autoreact.js @@ -5,7 +5,7 @@ module.exports = { name: 'autoreact', description: 'Ajoute des réactions automatiques à un salon', emote: '🔄', - utilisation: '+autoreact #salon/id ...', + utilisation: 'autoreact #salon/id ...', category: 'gestion', async execute(message, args) { if (args[0] === "remove" || args[0] === "del" ){ diff --git a/commands/gestion/buttonsrole.js b/commands/gestion/buttonsrole.js new file mode 100644 index 0000000..50da4f6 --- /dev/null +++ b/commands/gestion/buttonsrole.js @@ -0,0 +1,126 @@ +const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js'); + +module.exports = { + name: 'rolebutton', + description: 'Crée un message avec des boutons pour gérer les rôles', + emote: '🎭', + utilisation: 'rolebutton #salon/id ', + category: 'gestion', + async execute(message, args, client) { + const channel = message.mentions.channels.first() || message.guild.channels.cache.get(args[0]); + if (!channel) { + return message.reply('Salon invalide.'); + } + let buttonToRole = {}; + const embed = new EmbedBuilder() + .setTitle('Gestion des rôles') + .setDescription('Cliquez sur les boutons pour gérer les rôles.'); + const addButton = new ButtonBuilder() + .setCustomId('add_role') + .setLabel('Ajouté') + .setStyle(ButtonStyle.Primary); + + const removeButton = new ButtonBuilder() + .setCustomId('remove_role') + .setLabel('Supprime') + .setStyle(ButtonStyle.Danger); + + const sendButton = new ButtonBuilder() + .setCustomId('send_role') + .setLabel('Envoyé') + .setStyle(ButtonStyle.Success); + + const row = new ActionRowBuilder() + .addComponents(addButton, removeButton, sendButton); + + const baseembed = await message.channel.send({ embeds: [embed], components: [row] }); + + const filter = i => i.user.id === message.author.id; + const filter2 = m => m.author.id === message.author.id; + const collector = channel.createMessageComponentCollector({ filter: filter, time: 60000 * 2 }); + + collector.on('collect', async i => { + if (i.customId === 'add_role') { + await i.reply('Veuillez entrer le nom du rôle, l\'émoji, et le nom du bouton pour le bouton. Exemple : roleId notification 📧'); + + const collector = i.channel.createMessageCollector({ filter: filter2, max: 1, time: 60000 }); + collector.on('collect', async m => { + const [roleID, buttonName, emoji] = m.content.split(' '); + if (!roleID || !buttonName) { + return i.followUp('Format invalide. Veuillez entrer le nom du rôle et le nom du bouton séparés par des espaces.'); + } + + const emojiToUse = emoji || "aucun"; + + const role = m.guild.roles.cache.get(roleID); + if (!role) { + const role = m.guild.roles.cache.find(r => r.name.toLowerCase() === buttonName.toLowerCase()); + if (!role) { + return i.followUp('Rôle invalide. Veuillez entrer un ID de rôle valide ou un nom de rôle existant.'); + } + } + const roleExists = Object.values(buttonToRole).some(button => button.roleId === role.id); + if (roleExists) { + return i.followUp('Le rôle est déjà associé à un bouton.'); + } + buttonToRole[buttonName] = { emoji: emojiToUse, roleId: role.id }; + + await m.delete(); + await i.deleteReply(); + + const updatedEmbed = new EmbedBuilder() + .setTitle('Gestion des rôles') + .setDescription('Cliquez sur les boutons pour gérer les rôles.'); + + for (const [buttonName, { emoji, roleId }] of Object.entries(buttonToRole)) { + const role = message.guild.roles.cache.get(roleId); + const roleName = role ? role.name : 'Rôle non trouvé'; + updatedEmbed.addFields({name: buttonName, value: `${emoji} ${roleName}`}); + } + + await i.message.edit({ embeds: [updatedEmbed] }); + }); + } else if (i.customId === 'remove_role') { + + await i.reply('Veuillez entrer le nom du rôle à supprimer.'); + } else if (i.customId === 'send_role') { + await i.reply('Veuillez entrer l\'ID du message à mettre à jour.'); + + const filter = m => m.author.id === i.user.id; + const collector = i.channel.createMessageCollector({ filter: filter, max: 1, time: 60000 }); + + collector.on('collect', async m => { + const messageId = m.content; + try { + const messageToUpdate = await channel.messages.fetch(messageId); + if(messageToUpdate.author.id !== client.user.id){ + i.followUp("Le message n'a pas etait envoyer par moi") + } + + const buttons = []; + for (const [buttonName, { emoji, roleId }] of Object.entries(buttonToRole)) { + const button = new ButtonBuilder() + .setCustomId(`rolebuttons_${roleId}`) + .setLabel(buttonName) + .setStyle(ButtonStyle.Primary); + if (emoji !== "aucun") { + button.setEmoji(emoji); + } + buttons.push(button); + } + const actionRow = new ActionRowBuilder().addComponents(buttons); + + await messageToUpdate.edit({ components: [actionRow] }); + await baseembed.edit({ components : []}) + await m.delete(); + await i.deleteReply(); + await message.channel.send(`Les boutons ont bien été envoyés sur le message. Voici le lien : ${messageToUpdate.url}`); + } catch (error) { + console.error(error); + await i.followUp('Une erreur est survenue lors de la mise à jour du message.'); + } + }); + } + }); + }, +}; \ No newline at end of file diff --git a/commands/gestion/confession.js b/commands/gestion/confession.js index be6f61f..0690db4 100644 --- a/commands/gestion/confession.js +++ b/commands/gestion/confession.js @@ -6,7 +6,7 @@ module.exports = { description: 'Configure les paramètres pour le salon de confession.', category: 'gestion', emote: '🙏', - usage: 'confession [channel|send] [channelID|#channel]', + utilisation: 'confession [channel|send] [channelID|#channel]', async execute(message, args) { const guildId = message.guild.id; if (!args.length) return message.reply('Veuillez spécifier une sous-commande: channel ou send.'); diff --git a/commands/gestion/rvocal.js b/commands/gestion/rvocal.js index 9969305..520e392 100644 --- a/commands/gestion/rvocal.js +++ b/commands/gestion/rvocal.js @@ -5,7 +5,7 @@ module.exports = { name: 'rvocal', description: 'Enregistre les rôles associés à un salon vocal', emote: '🔊', - utilisation: '+rvocal ', + utilisation: 'rvocal ', category: 'gestion', async execute(message, args) { diff --git a/commands/gestion/sync.js b/commands/gestion/sync.js index 28de27f..a6c6893 100644 --- a/commands/gestion/sync.js +++ b/commands/gestion/sync.js @@ -4,7 +4,7 @@ module.exports = { description: "Synchronise les permissions d'un salon avec sa catégorie ou synchronise tous les salons d'une catégorie.", category: 'gestion', emote: '🔄', - utilisation: '+sync [#salon/id/all]', + utilisation: 'sync [#salon/id/all]', async execute(message, args, client) { let channel; if (args[0] === 'all') { diff --git a/commands/logs/alerte.js b/commands/logs/alerte.js index 2f91811..45777b4 100644 --- a/commands/logs/alerte.js +++ b/commands/logs/alerte.js @@ -6,7 +6,7 @@ module.exports = { description: 'Active ou désactive l\'alerte.', category: 'gestion', emote: '📢', - utilisation: '+alerte ', + utilisation: 'alerte ', async execute(message, args) { if (!args.length) return message.reply('Veuillez spécifier "on" ou "off".'); diff --git a/commands/logs/logs.js b/commands/logs/logs.js index 21d5b86..6359dd9 100644 --- a/commands/logs/logs.js +++ b/commands/logs/logs.js @@ -6,7 +6,7 @@ module.exports = { description: 'Affiche et configure les logs du serveur.', category: 'gestion', emote: '📝', - utilisation: '+logs', + utilisation: 'logs', async execute(message, args) { let data = await new Promise((resolve, reject) => { db.get('SELECT value FROM gestion WHERE id = ?', [message.client.user.id], (err, row) => { diff --git a/commands/logs/messagelog.js b/commands/logs/messagelog.js index 55aff64..4329a16 100644 --- a/commands/logs/messagelog.js +++ b/commands/logs/messagelog.js @@ -7,7 +7,7 @@ module.exports = { description: 'Enregistre l\'ID d\'un salon pour les logs de messages.', category: 'gestion', emote: '📝', - utilisation: '+messagelog [#salon/id]', + utilisation: 'messagelog [#salon/id]', async execute(message, args, client) { let channelId; const mentionedChannel = message.mentions.channels.first(); diff --git a/commands/logs/modlog.js b/commands/logs/modlog.js index 69d6412..8fda1d8 100644 --- a/commands/logs/modlog.js +++ b/commands/logs/modlog.js @@ -7,7 +7,7 @@ module.exports = { description: 'Enregistre l\'ID d\'un salon pour les logs de modération.', category: 'gestion', emote: '📝', - utilisation: '+modlog [#salon/id]', + utilisation: 'modlog [#salon/id]', async execute(message, args) { let channelId; const mentionedChannel = message.mentions.channels.first(); diff --git a/commands/logs/raidlog.js b/commands/logs/raidlog.js index aaf2239..7b03999 100644 --- a/commands/logs/raidlog.js +++ b/commands/logs/raidlog.js @@ -6,7 +6,7 @@ module.exports = { description: 'Enregistre l\'ID d\'un salon pour les logs de raid.', category: 'gestion', emote: '📝', - utilisation: '+raidlog [#salon/id]', + utilisation: 'raidlog [#salon/id]', async execute(message, args) { let channelId; const mentionedChannel = message.mentions.channels.first(); diff --git a/commands/logs/raidping.js b/commands/logs/raidping.js index bc0db64..28f962d 100644 --- a/commands/logs/raidping.js +++ b/commands/logs/raidping.js @@ -6,7 +6,7 @@ module.exports = { description: 'Enregistre le message pour les logs de raid.', category: 'antiraid', emote: '📝', - utilisation: '+raidping ', + utilisation: 'raidping ', async execute(message, args) { // Join all elements from args[0] onwards to form the full message const messageContent = args.slice(0).join(' '); diff --git a/commands/logs/rolelog.js b/commands/logs/rolelog.js index f0bc5a2..bfba46d 100644 --- a/commands/logs/rolelog.js +++ b/commands/logs/rolelog.js @@ -6,7 +6,7 @@ module.exports = { description: 'Enregistre l\'ID d\'un salon pour les logs de rôles.', category: 'gestion', emote: '📝', - utilisation: '+rolelog [#salon/id]', + utilisation: 'rolelog [#salon/id]', async execute(message, args) { let channelId; const mentionedChannel = message.mentions.channels.first(); diff --git a/commands/logs/vocallog.js b/commands/logs/vocallog.js index 4a20e3a..f593ff3 100644 --- a/commands/logs/vocallog.js +++ b/commands/logs/vocallog.js @@ -6,7 +6,7 @@ module.exports = { description: 'Enregistre l\'ID d\'un salon pour les logs vocaux.', category: 'gestion', emote: '📝', - utilisation: '+vocallog [#salon/id]', + utilisation: 'vocallog [#salon/id]', async execute(message, args) { let channelId; const mentionedChannel = message.mentions.channels.first(); diff --git a/commands/moderation/prison.js b/commands/moderation/prison.js index 3dd3cca..97d93ef 100644 --- a/commands/moderation/prison.js +++ b/commands/moderation/prison.js @@ -6,7 +6,7 @@ module.exports = { description: "Configure le salon de prison pour le serveur.", category: 'gestion', emote: '🔒', - utilisation: '+prison [@user/remove/config] [#channel/@user]', + utilisation: 'prison [@user/remove/config] [#channel/@user]', async execute(message, args) { if (args[0] === 'config') { diff --git a/commands/moderation/slownmode.js b/commands/moderation/slownmode.js index 03cd2f0..4661553 100644 --- a/commands/moderation/slownmode.js +++ b/commands/moderation/slownmode.js @@ -3,7 +3,7 @@ module.exports = { description: "Met le salon en mode lent avec un délai spécifié en secondes.", category: 'moderation', emote: '⏳', - utilisation: '+slownmode ', + utilisation: 'slownmode ', async execute(message, args) { if (!args[0] || isNaN(args[0])) { diff --git a/commands/utils/botconfig.js b/commands/utils/botconfig.js index d25ef03..d463375 100644 --- a/commands/utils/botconfig.js +++ b/commands/utils/botconfig.js @@ -6,7 +6,7 @@ module.exports = { aliases: ['botconfig'], category: 'gestion', emote: '🤖', - utilisation: '+botconfig', + utilisation: 'botconfig', async execute(message, args, client) { const bot = client.user; diff --git a/commands/utils/help.js b/commands/utils/help.js index 16c8872..6e1ccae 100644 --- a/commands/utils/help.js +++ b/commands/utils/help.js @@ -108,7 +108,7 @@ module.exports = { let description = `Sélectionnez une catégorie parmi les options ci-dessous pour obtenir de l'aide sur les commandes correspondantes.\n Il y a ${totalCommands} commandes disponibles\n\n`; for (const [category, commands] of Object.entries(categories)) { - description += `**Catégorie ${category}**\n`; + description += `**Catégorie ${category}** (${commands.length})\n`; description += commands.length > 0 ? '> ' + commands.map(command => `\`${command.name}\``).join(', ') : '> Aucune commande dans cette catégorie'; description += '\n\n'; } diff --git a/commands/utils/weather.js b/commands/utils/weather.js index 1e6b9c0..2b062d6 100644 --- a/commands/utils/weather.js +++ b/commands/utils/weather.js @@ -1,6 +1,12 @@ -const weather = require('weather'); +const Weather = require('weather'); const { EmbedBuilder } = require('discord.js'); +const appID = ''; +const appCode = ''; +const weather = new Weather({ + appID, + appCode +}); module.exports = { name: 'weather', description: 'Affiche les informations météorologiques d\'une ville', @@ -8,19 +14,16 @@ module.exports = { emote: '☀️', utilisation: 'weather [ville]', async execute(message, args) { - if (!args.length) { - return message.channel.send('Veuillez fournir une ville.'); + if (args.length < 2) { + return message.channel.send('Veuillez fournir une ville et un pays.'); } - - const city = args.join(' '); - const woeid = await weather.find({ search: city, degreeType: 'C' }); - - if (!woeid || woeid.length === 0) { - return message.channel.send('Ville non trouvée.'); - } - - const weatherData = await weather.get({ search: woeid[0].title, degreeType: 'C' }); - + + const city = args[0]; + const country = args[1]; + + const weatherData = await weather.now(`${city}, ${country}`).then((results) => { + console.log(results); + }); const embed = new EmbedBuilder() .setTitle(`🌤️ Météo pour ${weatherData.location.name}`) .setDescription(`Température: ${weatherData.current.temperature}°C`) diff --git a/events/auto/buttonrolevent.js b/events/auto/buttonrolevent.js new file mode 100644 index 0000000..355637b --- /dev/null +++ b/events/auto/buttonrolevent.js @@ -0,0 +1,30 @@ +const {Events } = require("discord.js") + +module.exports = { + name: Events.InteractionCreate, + async execute(interaction, client) { + const botId = client.user.id; + const customId = interaction.customId; + if (!customId.startsWith('rolebuttons_')) return; + + const roleId = customId.split('_')[1]; + const role = interaction.guild.roles.cache.get(roleId); + + if (!role) { + return; + } + + const member = interaction.member; + if (!member) { + return; + } + + if (member.roles.cache.has(role.id)) { + await member.roles.remove(role); + await interaction.reply({ content: `Le rôle ${role.name} a été retiré.`, ephemeral: true }); + } else { + await member.roles.add(role); + await interaction.reply({ content: `Le rôle ${role.name} a été ajouté.`, ephemeral: true }); + } + }, +}; \ No newline at end of file