发送错误命令时,机器人会不断崩溃。 [不和谐JS]

时间:2021-05-15 06:54:04

标签: javascript node.js discord discord.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?")
});

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

希望这有帮助>:D