简体   繁体   中英

Issues with messageid and putting reaction collectors on proper message in discord.js

I'm attempting to make an embed in discord that updates when a reaction is pressed on said embed. this is what I have attempted so far:

bot.on('message', message => {
    //  if (message.author.bot) return;
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    let msg = message.content.toLowerCase();

    if (msg === 'test2') {
        message.channel.send(embed)
         .then(async embedmsg => 
        await embedmsg.react('⚔️')).then(() => {
            let collector = embedmsg.createReactionCollector(() => {
                return true;
            }, {
                dispose: true,
            });
            collector.on('collect', (reaction, user) => {
                let emojiName = reaction.emoji.name;
                if (user.id == bot.user.id) return;
                if (emojiName != '⚔️') return;
                console.log('here')
                embedmsg.edit(newEmbed1)

everything seems to work, but I'm receiving the error msg

(node:58428) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'createReactionCollector' of undefined

and yes I have declared var embedmsg. If I change the line:

let collector = embedmsg.createReactionCollector(() => { 

to:

let collector = message.createReactionCollector(() => {

it only runs the collector on the original 'test2' message, which cannot be edited due to the fact that it was not sent by the bot. Any tips would be much appreciated. It is probably a super easy fix due to the fact that I haven't coded in a very long time.

embedmsg is no longer defined because you closed the .then() method.

 message.channel.send(embed)
         .then(async embedmsg => 
        await embedmsg.react('⚔️'))
//                                ^^
// since you ended the callback, embedmsg is no longer defined.
             .then(() => {

Instead, use:

message.channel.send(embed).then(async (embedmsg) => {
  await embedmsg.react("⚔️").then(() => {
    let collector = embedmsg.createReactionCollector(
      () => {
        return true;
      },
      {
        dispose: true,
      }
    );
  });
}); // close the callback down here instead

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