如何检查Discord.js雪花是否包含在其他ID数组中

时间:2018-11-04 15:00:17

标签: javascript node.js if-statement discord.js

我目前正在使用Discord.js机器人,并且正在尝试编写一个系统,其中只有JSON中的给定ID才能执行命令。我不知道如何在if语句中查询它。

if (message.author.id === config.admin) {
  if (cmd === 'mode') {
    let mode = args[0];
    if (mode === 'maintenance') {
      bot.user.setActivity(config.maintenanceGame);
      config.mode = 'maintenance';
      const embed = new Discord.RichEmbed()
        .setAuthor("Gorded Media Ltd. - Maintenance", 'http://imageurl/logo.png')
        .setColor(color.red)
        .setDescription("The mode was set to `Maintenance`. All users can't use the commands in this mode.");
      message.channel.send(embed);
    }
    if (mode === 'online') {
      bot.user.setActivity(config.defaultGame);
      config.mode = 'online';
      const embed = new Discord.RichEmbed()
        .setAuthor("Gorded Media Ltd. - Maintenance", 'http://imageurl/logo.png')
        .setColor(color.lime)
        .setDescription("The mode was set to `Online`. All users can use the commands in this mode.");
      message.channel.send(embed);
    }
    console.log(`[` + date + `] ` + config.consoleprefix + ` User ${message.author.tag} set the mode to ${config.mode}`);
  }
} else {
  message.channel.send(no_perms)
}

这是config中解析的JSON文件的结构:

"admin": [
  "Discord Client ID",
  "Discord Client ID",
  "Discord Client ID"
]

1 个答案:

答案 0 :(得分:0)

您不能检查message.author.id是否等于config.admin,因为那是一个数组。
但是,您可以使用Array.includes()检查该数组是否包含ID:

if (config.admin.includes(message.author.id)) {...}