我正在使用discord.js v14创建一个不一致机器人,并且遇到了自动完成功能的问题.我完全按照命令中的discord.js documentation执行,没有出现错误,但当我try 使用该命令时,它显示‘加载选项失败’:
以下是我的代码:
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('test')
.setDescription('testing')
.addStringOption(option =>
option.setName('option')
.setDescription('The option to select')
.setRequired(true)
.setAutocomplete(true)),
async autocomplete(interaction) {
const focusedValue = interaction.options.getFocused();
const choices = ['Apples' , 'Oranges' , 'Bananas' , 'Grapes' , 'Watermelons'];
const filtered = choices.filter(choice => choice.startsWith(focusedValue));
await interaction.respond(
filtered.map(choice => ({ name: choice, value: choice })),
);
},
};
如果需要,下面是我的index.js文件:
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits, ActivityType } = require('discord.js');
const { token, clientId, guildId } = require('./config.json');
let slashcmdExec = require("./deploy-slashcmds.js")
slashcmdExec.deployCommands()
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
client.once("ready", () => {
console.log('Ready!');
client.user.setPresence({
activities: [{ name: `over Everything...`, type: ActivityType.Watching }],
status: 'idle',
});
});
client.on(Events.InteractionCreate, async interaction => {
const command = client.commands.get(interaction.commandName);
if (!command) return console.log("Command was not found");
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(token);
我看到一个类似的帖子,说要删除if (!interaction.isChatInputCommand()) return;
,我照做了,但错误仍然存在.