我试着用斜杠命令中的子命令来对付一个不和谐的机器人。将会有许多子命令,每个子命令都与另一个相似,所以我想使用一个在.addSubCommand中调用的函数,但显然它并不能正常工作。
这是代码:
async function subCommandDB(subcommand,nameDB,name) {
subcommand = Object();
subcommand.setName(name)
.setDescription('Add record to specified database')
for (i=0;i<nameDB.properties.length;i++) {
subcommand.addStringOption(option=>
option.setName(nameDB.properties[i])
.setDescription('///')
.setRequired(true))
}
return subcommand
}
module.exports = {
data: new SlashCommandBuilder()
.setName('dbad')
.setDescription('ADMIN ONLY. Add a record to the database of choice')
.addSubcommand(subCommandDB(subcommand,dbs.itemDB,'item')),
async execute(interaction) {
// whatever . . .
},这是一个错误:
C:\users\me\Documents\Discord Bot\commands\dbad.js:24
.addSubcommand(subCommandDB(subcommand,dbs.itemDB,'item')),
^
ReferenceError: subcommand is not defined
at Object.<anonymous> (C:\Users\me\Documents\Discord Bot\commands\dbad.js:24:31)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Users\me\Documents\Discord Bot\deploy-commands.js:11:18)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)如果我把它放在.addSubcommand()中,那么整个过程就可以工作了:
.addSubcommand(subcommand => {
subcommand.setName('item')
.setDescription('Add record to Item database')
for (i=0;i<dbs.itemDB.properties.length;i++) {
subcommand.addStringOption(option=>
option.setName(dbs.itemDB.properties[i])
.setDescription('a')
.setRequired(true))
}
return subcommand
}),这段代码就是我希望转换成的函数。非常感谢你的帮助!
发布于 2022-11-26 23:33:15
这是工作的代码:
function subCommandDB(subcommand,nameDB,name) {
subcommand.setName(name)
.setDescription('Add record to specified database')
for (i=0;i<nameDB.properties.length;i++) {
subcommand.addStringOption(option=>
option.setName(nameDB.properties[i])
.setDescription('///')
.setRequired(true))
}
return subcommand
}
module.exports = {
data: new SlashCommandBuilder()
.setName('dbad')
.setDescription('ADMIN ONLY. Add a record to the database of choice')
.addSubcommand(subcommand => subCommandDB(subcommand,dbs.itemDB,'item')),
async execute(interaction) {
// whatever . . .
},主要问题是异步函数返回了一个承诺,而.addSubcommand(.)需要一个函数: SlashCommandSubcommandBuilder --这意味着需要一个常规函数而不是异步函数。
https://stackoverflow.com/questions/74578190
复制相似问题