【问题标题】:How would I use RETURN in this situation? (discord.js bot)在这种情况下我将如何使用 RETURN? (discord.js 机器人)
【发布时间】:2020-02-22 10:16:31
【问题描述】:

我正在尝试制作一些人们必须付费才能使用一组命令的东西。我把那部分记下来了,但我很确定我在使用 return 函数时遇到了问题。

下面的代码使它发送错误消息,然后立即发送命令输出!如果他们的用户 ID 不在数组中,我希望它只发送错误消息。如果他们的 ID 在数组中,则发送正常的命令用法(使用 return,对吗?)

^^ 这是不在黄金成员数组中的用户所看到的。我想要它,所以我添加的人的 ID 只会显示命令输出。不在该用户 ID 数组中的人将看到错误(哎呀)消息。

var Spark = require("sparkbots")
const Permission = Spark.permission("Golds")
Permission.setLevel(5)
module.exports = Permission;

Permission.code = (client, message) => {
    let golds = ['190916650143318016']
    if(!golds.includes(message.author.id)){
      return message.reply("Whoops! That's a ⭐ GOLD ⭐ user only command. Purchase gold here: https://willm.xyz/dis/gold")

    } else {
      return false }
}```

【问题讨论】:

  • 您还应该共享调用 Permission.code 的函数。

标签: javascript node.js discord discord.js


【解决方案1】:

您需要的是一个For ... of ... 循环。 Here 是一些信息

// here is an example
arr = ['1', '2', '3']
for (let i of arr) {
   console.log(i); // logs 1, 2, 3
}

所以你需要做的就是把你的 if 语句放在一个 for 循环中

let golds = ['ID', 'ID2', 'ID3']
for (let i of golds) {
  if( i === message.author.id)){
    // if in gold
  }
  else {
    // if not in gold
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-21
    • 2015-05-20
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2011-04-16
    • 2016-04-11
    • 1970-01-01
    相关资源
    最近更新 更多