将消息复制到另一个频道

时间:2021-07-25 08:52:10

标签: javascript discord.js

我希望机器人复制用户在输入消息后发送的号码,这是我的代码

client.on('message', (message) => {
  // Command handler, seen previously
  if (message.content === '#مسعف') {
    message.channel
      .awaitMessages((m) => m.author.id == message.author.id, { max: 1 })
      .then((collected) => {
        // only accept messages by the user who sent the command
        // accept only 1 message, and return the promise after 30000ms = 30s

        // first (and, in this case, only) message of the collection
        if (message.content = 1 - 10000) {
          message.channel.send(message.content);
        }
      });
  }
});

我需要机器人发送用户输入的号码

1 个答案:

答案 0 :(得分:3)

if (message.content = 1 - 10000) 存在问题。首先,使用单个等号 (=) 进行赋值。您使用该代码实现的是将 message.content 的值更改为 -9,999。如果您想检查某物的值,则需要使用双等号 (==) 或三重等号 (===)。另外,如果你想检查一个数字是否在两个数字之间,你需要检查这个数字是否大于较小的数字,是否小于较大的数字。

另一个问题是您检查的是原始 message 的内容,而不是您在消息收集器中收集的内容。

查看下面的代码片段:

client.on('message', (message) => {
  if (message.content === '#مسعف') {
    message.channel
      .awaitMessages((m) => m.author.id === message.author.id, { max: 1 })
      .then((collected) => {
        // first (and, in this case, only) message of the collection
        const response = collected.first();

        // check if it's a number
        if (isNaN(response.content))
          return message.channel.send(`${response.content} is not a number`);

        const num = parseInt(response.content);
        // check if num is between 1 and 10,000 (inclusive)
        if (num >= 1 && num <= 10000) {
          message.channel.send(num);
        } else {
          message.channel.send(`Nah, ${num} is not a valid number`);
        }
      });
  }
});

enter image description here