From c2c53edc589cca03fce72338bb3512606929c25b Mon Sep 17 00:00:00 2001 From: RADEMAKER Robin Date: Wed, 30 Mar 2022 08:34:27 +0200 Subject: [PATCH] Reaction add Role --- config.json | 4 +-- index.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/config.json b/config.json index b150276..97136e5 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { - "clientId": "890975056400904242", + "clientId": "905433349080875038", "guildId": "876543210987654321", - "token": "ODkwOTc1MDU2NDAwOTA0MjQy.YU3nLg.T5wCbJ4UuDBanMjVaE-JX-aSELo", + "token": "OTA1NDMzMzQ5MDgwODc1MDM4.YYKAhA.lX9_YqbkTxlkSfnYPOKa2pl5V8s", "authorId": "307975903999164416" } \ No newline at end of file diff --git a/index.js b/index.js index f1b7830..e59ce7a 100644 --- a/index.js +++ b/index.js @@ -2,16 +2,26 @@ const Discord = require('discord.js'); const config = require('./config.json'); const {SlashCommandBuilder} = require("@discordjs/builders") const friendsCommands = require('./friendsCommands.json') +const {GuildEmoji, Emoji, CommandInteraction} = require("discord.js"); const Client = new Discord.Client({ intents: [ Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, - Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS + Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS, + Discord.Intents.FLAGS.GUILD_MEMBERS ] }); const data = new SlashCommandBuilder() - .setName("games") - .setDescription("Afficher les mini-jeux"); + .setName("react") + .setDescription("Créer un ajout de rôle") + .addStringOption(option => + option.setName('role') + .setDescription('Le rôle à ajouter') + .setRequired(true) + .addChoice('Lol', 'League_of_Legends') + .addChoice('OW', 'OverWatch') + .addChoice('SW', 'Summoners_War') + ) let embedCommands = new Discord.MessageEmbed().setTitle("Liste des commandes").setColor("#ff0505") @@ -22,8 +32,9 @@ let embedCommands = new Discord.MessageEmbed().setTitle("Liste des commandes").s Client.on("ready", async () => { fromCommandsToEmbed(friendsCommands.commands) + console.log(`Logged in as ${Client.user.tag}!`) // pour tout les serveurs - lent : Client.application.commands.create(data) - await Client.guilds.cache.get("903598468306702336").commands.create(data) + /*await Client.guilds.cache.get("903598468306702336").commands.create(data) .then(() => { console.log(`Logged in as ${Client.user.tag}!`) @@ -31,16 +42,25 @@ Client.on("ready", async () => { .catch(r => { console.log("Erreur lors de l'ajout de la commande\n" + r) Client.destroy() - }) + })*/ }) Client.login(config.token) - +/* config prod +* { + "clientId": "890975056400904242", + "guildId": "876543210987654321", + "token": "ODkwOTc1MDU2NDAwOTA0MjQy.YU3nLg.T5wCbJ4UuDBanMjVaE-JX-aSELo", + "authorId": "307975903999164416" +} +* */ // dev token OTA1NDMzMzQ5MDgwODc1MDM4.YYKAhA.lX9_YqbkTxlkSfnYPOKa2pl5V8s && "clientId": "905433349080875038", Client.on("interactionCreate", async interaction => { if (interaction.isCommand()) { - if (interaction.commandName === "games") { + const {commandName} = interaction + + if (commandName === "games") { let embed = new Discord.MessageEmbed() .setTitle("Mini-Jeux") .setColor("#ff0505") @@ -51,10 +71,36 @@ Client.on("interactionCreate", async interaction => { .addField("__LETTERS__", "https://edjefferson.com/letterle/ ") .setTimestamp() await interaction.channel.send({embeds: [embed]}) - } else if (interaction.commandName === "help") { + interaction.reply({content: 'Done ! :white_check_mark:', ephemeral: true,}) + + } else if (commandName === "help") { await interaction.channel.send({embeds: [embedCommands]}) + interaction.reply({content: 'Done ! :white_check_mark:', ephemeral: true,}) + + } else if (commandName === 'react') { + const message = await interaction.reply({content: 'Ajout du role : ' + interaction.options._hoistedOptions[0].value, fetchReply: true}); + console.log( interaction.options) + + message.react('👍') + .then(() => message.react('👎')) + .catch(error => console.error('One of the emojis failed to react:', error)) + + const filter = (reaction, user) => { + return ['👍', '👎'].includes(reaction.emoji.name) && user.id === interaction.user.id; + }; + message.awaitReactions({filter, max: 1, time: 60000, errors: ['time']}) + .then(collected => { + const reaction = collected.first(); + if (reaction.emoji.name === '👍') { + message.reply('Ajout effectué !'); + } else { + message.reply('Suppression effectuée !'); + } + }) + .catch(collected => { + message.reply('You reacted with neither a thumbs up, nor a thumbs down.'); + }); } - interaction.reply({content: 'Done ! :white_check_mark:', ephemeral: true,}) } }) Client.on('messageCreate', message => { @@ -62,7 +108,6 @@ Client.on('messageCreate', message => { console.log(`Message [${message.content}] sent from : ${message.author.username}#${message.author.tag}`) */ if (message.author.bot) { - return; } else if (message.content === "55857548596523514251458-)Nydalicor" && message.author.id === config.authorId) { Client.destroy() console.log("Bye Bye") @@ -79,6 +124,24 @@ Client.on('messageCreate', message => { }) } }) +Client.on('messageReactionAdd', async (reaction, user) => { + // When a reaction is received, check if the structure is partial + if (reaction.partial) { + // If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled + try { + await reaction.fetch(); + if (reaction.emoji.name === '👍') { + + reaction.reply("J'ajoute ton rôle") + } + } catch + (error) { + console.error('Something went wrong when fetching the message:', error); + // Return as `reaction.message.author` may be undefined/null + + } + } +}) function fromCommandsToEmbed(commands) {