首页 > 解决方案 > 发送错误命令时,Bot 不断崩溃。[不和谐 JS]

问题描述

我正在尝试制作 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?")
});

如果有人可以请帮助我,那将是惊人的。

标签: javascriptnode.jsdiscorddiscord.js

解决方案


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

推荐阅读