首页 > 解决方案 > 命令限制

问题描述

如果它不在某个频道中,我正在尝试使命令不起作用。当我运行我的代码时,我得到这个错误: SyntaxError: Unexpected token '!'

module.exports = {
  name: "kill",
  desciprtion: "idk",

  if (!message.channel.id === '794303555975643136') return;

  const { member } = message;

  member.roles.add('794308638125981726')
}

标签: discorddiscord.js

解决方案


在 JavaScript 和大多数其他语言中,您可以!在函数中将not.

例如,让我们采取message.member.hasPermission(). 如果我们!在开头添加,给我们:

if (!message.member.hasPermission('ADMINISTRATOR') return

我们基本上是在告诉客户,if the message member does **not** have the administrator permission, return the command.

现在,让我们拿你的if statement话说if (!message.channel.id === 'id') return,你基本上是在告诉客户if not message id equals to id return the command,让我们面对这完全没有意义。

当我们想将变量与某个值进行比较时,我们想告诉客户端if the variable is **not** equal to value, return the command。因此,为什么,您应该将 if 语句修改为:

if (message.channel.id !== '794303555975643136') return;

很抱歉回答的很长,感觉就像给某人上了一课:p


推荐阅读