【问题标题】:How to use AutoComplete in Discord.js v13?如何在 Discord.js v13 中使用 AutoComplete?
【发布时间】:2022-03-24 12:48:30
【问题描述】:
您如何通过 Discord.js v13.6、@Discord.js/builders v0.11.0 使用 Discord 自动完成功能?
我在任何地方都找不到指南或教程
这是我目前所拥有的:
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('autocomplete')
.setDescription('Test command')
async execute(interaction) {
await interaction.replay({ content: "Hello World" })
},
};
【问题讨论】:
标签:
node.js
discord
discord.js
【解决方案1】:
基本上,自动完成是机器人为命令交互建议整数和字符串选项值的一种方式。
当用户使用斜杠命令(让我们称之为帮助)并且该命令接受要描述的命令的字符串参数时,用户知道每个命令可能会很烦人。自动完成通过建议值(在本例中为命令)来解决此问题。
您可以在使用斜杠命令生成器时将选项设置为可自动竞争:
import { SlashCommandBuilder } from '@discordjs/builders';
const command = new SlashCommandBuilder()
.setName('help')
.setDescription('command help')
.addStringOption((option) =>
option
.setName('command')
.setDescription('The command to get help for.')
.setRequired(true)
// Enable autocomplete using the `setAutocomplete` method
.setAutocomplete(true)
);
当自动完成设置为 true 时,每次用户开始使用某个选项时,Discord 都会向您的机器人发送请求(作为交互)以查看应该建议的内容。您可以这样处理响应:
client.on('interactionCreate', async (interaction) => {
// check if the interaction is a request for autocomplete
if (interaction.isAutocomplete()) {
// respond to the request
interaction.respond([
{
// What is shown to the user
name: 'Command Help',
// What is actually used as the option.
value: 'help'
}
]);
}
});