javascript 要使用函数- ReferenceError的Discord.js斜杠(子)命令:未定义子命令

9gm1akwq  于 2022-11-27  发布在  Java
关注(0)|答案(1)|浏览(150)

我正在尝试在discord bot的斜杠命令中使用子命令。会有很多子命令,每个子命令都很相似,所以我想使用.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
        }),

这段代码就是我希望转换成那个函数的代码。非常感谢你的帮助!

x8goxv8g

x8goxv8g1#

这是一个很好的例子。

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(...)需要一个[Function:SlashCommandSubcommandBuilder] -这意味着需要一个常规函数而不是异步函数

相关问题