简体   繁体   中英

Discord.JS Reactions

I'm working on a ticket bot and I would like it so that you react to a message to create a ticket. I can make the message but when I use bot.on('messageReactionAdd') if my bot restarts it won't detect that a reaction has been added unless you make a new message and react to a message that has been created since the bot has been online. This is a problem for me and I know it's fixable. I've tried google searches and couldn't fix the problem. Can anyone here help me?

Starting from Discord.js v12, you can enable partials on the bot that allow it to send events for unfetched messages, with the tradeoff that you have to fetch the message yourself at the start of the handler:

const Discord = require('discord.js');
// Make sure you instantiate the client with the correct settings
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
client.on('messageReactionAdd', async (reaction, user) => {
    // When we receive a reaction we check if the reaction is partial or not
    if (reaction.partial) {
        // If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
        try {
            await reaction.fetch();
        } catch (error) {
            console.error('Something went wrong when fetching the message: ', error);
            // Return as `reaction.message.author` may be undefined/null
            return;
        }
    }
    // Now the message has been cached and is fully available
    console.log(`${reaction.message.author}'s message "${reaction.message.content}" gained a reaction!`);
    // The reaction is now also fully available and the properties will be reflected accurately:
    console.log(`${reaction.count} user(s) have given the same reaction to this message!`);
});

You can use this however you want, but here is an example for you:

message.channel.send("React test!").then(messageReaction => {
    messageReaction.react("✅");
    messageReaction.react("⛔");
  });

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