Soulless Rekkr
Soulless Rekkr

Reputation: 125

Discord Bot Reads Reaction to a Set Message

I have my bot reading all messages in all channels and then assigning a role based on there only being the 🦴 emoji and nothing else.

Const Above All Code

const Discord = require('discord.js');
const bot = new Discord.Client();
const Yard = '694204522528243752';

Code works to read messages

bot.on('message', (message) => {
    if (message.content == '🦴') {
        message.member.roles.add(Yard);
    }
});

I am attempting to make the bot listen to a single message in a certain channel, Then depending on the the reaction assigning a role/permission group to the user that placed the reactions.

Trying to have bot read reactions instead

bot.on('Reaction Assign Role', async (reaction, user) => {
    const filter = (reaction) => reaction.emoji.name === '🦴';
    const rules = message.id('694538155336138752')

    await rules.message.id === filter;
        message.member.roles.add(Yard);
});

I am not sure how this is suppose to work. I figure the bot is listening for reaction on a target message. If message gets a reaction and the reaction is equal to the filter emoji then add role to the target user.

Not sure how to make the bot listen for reactions on a message by ID. Once I get that I hope I can figure out the rest.

Upvotes: 2

Views: 4991

Answers (2)

Soulless Rekkr
Soulless Rekkr

Reputation: 125

This is using discord.js v12

const discord = require('discord.js');
const bot = new discord.Client({
    partials: ['MESSAGE','REACTION']
});
const TOKEN = require('./config.json');
const Yard = '<roleID to Set>'
const MessageNumber = '<messageID to Watch>'

bot.login(TOKEN.token)

bot.on('ready', () => {
    console.log(bot.user.tag + " has logged in.");
});

bot.on('messageReactionAdd', async (reaction, user) => {
    console.log("Message Reaction Add Top");

    let applyRole = async () => {
        let emojiName = reaction.emoji.name;
        let role = reaction.message.guild.roles.cache.find;
        let member = reaction.message.guild.members.cache.find(member => member.id == user.id);
        if (role && member) {
            console.log("Role and Member Found");
            await member.roles.add(Yard);
        }
    }
    if (reaction.message.partial) {
        try {
            let msg = await reaction.message.fetch()
            console.log(msg.id);
            if (msg.id === MessageNumber) {
                console.log("Cached - Applied");
                applyRole();
            }
        }
        catch (err) {
            console.log(err);
        }
    }
    else {
        console.log("Not a Partial");
        if (reaction.message.id === MessageNumber) {
            console.log("Not a Partial - applied")
            applyRole();
        }
    }
});

Upvotes: 1

Aci
Aci

Reputation: 566

This is more complicated than it sounds. You need to create a "raw" listener, which essentially tracks all changes in all channels. You can then focus on the reactions on a specific message.

This is how I did it:

const events = {
    MESSAGE_REACTION_ADD: 'messageReactionAdd',
};

//you dont need to modify any of this:
bot.on('raw', async event => {
    if (!events.hasOwnProperty(event.t)) return;

    const { d: data } = event;
    const user = bot.users.get(data.user_id);
    const channel = bot.channels.get(data.channel_id) || await user.createDM();

    if (channel.messages.has(data.message_id)) return;

    const message = await channel.fetchMessage(data.message_id);
    const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
    const reaction = message.reactions.get(emojiKey);
    bot.emit(events[event.t], reaction, user);
})

//change the chnl variable so it gets the channel you want, the server ID for the correct server and the name of the emoji:
bot.on('messageReactionAdd', async (reaction, user) => {
    let chnl= bot.channels.get(`289390221789757440`);
    if(reaction.emoji.name === 'NAMEHERE') {
        let msgserver = bot.guilds.get(`SERVERIDHERE`)
        let usr = await msgserver.fetchMember(user)
        console.log(reaction + ` ` + user)
    }
});

This should log the reaction and user any time someone reacts to it.

Upvotes: 1

Related Questions