Discord.js自动完成不显示选项



我使用Discord.js v14来实现我的Discord bot的自动完成功能。我主要遵循以下指南:

https://discordjs.guide/slash-commands/autocomplete.html发送结果

对于/character命令触发自动完成,但无论我键入什么,都没有选择(我在代码中编写了2个选择,遵循指南)。

其他不使用自动完成的命令可以正常工作。

bot具有以下权限:读取消息/查看通道,发送消息,嵌入链接,附加文件和使用斜杠命令。

命令character.js(执行部分目前只是占位符):

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { ref, child, get } = require('firebase/database');
const { db, dbref } = require('..');
module.exports = {
data: new SlashCommandBuilder()
.setName('character')
.setDescription('Choose a Character')
.addStringOption(option =>
option.setName('character-name')
.setDescription('Name of the Character')
.setAutocomplete(true)),
async autocomplete(interaction) {
const focusedValue = interaction.options.getFocused();
const choices = ['option', 'choice'];
const filtered = choices.filter(choice => choice.startsWith(focusedValue));
await interaction.respond(
filtered.map((choice) => ({ name: choice, value: choice })),
)
},
async execute(interaction) {
await interaction.reply(interaction.options.getString("character-name"));
},
};

File:interactionCreate.js

const { Events, InteractionType } = require('discord.js');
module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;
if(interaction.type == InteractionType.ApplicationCommand){
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction);
} catch (error) {
console.error(`Error executing ${interaction.commandName}`);
console.error(error);
}
}
else if(interaction.isAutocomplete()){
const command = interaction.client.commands.get(interaction.commandName);
if(!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try{
await command.autocomplete(interaction);
} catch(error) {
console.error(error);
}
}
},
};

如果你需要更多的信息,请在评论中留言。我不确定是什么问题,所以我不知道你可能需要的信息。

我也试过了:

const { Events, InteractionType } = require('discord.js');
module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;

if(interaction.type == InteractionType.ApplicationCommand){
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction);
} catch (error) {
console.error(`Error executing ${interaction.commandName}`);
console.error(error);
}

if(interaction.isAutocomplete()){

if(!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try{
await command.autocomplete(interaction);
} catch(error) {
console.error(error);
}
}
}


},
};

顶部有一个typeguard,防止你响应自动完成交互

if (!interaction.isChatInputCommand()) return;

你应该删除这个,这样它就可以运行非聊天输入命令的交互。

最新更新