简体   繁体   中英

remove reactions after click discord.js

I am creating music bot using discord.js and already created a custom embed with reactions which can pause , stop etc. the music.

I would like to make sure that when any user clicks on the reaction this is automatically removed , I tried to use the reactionCollector following the Guide but I was unable to implement it, can you help me out?

This is my code:

const { Message } = require("discord.js");

module.exports = {
    name: 'setup',
    description: "ruoli con emote",
    async execute(message, args, Discord, client, testChannelId) {

        console.log(testChannelId)
        const channel = '831573587579371580';
        const playem = '⏯️';
        const stopem = '⏹️';
        const nextem = '⏭️';
        //const shuffleem = '🔀';
        const loopem = '🔄';
        const volume15 = '🔉';
        const volume30 = '🔊';
        const mute = '🔇';

        
        let embed = new Discord.MessageEmbed()
       
            .setColor('#e42643')
            .setTitle('Nessuna canzone in riproduzione al momento :(')
            .setImage('https://images4.alphacoders.com/943/943845.jpg')
            .addFields(
                {name: 'Rule 1', value: 'figo'},
                {name: 'Rule 2', value: 'hhj'},
                {name: 'Rule 3', value: 'tipo'}
            )    
            .setFooter('il prefisso per questo server è: *');

        console.log('message: ' + message)
        console.log('args: ' + args)
        console.log('Discord: ' + Discord)
        console.log('client: ' + client)
       
        var botname = '𝐃𝐉 𝐌𝐮𝐬𝐢𝐜𝐚' // setup messaggio con reazioni
    
        const createdChannel = await message.guild.channels.create(botname, { //Create a channel

            type: 'text', //Make sure the channel is a text channel
            permissionOverwrites: [{ //Set permission overwrites
                id: message.guild.id,
                allow: ['VIEW_CHANNEL'],
            }],            

    }).then(createdChannel =>  createdChannel.send(createdChannel.id, embed)).then(function (messageEmbed) {
               
        messageEmbed.react(playem);
        messageEmbed.react(stopem);
        messageEmbed.react(nextem);
        //messageEmbed.react(shuffleem); 
        messageEmbed.react(loopem);
        messageEmbed.react(volume15);
        messageEmbed.react(volume30);
        messageEmbed.react(mute);        
  
        client.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;       
                      
                switch (reaction.emoji.name) {
                    
                    case playem:
                        console.log('Pausa / resume');
                        client.commands.get('prova').execute(client, message, args, Discord, createdChannel);
                        break;

                    case stopem:
                        console.log('stop');
                        client.commands.get('stop').execute(client, message, args);
                        break;

                    case nextem:
                        console.log('Skip');
                        client.commands.get('skip').execute(client, message, args);
                        break;

                    case loopem:
                        console.log('loop');
                        client.commands.get('loop').execute(client, message, args)
                        break;

                    case volume15:
                        console.log('volume15');
                        client.commands.get('volume15').execute(client, message, args)
                        break;

                    case volume30:
                        console.log('volume30');
                        client.commands.get('volume30').execute(client, message, args)
                        break;

                    case mute:
                        console.log('muto');
                        client.commands.get('mute').execute(client, message, args)
                        break;
                        

                }            
                
        });

        }). catch(function(){
            console.log('Errore');
        });
        
    }
}

The method you would want to use is reaction.users.remove(user) . Take a look at the below links. Some of the methods in Discord.Js have changed when Discord.Js v12 came out.

Hopefully, these links help you with what you're trying to do


So in your case you would do something like

client.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.message.partial) await reaction.message.fetch();
    if (reaction.partial) await reaction.fetch();
    if (user.bot) return;
    if (!reaction.message.guild) return;        
    switch (reaction.emoji.name) {
        // CASES IN-BETWEEN.
    }
    reaction.users.remove(user)          
});

You can remove a specific reaction like this:

client.on('messageReactionAdd', async (message) => {
   message.reactions.cache.get('484535447171760141').remove()
      .catch(error => console.error('Failed to remove reactions: ', error));
});

And if you only want to remove the reactions from a user you should do this:

const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(userId));
try {
    for (const reaction of userReactions.values()) {
        await reaction.users.remove(userId);
    }
} catch (error) {
    console.error('Failed to remove reactions.');
}

Keep this in mind:

  • Your bot needs MANAGE_MESSAGES permissions in order to do this!

  • Make sure not to remove reactions by emoji or by user too much; if there are many reactions or users, it can be considered API spam


You can also take a look at the Official guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM