【问题标题】:msg isn't defined | discord.js | nodejs味精未定义 |不和谐.js |节点
【发布时间】:2020-11-24 17:19:27
【问题描述】:

在我在下面发布的命令中,我让机器人用两个表情符号对发送到某个频道的消息做出反应。但是由于某种原因,当我将消息指定为 msg 时,它说它没有定义,有什么帮助吗?

我尝试同时使用 msg 和 message,但对于 msg,它显示一个错误,指出它未定义,但对于 message,它只是对我发送的运行命令的原始消息作出反应。

谢谢,布鲁。

代码:

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"
};

【问题讨论】:

    标签: javascript node.js discord bots discord.js


    【解决方案1】:

    在您的代码的以下部分中,msg 不存在,message 指的是原始消息:

    .send(sellingembed)
    .then(succ => {
      collectedMessage.reply(
        "Sent selling message for review."
      );
    
      msg.react(emojis[0]);
      msg.react(emojis[1]);
    

    发送的消息存储在succ变量中,所以如果要对发送的消息做出反应,需要使用succ.react()

    您还需要使用asyncawait 来确保反应按顺序进行。欲了解更多信息,请阅读reactions section of the Discord.js guide

    这是修改后的代码:

    .send(sellingembed)
    .then(async succ => {
      collectedMessage.reply(
        "Sent selling message for review."
      );
    
      await msg.react(emojis[0]);
      await msg.react(emojis[1]);
    

    【讨论】:

    • 哦,是的!非常感谢。我完全忘记了!
    猜你喜欢
    • 2020-11-07
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 2018-03-01
    • 2017-04-26
    相关资源
    最近更新 更多