如何在 Discord.js v13 中使用自动完成功能?

Dod*_*Dev 1 node.js discord discord.js

如何使用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" })
    },
};
Run Code Online (Sandbox Code Playgroud)

Har*_*len 8

基本上,自动完成是机器人为命令交互建议整数和字符串选项值的一种方式。当用户使用斜杠命令(我们称其为帮助)并且该命令采用要描述的命令的字符串参数时,用户可能会很烦人知道每个命令。自动完成通过建议值(在本例中为命令)来修复此问题。

当您使用斜杠命令生成器时,您可以将选项设置为自动竞争:

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)
    );
Run Code Online (Sandbox Code Playgroud)

当自动完成设置为 true 时,每次用户开始使用某个选项时,Discord 都会向您的机器人发送请求(作为交互)以查看应建议的内容。您可以这样处理响应:

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)
    );
Run Code Online (Sandbox Code Playgroud)