javascript - 预期收到 SlashCommandChannelOption 构建器,但未定义

标签 javascript node.js discord discord.js bots

在使用 Discord.js v13 创建命令时,我遇到了一个我似乎无法弄清楚的有趣错误。

如果我使用命令处理系统,并通过不久前为开发人员应用程序创建的 应用程序命令 功能 Discord 创建带有 channel 输入字段的命令,我会运行运行 Node.js 应用程序时出现以下错误:

F:\Development\eco-bot\node_modules\@discordjs\builders\dist\interactions\slashCommands\Assertions.js:44
        throw new TypeError(`Expected to receive a ${instanceName} builder, got ${input === null ? 'null' : 'undefined'} instead.`);
        ^

TypeError: Expected to receive a SlashCommandChannelOption builder, got undefined instead.
    at Object.assertReturnOfBuilder (F:\Development\eco-bot\node_modules\@discordjs\builders\dist\interactions\slashCommands\Assertions.js:44:15)
    at MixedClass._sharedAddOptionMethod (F:\Development\eco-bot\node_modules\@discordjs\builders\dist\interactions\slashCommands\mixins\CommandOptions.js:76:22)
    at MixedClass.addChannelOption (F:\Development\eco-bot\node_modules\@discordjs\builders\dist\interactions\slashCommands\mixins\CommandOptions.js:40:21)
    at Object.<anonymous> (F:\Development\eco-bot\commands\faq.js:7:10)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153: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:94:18)

在提出任何相关问题之前,是的,所有其他命令都已正确加载并发送到 Discord 的应用程序命令 API,并且 Discord 会毫无错误地注册它们。

这是产生问题的代码:

const { SlashCommandBuilder } = require('@discordjs/builders')

module.exports = {
    data: new SlashCommandBuilder()
        .setName("faq")
        .setDescription("Send a FAQ question and answer in selected channel")
        .addChannelOption(channel => {
            channel
                .setName("channel")
                .setDescription("Channel you want to send the FAQ embed in")
                .setRequired(true)
        }),
    async execute (interaction) {
        await interaction.reply("Pong! :ping_pong:")
    }
}

最佳答案

您作为参数传递到 .addChannelOption() 方法的函数预计会返回 SlashCommandChannelOption 的实例,在您的情况下是 channel.因此请确保返回 channel

enter image description here

const { SlashCommandBuilder } = require('@discordjs/builders')

module.exports = {
    data: new SlashCommandBuilder()
        .setName("faq")
        .setDescription("Send a FAQ question and answer in selected channel")
        .addChannelOption(channel => {
            return channel // Add return here
                .setName("channel")
                .setDescription("Channel you want to send the FAQ embed in")
                .setRequired(true)
        }),
    async execute (interaction) {
        await interaction.reply("Pong! :ping_pong:")
    }
}

关于javascript - 预期收到 SlashCommandChannelOption 构建器,但未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68807171/

相关文章:

javascript - 在 jQuery/JavaScript 中使用循环和 promise 在另一个函数之后运行一个函数

javascript - 为什么数组切片将javascript参数转换为数组

javascript - 如何使用 Jquery 或 javascript 清除缓存?

javascript - 如何使用 AWS Cognito SDK 使用 NodeJS 对来自 REST 服务的用户进行身份验证?

youtube - 使用 YouTube API 搜索 channel 返回错误结果

discord - 使用相同的 token 同时运行单独的程序

javascript - 在 Angularjs 中更改 JSON 请求和响应中的字段/属性名称

node.js - 查找字符串并删除行 - Node.JS

node.js - 无法读取 finalHandler Node 模块中未定义的属性 '_header'

javascript - 如何在 Math.floor 中使用变量