首页 > 解决方案 > 慢模式命令总是发送相同的东西

问题描述

我正在尝试让我的慢模式命令正常工作。基本上,当我输入 >sm 2s 时,它会回复“请按照此示例进行操作:;sm 5”<- 如果 args 为空,则该回复应该只发送。


  if(args[1] == null) { 
       return message.channel.send("Please follow this example: ;sm 5")
   }if (args[1] !== null) {
    message.channel.setRateLimitPerUser(args[0])
    message.channel.send(`Slowmode is now ${args[0]}s`)
}}

module.exports.config = {
    name: "sm",
    aliases: []
}```

标签: node.jsdiscord.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 语句,说if (!args[1] == null) {,你基本上是在告诉客户if not args[1] equals to null do this:,让我们面对它完全没有意义。

当我们想将变量与某个值进行比较时,我们想告诉客户端if args[1] is **not** equal to null, do this.因此为什么你应该将你的 if 语句修改为:

if (args[1] !== null) {

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


推荐阅读