Discord.js Slash 命令名称无效

Posted

技术标签:

【中文标题】Discord.js Slash 命令名称无效【英文标题】:Discord.js Slash commands name invalid 【发布时间】:2021-08-15 20:10:26 【问题描述】:

我对 discord.js 斜杠命令有疑问。

正如您在我的代码中看到的,我将名称设置为“姓名”和“年龄”,所以我不明白问题出在哪里。

这是我的错误:

error: (node:16004) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body 
options[0].name: Command name is invalid

这是我的代码:

const Discord = require('discord.js');
require("dotenv").config();
const client = new Discord.Client();

client.on('ready', async () => 
  console.log(`Logged in as $client.user.tag!`);

  const getApp = (guildid) => 
      const app = client.api.applications(client.user.id);
      if(guildid)
          app.guilds(guildid);
      
      return app;
  

  await getApp("842486032842358784").commands.post(
    data: 
        name: 'embed',
        description: 'Displays embed',
        options: [
            
                name: 'Name',
                description: 'Your Name',
                required: true,
                type: 3
            ,
            
                name: 'Age',
                description: 'Your Age',
                required: false,
                type: 4
            
        ]
    ,
  )

  client.ws.on('INTERACTION_CREATE', async(interaction) => 
      const command = interaction.data.name.toLowerCase();
      const  name, options  = interaction.data;
      console.log(options);
      if(command === "embed")
          reply(interaction, 'hi');
      
  )

  const reply = (interaction, response) => 
    client.api.interactions(interaction.id, interaction.token).callback.post(
        data: 
            type: 4,
            data: 
                content: response,
            
        
    )
  
);

client.login(process.env.TOKEN);

【问题讨论】:

请仅在您的问题中添加必要的信息。不需要将其添加到末尾。 【参考方案1】:

选项名称应全部小写,在您的情况下,将“Name”替换为“name”,将“Age”替换为“age”。那应该行得通。代码如下:

const Discord = require('discord.js');
require("dotenv").config();
const client = new Discord.Client();

client.on('ready', async() => 
  console.log(`Logged in as $client.user.tag!`);

  const getApp = (guildid) => 
    const app = client.api.applications(client.user.id);
    if (guildid) 
      app.guilds(guildid);
    
    return app;
  

  await getApp("842486032842358784").commands.post(
    data: 
      name: 'embed',
      description: 'Displays embed',
      options: [
          name: 'name',
          description: 'Your Name',
          required: true,
          type: 3
        ,
        
          name: 'age',
          description: 'Your Age',
          required: false,
          type: 4
        
      ]
    ,
  )

  client.ws.on('INTERACTION_CREATE', async(interaction) => 
    const command = interaction.data.name.toLowerCase();
    const 
      name,
      options
     = interaction.data;
    console.log(options);
    if (command === "embed") 
      reply(interaction, 'hi');
    
  )

  const reply = (interaction, response) => 
    client.api.interactions(interaction.id, interaction.token).callback.post(
      data: 
        type: 4,
        data: 
          content: response,
        
      
    )
  
);

client.login(process.env.TOKEN);

【讨论】:

以上是关于Discord.js Slash 命令名称无效的主要内容,如果未能解决你的问题,请参考以下文章

Discord.js v13 语音通道数据未更新

Slack App Slash 命令 - 检测到无效签名(401 未经授权)

message.guild.createRole无效-discord.js

ReferenceError:discord.js 突击队中分配的左侧无效

Discord.js DiscordAPIError:无效的表单正文 0.permissions:长度必须为 10 或更少

Discord.js 如何列出具有特定角色的所有成员的显示名称