Search code examples
javascriptdiscorddiscord.js

Discord.js: ban user that reacted to a message with emoji


I am working on a discord bot who is supposed to ban members that reacted to a specific message with a specific emote. The file I created for the messageReactionAdd event currently contains the following code:

module.exports = {
    name: 'messageReactionAdd',
    execute(client, reaction, user) {
        const channel = client.channels.cache.find(channel => channel.name === 'test');
        let message = 874736592542105640;
        let emotes = ['kannathinking', '🍎'];
        let roleID = (reaction.emoji.name == emotes[0] ? '874730080486686730' : '874729987310235738')

        if (message == reaction.message.id && (emotes[0] == reaction.emoji.name || emotes[1] == reaction.emoji.name)) {
        user.ban();
        channel.send(`${user} was banned`);
        }
    }
}

However this code doesn't work and throws me the following error:

user.ban() is not a function

After doing some research I found out that the ba command only works on GuildMember objects. Unfortunately those aren't created when messageReactionAdd is called. Does anyone have an idea how to work around it?


Solution

  • You don't have to get the GuildMember object, you can just ban the user by id from GuildMemberManager which can be found from the reaction object via reaction.message.guild.members

    So instead of using user.ban() you can use

    reaction.message.guild.members.ban(user.id)