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

如何让Discord.js中Slash命令的字符串选项直接显示输入框?

解决Discord机器人命令直接输入内容的问题

要实现输入/test后直接输入内容,无需点击type选项,只需将该字符串选项设置为必填即可。Discord的斜杠命令规则是:必填选项会自动嵌入命令输入流,用户输入命令后可直接输入选项内容,不用手动点击选择。

修改后的代码如下:

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('Select a test to use.')
        .addStringOption(option =>
            option
                .setName('type')
                .setDescription('Type of test requested')
                .setRequired(true)), // 关键:添加此行设置选项为必填

    async execute(interaction) {
        const ptypes = interaction.options.getString('type') ?? 'No reason provided';
        await interaction.reply(`You chose ${ptypes}`);
    },
};

补充说明

  • 加上.setRequired(true)后,Discord客户端会强制要求用户输入该选项内容,同时自动将选项输入框直接展示在命令后方,省去点击步骤。
  • 若后续需要允许用户不输入内容(设为可选),可将该行改为.setRequired(false),但此时会回到原有的需点击选项的流程。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 08:35:20