You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js v14自动补全无选项显示问题求助

排查Discord.js v14自动补全无选项显示问题的步骤

1. 检查命令参数的自动补全开关

确保character.js的命令定义中,目标参数已开启自动补全配置:

// character.js 命令参数示例
data: new SlashCommandBuilder()
    .setName('character')
    .setDescription('角色相关命令')
    .addStringOption(option =>
        option.setName('target')
            .setDescription('选择目标角色')
            .setAutocomplete(true) // 必须启用此配置才会触发自动补全事件
    ),

如果遗漏setAutocomplete(true),Discord不会向机器人发送自动补全请求。

2. 验证interactionCreate事件的处理逻辑

无论采用哪种写法,必须确保正确监听autocomplete类型交互,并精准匹配character命令:

写法一:直接在事件中处理

// interactionCreate.js
client.on('interactionCreate', async interaction => {
    // 过滤非自动补全交互
    if (!interaction.isAutocomplete()) return;

    // 匹配目标命令
    if (interaction.commandName === 'character') {
        const focusedInput = interaction.options.getFocused();
        // 预设的补全选项
        const availableChoices = ['角色A', '角色B'];
        // 根据输入过滤选项(可根据需求调整匹配逻辑)
        const filteredChoices = availableChoices.filter(choice => 
            choice.toLowerCase().includes(focusedInput.toLowerCase())
        );
        // 返回补全结果
        await interaction.respond(
            filteredChoices.map(choice => ({ name: choice, value: choice }))
        );
    }
});

写法二:命令文件拆分模式

如果使用命令文件结构,需在character.js中导出autocomplete方法:

// character.js
module.exports = {
    data: new SlashCommandBuilder(/* 命令定义 */),
    async execute(interaction) { /* 执行逻辑占位符 */ },
    async autocomplete(interaction) {
        const focusedInput = interaction.options.getFocused();
        const availableChoices = ['角色A', '角色B'];
        const filteredChoices = availableChoices.filter(choice => 
            choice.toLowerCase().includes(focusedInput.toLowerCase())
        );
        await interaction.respond(filteredChoices.map(c => ({ name: c, value: c })));
    }
};

然后在interactionCreate.js中调用该方法:

// interactionCreate.js
client.on('interactionCreate', async interaction => {
    if (interaction.isAutocomplete()) {
        const command = client.commands.get(interaction.commandName);
        if (!command) return;
        try {
            await command.autocomplete(interaction);
        } catch (err) {
            console.error('自动补全出错:', err);
        }
        return;
    }
    // 其他交互处理逻辑...
});

3. 确认命令同步状态

修改命令的自动补全配置后,必须重新同步命令到Discord:

  • 运行你的命令部署脚本,确保新的配置被推送到Discord服务器
  • 检查同步时使用的机器人Token、应用ID是否正确,避免同步到错误的机器人实例

4. 排查错误日志与权限

  • 查看机器人的控制台日志,确认interaction.respond()是否抛出异常(如异步错误未捕获会导致补全无响应)
  • 确认机器人在服务器中的角色优先级足够高,没有被服务器权限设置拦截自动补全请求

5. 简化测试定位问题

先使用最简化的补全逻辑测试,排除过滤逻辑的影响:

async autocomplete(interaction) {
    // 直接返回预设选项,不做过滤
    await interaction.respond([
        { name: '测试角色1', value: 'role1' },
        { name: '测试角色2', value: 'role2' }
    ]);
}

如果此代码能显示选项,说明问题出在过滤逻辑;如果仍无显示,需重新检查命令定义或事件监听逻辑。


内容的提问来源于stack exchange,提问作者LosmiNCL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 05:16:36