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

Discord.js斜杠命令能否添加参数?如何注册/player-stats的玩家名称参数

discord.js 带参数斜杠命令注册实现方法

1. 命令注册阶段配置参数

  • 注册/player-stats命令时,在命令配置的options字段中添加玩家名称参数,参数类型指定为字符串,使用官方SlashCommandBuilder的示例配置如下(适配discord.js v14+版本):
const { SlashCommandBuilder } = require('discord.js');

const playerStatsCommand = new SlashCommandBuilder()
  .setName('player-stats')
  .setDescription('查询指定NBA球员的赛季统计数据')
  // 添加球员名称必填参数
  .addStringOption(option => 
    option.setName('player-name')
      .setDescription('要查询的球员全名,例如:LeBron James')
      .setRequired(true)
  );
  • 若你直接通过Discord REST API上传命令配置,对应参数的JSON结构为:
    { "type": 3, "name": "player-name", "description": "要查询的球员全名", "required": true }
    其中type: 3对应Discord斜杠命令的字符串参数类型。

2. 命令触发时读取参数

监听interactionCreate事件响应命令时,直接通过交互对象的options属性读取用户提交的参数值,传入你已封装的NBA API请求逻辑即可:

client.on('interactionCreate', async interaction => {
  // 过滤非斜杠命令的交互
  if (!interaction.isChatInputCommand()) return;
  
  if (interaction.commandName === 'player-stats') {
    // 读取用户输入的球员名称参数
    const playerName = interaction.options.getString('player-name');
    // 调用你已实现的NBA API查询逻辑
    const playerStats = await yourNBAApiRequestMethod(playerName);
    // 向用户返回查询结果
    await interaction.reply(playerStats);
  }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 17:36:04