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

如何在Discord.js中定义并引用stringOption参数的输入文本?

问题解决:Discord.js 获取斜杠命令字符串参数并回复

你的代码存在两处关键问题,导致无法正确获取并回复用户输入的文本:

  1. 字符串参数获取方式错误:interaction.getOption('text') 针对字符串类型的选项,直接返回的就是用户输入的字符串内容,不需要额外访问 .content 属性。
  2. 回复方法使用错误:interaction.editReply 用于编辑已发送的机器人回复,你需要的是首次发送回复,应该使用 interaction.reply。

另外,toString(Text) 属于多余操作,因为获取到的参数本身就是字符串类型。

修正后的代码如下:

module.exports = {
    data: new SlashCommandBuilder()
        .setName('tsay')
        .setDescription('Replies with the text that was entered (General)')
        .addStringOption(option => 
            option.setName('text')
                .setDescription('Text to be copied')
                .setRequired(true)),

    async execute(interaction, client) {
        // 直接获取用户输入的字符串参数
        const userInputText = interaction.getOption('text');
        // 发送回复
        await interaction.reply({
            content: userInputText
        });
    } 
}

简单说明:

  • interaction.getOption('text') 会直接返回用户在 text 参数中输入的内容。
  • interaction.reply() 是机器人响应斜杠命令发送首次回复的标准方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 12:10:16