搭建Discord音乐Bot加载命令时遇DiscordAPIError错误求助
Discord音乐Bot命令加载错误问题
我正在为Discord频道搭建音乐Bot,但加载命令时持续出错,哪怕使用Discord官方文档的示例代码也会触发问题。
示例代码
const { REST, Routes } = require("discord.js"); const { Client, GatewayIntentBits } = require("discord.js"); const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const CLIENT_ID = "ID"; const TOKEN = "token"; const commands = [ { name: "ping", description: "Replies with Pong!", }, ]; const rest = new REST({ version: "10" }).setToken(TOKEN); (async () => { try { await rest.put(Routes.applicationCommands(CLIENT_ID), { headers: { authorization: `Bot ${TOKEN}` }, body: commands, }); } catch (error) { console.error(error); } })(); client.on("ready", () => { console.log(`Logged in as ${client.user.tag}!`); }); client.on("interactionCreate", async (interaction) => { if (!interaction.isChatInputCommand()) return; if (interaction.commandName === "ping") { await interaction.reply("Pong!"); } }); client.login(TOKEN);
报错信息
DiscordAPIError[undefined]: 无描述信息 在 SequentialHandler.runRequest (...\MusicBot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:293:15) 在 processTicksAndRejections (node:internal/process/task_queues:96:5) 在 async SequentialHandler.queueRequest (...\MusicBot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14) 在 async REST.request (...\React\MusicBot\node_modules\@discordjs\rest\dist\lib\REST.cjs:52:22) 在 async ...\MusicBot\index.js:24:5 { rawError: Uint8Array(155) [ 60, 104, 116, 109, 108, 62, 13, 10, 60, 104, 101, 97, 100, 62, 60, 116, 105, 116, 108, 101, 62, 52, 48, 48, 32, 66, 97, 100, 32, 82, 101, 113, 117, 101, 115, 116, 60, 47, 116, 105, 116, 108, 101, 62, 60, 47, 104, 101, 97, 100, 62, 13, 10, 60, 98, 111, 100, 121, 62, 13, 10, 60, 99, 101, 110, 116, 101, 114, 62, 60, 104, 49, 62, 52, 48, 48, 32, 66, 97, 100, 32, 82, 101, 113, 117, 101, 115, 116, 60, 47, 104, 49, 62, 60, 47, 99, 101, 110, 116, 101, ... 55 more items ], code: undefined, status: 400, method: 'PUT', url: 'https://discord.com/api/v9/applications/1021797370285010954/commands', requestBody: { files: undefined, json: undefined } }
问题原因与解决办法
- 问题根源:你已经通过
rest.setToken(TOKEN)完成了令牌配置,却又在请求的headers里重复添加authorization字段,导致请求体解析异常,触发"无描述信息"错误。 - 修复步骤:
- 移除请求配置中的
headers属性,rest.setToken会自动处理授权头。 - 确保
CLIENT_ID和TOKEN替换为你Bot的真实ID与令牌。 - 重新运行代码,命令即可正常加载。
- 移除请求配置中的
修改后的核心代码片段:
(async () => { try { await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands, }); } catch (error) { console.error(error); } })();
内容的提问来源于stack exchange,提问作者Pieter Burger
相关产品推荐
相关产品推荐

