aakhilv
aakhilv

Reputation: 145

msg isn't defined | discord.js | nodejs

In the command I've posted below, I have the bot react with two emojis to the message that is sent to a certain channel. But for some reason when I specify the message as msg, it says it's not defined, any help?

I tried using both msg and message but for msg it showed an error saying that it isn't defined, but for message, it just reacted to the original message I sent to run the command.

Thanks, Blu.

Code:

const Discord = require("discord.js");
let TicketChannelID = "729912166337216575";
let GuildID = "710634001513185371";
const talkedRecen = new Set();

module.exports.run = async (bot, message, args) => {
  let avatar = message.author.avatarURL({
    size: 2048
  });
  const filter = m => m.author.id === message.author.id;
  let description = "";
  let price = "";
  const emojis = ["715383579059945512", "715383579059683349"];

  if (talkedRecen.has(message.author.id)) {
    message.reply(
      "please wait till your two hours are up before you type this again."
    );
  } else {
    let Prompt1 = new Discord.MessageEmbed()
      .setTitle("Description")
      .setDescription(
        "Please specify a description for the asset you are selling below."
      )
      .setColor("#8c52ff")
      .setTimestamp();

    let Prompt2 = new Discord.MessageEmbed()
      .setTitle("Price")
      .setDescription(
        "Please specify the price for the asset you are selling below."
      )
      .setColor("#8c52ff")
      .setTimestamp();

    message.channel.send("Redirected prompt to your DMs. If you didn't receive a DM from me, check your privacy settings and try again.");

    message.author.send(Prompt1).then(p1 => {

      let Description = new Discord.MessageCollector(p1.channel, filter, {
        max: 1,
        time: 60000
      });

      Description.on("collect", async newMS => {
        let collectedMessage = newMS.content.toLowerCase();
        if (collectedMessage.length >= 2048) {
          collectedMessage.reply(
            "The description you set for your asset is too long. Please try running the command again."
          );
          Description.stop();
          return;
        }
        Description.stop();
        description = collectedMessage;

        p1.channel
          .send(Prompt2)
          .then(p2 => {

            let Price = new Discord.MessageCollector(p2.channel, filter, {
              max: 1,
              time: 1200000
            });

            Price.on("collect", collectedMessage => {
              if (collectedMessage.length >= 2048) {
                collectedMessage.reply(
                  "The price you set for your asset is too long. Please try running the command again."
                );
                Price.stop();
                return;
              }
              Price.stop();
              price = collectedMessage.content;

              let Prompt3 = new Discord.MessageEmbed()
                .setTitle("Do you wish to send this for review?")
                .setAuthor(message.author.tag, avatar)
                .setColor("#8c52ff")
                .addField("Description", description)
                .addField("Price", price)
                .addField("Contact", `<:discord:715391744090308700> - <@${message.author.id}>`)
                .setFooter("Please respond with yes or no.")
                .setTimestamp();

              message.author.send(Prompt3).then(ex => {
                let confirmationListener = new Discord.MessageCollector(
                  ex.channel,
                  filter, {
                    max: 1,
                    timer: 100000
                  }
                );
                confirmationListener.on("collect", collectedMessage => {
                  confirmationListener.stop();
                  let Message1 = collectedMessage.content.toLowerCase();
                  if (Message1 === "yes") {

                    let sellingembed = new Discord.MessageEmbed()
                      .setAuthor(message.author.tag, avatar)
                      .setColor("#8c52ff")
                      .addField("Description", description)
                      .addField("Price", price)
                      .addField("Contact", `<:discord:715391744090308700> - <@${message.author.id}>`)
                      .setTimestamp();

                    bot.guilds.cache
                      .get(GuildID)
                      .channels.cache.get(TicketChannelID)
                      .send(sellingembed)
                      .then(succ => {
                        collectedMessage.reply(
                          "Sent selling message for review."
                        );

                        msg.react(emojis[0]);
                        msg.react(emojis[1]);

                        const filter = (reaction, user) => emojis.includes(reaction.emoji.id) && user.id != bot.user.id;
                        const options = {
                          errors: ["time"],
                          time: 86400000,
                          max: 1
                        };
                        msg.awaitReactions(filter, options)
                          .then(collected => {
                            const first = collected.first();
                            if (emojis.indexOf(first.emoji.id) === 0) {
                              msg.delete();
                              let certainChannel = bot.channels.cache.get("715758259713212448");

                              certainChannel.send(sellingembed);
                              collectedMessage.reply("your selling message has been approved.");
                            } else {
                              msg.delete();
                              collectedMessage.reply("your selling message has been declined.");
                            }
                          })
                          .catch(err => {
                            console.log(err)
                          });
                      })
                      .catch(err => {
                        collectedMessage.reply("Error:\n" + err);
                      });
                  }
                });
                confirmationListener.on("end", (col, res) => {
                  if (!col.size && res == "time") {
                    return ex.reply(
                      "Timed out, try to repond within the given time."
                    );
                  }
                });
              });
            });
            Price.on("end", (col, res) => {
              if (!col && res == "time") {
                return message.reply(
                  "Timed out, try to respond within the given time."
                );
              }
            });
          })
          .catch(err => {
            p1.reply("Error: " + err);
          });

        Description.on("end", (col, res) => {
          if (!col.size && res == "time") {
            return p1.reply("Timed out, try to respond within the given time.");
          }
        });
      });
    });

    talkedRecen.add(message.author.id);
    setTimeout(() => {
      talkedRecen.delete(message.author.id);
    }, 7200000);
  }
};
module.exports.help = {
  name: "selling"
};

Upvotes: 0

Views: 150

Answers (1)

Daemon Beast
Daemon Beast

Reputation: 2909

In the following section of your code, msg does not exist and message refers to the original message:

.send(sellingembed)
.then(succ => {
  collectedMessage.reply(
    "Sent selling message for review."
  );

  msg.react(emojis[0]);
  msg.react(emojis[1]);

The sent message is stored in the succ variable, so if you want to react to the sent message, you need to use succ.react().

You also need to use async and await to ensure that the reactions happen in order. For more information, please read the reactions section of the Discord.js guide.

Here is the modified code:

.send(sellingembed)
.then(async succ => {
  collectedMessage.reply(
    "Sent selling message for review."
  );

  await msg.react(emojis[0]);
  await msg.react(emojis[1]);

Upvotes: 1

Related Questions