如何在导入的JSON数据中使用.includes方法判断指定值是否存在
问题原因
includes()是数组的原型方法,你配置里的config.developer是对象结构,不支持直接调用该方法,所以逻辑执行报错。
修复方案
只需要修改权限判断那一行逻辑,先提取developer对象的所有值转为数组,再调用includes()匹配即可:
// 原有逻辑保留,仅修改判断行 const config = require("../../config.json"); const devsId = config.developer; // 提取对象所有开发者ID值转为数组,再判断是否包含当前用户ID if(!Object.values(devsId).includes(message.author.id)) return; // 后续eval执行逻辑保持不变 try { const code = args.join(" "); let evaled = eval(code); if (typeof evaled !== "string") evaled = require("util").inspect(evaled); message.channel.send(`\`\`\`xl\n${clean(evaled)}\`\`\``); } catch (err) { message.channel.send(`\`ERROR\` \`\`\`xl\n${clean(err)}\n\`\`\``); }
可选兼容处理
如果存在message.author.id返回值为数字类型、和配置里的字符串ID类型不匹配的场景,可以做一层类型统一:
if(!Object.values(devsId).includes(message.author.id.toString())) return;
内容的提问来源于stack exchange,提问作者VK_
相关产品推荐
相关产品推荐

