Discord.js v14创建语音频道报错:name[BASE_TYPE_REQUIRED]字段必填
解决Discord临时语音频道创建的50035错误
错误原因
你使用的是Discord.js v14版本,但代码沿用了v13及更早的写法。v14中guild.channels.create()的参数结构已变更——不再接受单独的name作为第一个参数,所有配置(包括name)必须放在单一的选项对象中。你的代码把频道名作为第一个参数传入,导致Discord API无法识别name字段,从而返回name[BASE_TYPE_REQUIRED]错误。
修复步骤
1. 修正频道创建代码
将频道名移至选项对象内部,同时适配v14的参数要求:
// 替换原创建频道的代码 const newChannel = await guild.channels.create({ name: `${user.username}'s Channel`, type: 2, // v14中语音频道类型用数字2,或导入ChannelType后用ChannelType.GuildVoice parent: newState.channel.parentId, // v14中parent需传入父分类ID,而非对象 userLimit: 4 });
若使用
ChannelType,需先导入:const { ChannelType } = require('discord.js');,此时type可写为ChannelType.GuildVoice
2. 修正用户移动代码
v14中newState.setChannel()方法已废弃,改用成员的voice属性设置:
// 替换原移动用户的代码 await newState.member.voice.setChannel(newChannel);
3. 优化临时频道删除逻辑
原代码通过用户名匹配频道名存在缺陷(用户改名后无法匹配),建议通过频道分类或固定标识判断:
// 修改删除判断逻辑 if (oldState.channel && oldState.channel.parentId === newState.channel.parentId // 限制为触发频道同分类下的频道 && oldState.channel.name.includes("'s Channel") // 保留原命名标识 && oldState.channel.members.size === 0) { await oldState.channel.delete(); console.log(`Il canale vocale ${oldState.channel.name} è stato eliminato.`); }
完整修正后的代码
const { ChannelType } = require('discord.js'); client.on('voiceStateUpdate', async (oldState, newState) => { const channelID = '802270488125636609'; // 监听的目标频道ID // 用户加入指定频道时创建临时频道 if (newState.channelId === channelID) { const guild = newState.guild; const user = newState.member.user; const newChannel = await guild.channels.create({ name: `${user.username}'s Channel`, type: ChannelType.GuildVoice, parent: newState.channel.parentId, userLimit: 4 }); await newState.member.voice.setChannel(newChannel); console.log(`Il canale vocale ${newChannel.name} è stato creato per ${user.username}.`); } // 临时频道无人时自动删除 if (oldState.channelId && oldState.channel.parentId === newState.channel?.parentId && oldState.channel.name.includes("'s Channel") && oldState.channel.members.size === 0) { await oldState.channel.delete(); console.log(`Il canale vocale ${oldState.channel.name} è stato eliminato.`); } });
内容的提问来源于stack exchange,提问作者Darla
相关产品推荐
相关产品推荐

