discord.js v14创建频道报错DiscordAPIError[50035]求助
问题
使用discord.js v14.14.1编写监听$setup命令创建文本频道的代码,执行后触发DiscordAPIError[50035],错误提示类型参数"text"不是int32类型。
代码片段
client.on('messageCreate', async (msg) => { if (msg.content === '$setup') { msg.reply('Setup command received'); msg.guild.channels.create({ name: "new-channel", type: "text", permissionOverwrites: [ { id: msg.guild.id, accept: [PermissionsBitField.Flags.ViewChannel], }, ], }); }
完整报错信息
/home/runner/MegaBot/node_modules/@discordjs/rest/dist/index.js:722 throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData); ^ DiscordAPIError[50035]: Invalid Form Body type[NUMBER_TYPE_COERCE]: Value "text" is not int32. at handleErrors (/home/runner/MegaBot/node_modules/@discordjs/rest/dist/index.js:722:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async SequentialHandler.runRequest (/home/runner/MegaBot/node_modules/@discordjs/rest/dist/index.js:1120:23) at async SequentialHandler.queueRequest (/home/runner/MegaBot/node_modules/@discordjs/rest/dist/index.js:953:14) at async _REST.request (/home/runner/MegaBot/node_modules/@discordjs/rest/dist/index.js:1266:22) at async GuildChannelManager.create (/home/runner/MegaBot/node_modules/discord.js/src/managers/GuildChannelManager.js:171:18) { requestBody: { files: undefined, json: { name: 'new-channel', topic: undefined, type: 'text', nsfw: undefined, bitrate: undefined, user_limit: undefined, parent_id: undefined, position: undefined, permission_overwrites: [ { id: '1157099244453376162', type: 0, allow: '0', deny: '0' } ], rate_limit_per_user: undefined, rtc_region: undefined, video_quality_mode: undefined, default_thread_rate_limit_per_user: undefined, available_tags: undefined, default_reaction_emoji: undefined, default_auto_archive_duration: undefined, default_sort_order: undefined, default_forum_layout: undefined } }, rawError: { message: 'Invalid Form Body', code: 50035, errors: { type: { _errors: [ { code: 'NUMBER_TYPE_COERCE', message: 'Value "text" is not int32.' } ] } } }, code: 50035, status: 400, method: 'POST', url: 'https://discord.com/api/v10/guilds/1157099244453376162/channels' } Node.js v20.10.0
解决方法
修正频道类型参数
discord.js v14中,创建频道的type参数不再接受字符串值,必须使用ChannelType枚举的对应常量,文本频道对应ChannelType.GuildText,使用前需从discord.js导入该枚举。修正权限覆盖字段名
代码中permissionOverwrites里的accept字段错误,v14中正确字段名为allow。
修改后的完整代码
const { Client, GatewayIntentBits, PermissionsBitField, ChannelType } = require('discord.js'); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); client.on('messageCreate', async (msg) => { if (msg.content === '$setup') { msg.reply('Setup command received'); await msg.guild.channels.create({ name: "new-channel", type: ChannelType.GuildText, permissionOverwrites: [ { id: msg.guild.id, allow: [PermissionsBitField.Flags.ViewChannel], }, ], }); } }); client.login('你的机器人Token');
关键注意点
- 必须正确导入
ChannelType枚举,否则会出现未定义错误。 - 调用
channels.create建议添加await,避免异步操作引发的潜在问题。 - 机器人需拥有
ManageChannels权限才能创建频道,否则会触发权限不足错误。
内容的提问来源于stack exchange,提问作者user23356876
相关产品推荐
相关产品推荐

