【问题标题】:Bot keeps crashing when wrong command is sent. [Discord JS]发送错误命令时,Bot 不断崩溃。 [不和谐 JS]
【发布时间】:2021-05-15 06:54:04
【问题描述】:

我正在尝试制作 Arby 的机器人。每当有人在频道中发送消息时,机器人就会崩溃并且机器人无法工作。

client.on("message", (message) => {
  if (message.content.startsWith("arbys") || message.guild.channel == message.guild.channels.cache.get("843008525335920651")) {
  } else
    setTimeout(() => message.delete(), 1);
  message.author.send("**arbys** Only Arbys. GOT IT?")
});

如果有人可以帮助我,那就太棒了。

【问题讨论】:

  • 只要检查message.channel.id ==="843008525335920651",检查消息是否在正确的通道中发送。虽然,这个问题似乎有点不清楚。如有错误请附上。
  • message.guild.channel == message.guild.channels.cache.get("843008525335920651") 代表什么?想一想,当你输入else(以及何时不输入)。
  • 崩溃时可能有一些错误信息?
  • message.author.send("**arbys** Only Arbys. GOT IT?") 会导致错误,如果我没记错的话。您需要一个 channel 对象来发送消息,但 author 是一个用户对象。
  • @binoy638,不,这就是您发送直接消息的方式。 OP,请附上错误。

标签: javascript node.js discord discord.js


【解决方案1】:

这是因为message.guild 没有频道属性,而是这样做

if (message.content.startsWith("arbys") || message.channel.id == "843008525335920651"){

希望对你有帮助>:D

【讨论】:

    【解决方案2】:

    message.guild 没有channel 属性,但这不是唯一的问题。您不能像这样将一个对象 (message.guild.channel) 与另一个对象 (message.guild.channels.cache.get("843008525335920651")) 进行比较。但是,您可以检查发送消息的通道是否具有您想要的相同 ID (843008525335920651)。

    您也可以在使用.then() 方法向消息作者发送 DM 后删除该消息。查看下面的代码:

    client.on('message', (message) => {
      if (
        message.content.startsWith('arbys') ||
        message.channel.id === '843008525335920651'
      ) {
        // do something ?
        console.log('Correct channel or command');
      } else {
        // send a DM to the author
        message.author
          .send('**arbys** Only Arbys. GOT IT?')
          // and delete the original message
          .then(() => message.delete())
          // catch them errors :)
          .catch((error) => console.log(error));
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 2018-06-08
      • 2021-02-12
      • 2021-04-06
      • 2021-06-30
      • 1970-01-01
      • 2021-06-23
      • 2021-11-20
      • 2021-07-17
      相关资源
      最近更新 更多