Nightshade
Nightshade

Reputation: 23

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.

Upvotes: 1

Views: 312

Answers (1)

Lioness100
Lioness100

Reputation: 8402

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

Upvotes: 1

Related Questions