【问题标题】:How to check if a Discord.js snowflake is included in an array of other IDs如何检查 Discord.js 雪花是否包含在其他 ID 数组中
【发布时间】:2019-04-08 02:07:30
【问题描述】:

我目前正在开发一个 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"
]

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      相关资源
      最近更新 更多