【问题标题】:copying a message to another channel将消息复制到另一个频道
【发布时间】:2021-07-27 17:18:01
【问题描述】:

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

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

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

【问题讨论】:

  • 你觉得这个检查什么? if (message.content = 1-10000)
  • 用户输入的数字1-10000? @ZsoltMeszaros
  • 嘿@asdddd 我试图在下面为您提供答案。你有机会去看看吗?有意义吗?如果回答有用,请点击其左侧的点赞按钮(▲)。如果它回答了您的问题,请单击复选标记 (✓) 接受它。这样别人就知道你得到了帮助。另见What should I do when someone answers my question?

标签: javascript discord.js


【解决方案1】:

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

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

查看下面的 sn-p:

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`);
        }
      });
  }
});

【讨论】: