首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.js斜线(子)命令(希望)使用函数- ReferenceError:子命令未定义

Discord.js斜线(子)命令(希望)使用函数- ReferenceError:子命令未定义
EN

Stack Overflow用户
提问于 2022-11-25 22:06:03
回答 1查看 44关注 0票数 -1

我试着用斜杠命令中的子命令来对付一个不和谐的机器人。将会有许多子命令,每个子命令都与另一个相似,所以我想使用一个在.addSubCommand中调用的函数,但显然它并不能正常工作。

这是代码:

代码语言:javascript
复制
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 . . .
    },

这是一个错误:

代码语言:javascript
复制
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()中,那么整个过程就可以工作了:

代码语言:javascript
复制
    .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
        }),

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

EN

回答 1

Stack Overflow用户

发布于 2022-11-26 23:33:15

这是工作的代码:

代码语言:javascript
复制
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 --这意味着需要一个常规函数而不是异步函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74578190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档