如何向命令添加多个名称?
How to add more then one names to a command?
如果我向“名称”添加多于一个 属性,则它不会检测到任何内容。
这个有效:
module.exports = {
name: "info",
description: "Gives full details of a user.",
async execute(...) {
...
}
}
但是如果我尝试向“名称”添加诸如“whois”之类的内容:
module.exports = {
name: ["info", "whois"],
description: "Gives full details of a user.",
async execute(...) {
...
}
}
然后命令就停止工作了。
这是我的 index.js 的代码:
const infoCommandModule = require(`./BotCommands/Info.js`);
if (msg.content.toLowerCase() === `${infoCommandModule.name}`) {
infoCommandModule.execute(...)
}
这是因为当它是一个数组时,你不能将它与字符串进行比较。您可以使用 Array#includes()
来检查给定的字符串是否包含在数组中:
const infoCommandModule = require('./BotCommands/Info.js')
if (infoCommandModule.name.includes(msg.content.toLowerCase)) {
infoCommandModule.execute(...)
}
请注意,您需要为命令中的每个 name
使用数组。如果您也想接受数组和字符串,则需要检查 name
是否为数组。您可以为此使用 Array.isArray(name)
。
如果我向“名称”添加多于一个 属性,则它不会检测到任何内容。
这个有效:
module.exports = {
name: "info",
description: "Gives full details of a user.",
async execute(...) {
...
}
}
但是如果我尝试向“名称”添加诸如“whois”之类的内容:
module.exports = {
name: ["info", "whois"],
description: "Gives full details of a user.",
async execute(...) {
...
}
}
然后命令就停止工作了。
这是我的 index.js 的代码:
const infoCommandModule = require(`./BotCommands/Info.js`);
if (msg.content.toLowerCase() === `${infoCommandModule.name}`) {
infoCommandModule.execute(...)
}
这是因为当它是一个数组时,你不能将它与字符串进行比较。您可以使用 Array#includes()
来检查给定的字符串是否包含在数组中:
const infoCommandModule = require('./BotCommands/Info.js')
if (infoCommandModule.name.includes(msg.content.toLowerCase)) {
infoCommandModule.execute(...)
}
请注意,您需要为命令中的每个 name
使用数组。如果您也想接受数组和字符串,则需要检查 name
是否为数组。您可以为此使用 Array.isArray(name)
。