Discord v13机器人设置验证频道时无法识别文本频道问题求助
问题修复方案
核心原因
你的代码存在版本API不兼容和逻辑错误两类问题:
- 频道类型枚举不匹配:Discord.js v12中文本频道的
type属性值为text,而非你代码中写的GUILD_TEXT,这是频道识别失败的直接原因 - 权限方法已弃用:v13及以上版本中
updateOverwrite、createOverwrite方法已被移除,替换为新的权限覆写接口 - 逻辑错误:原代码中两个已设置校验的判断条件完全一致,第二个判断永远不会触发
修复方案
若仍使用Discord.js v12
修改频道类型判断和重复逻辑即可,完整可运行代码如下:
const db = require('quick.db'); const { PREFIX } = require('../../config'); module.exports = { config: { name: 'setverification', aliases: ['sv', 'setv', 'setverify'], category: 'moderation', description: '设置验证频道和角色', usage: '[频道名 | 频道ID | 频道@提及] <角色名 | 角色ID | 角色@提及>', accessableby: '管理员' }, run: async (bot, message, args) => { let prefix; let fetched = await db.fetch(`prefix_${message.guild.id}`); if (fetched === null) { prefix = PREFIX } else { prefix = fetched } if (!message.member.permissions.has("ADMINISTRATOR")) return message.channel.send("**你没有所需权限!需要[管理员]权限!**"); if (!args[0]) return message.channel.send("**请输入用户验证使用的频道名称!**"); if (!args[1]) return message.channel.send("**请输入验证通过后授予用户的角色!**"); let channel = message.mentions.channels.first() || message.guild.channels.cache.get(args[0]) || message.guild.channels.cache.find(c => c.name.toLowerCase() === args[0].toLocaleLowerCase()); if (!channel || channel.type !== "text") return message.channel.send("**请输入有效的频道!**") let role = message.mentions.roles.first() || message.guild.roles.cache.get(args[1]) || message.guild.roles.cache.find(r => r.name.toLowerCase() === args[1].toLocaleLowerCase()); if (!role) return message.channel.send("**请输入有效的角色!**"); let verifiedchannel = channel; try { let a = await db.fetch(`verificationchannel_${message.guild.id}`); let b = await db.fetch(`verificationrole_${message.guild.id}`); if (channel.id === a && role.id === b) { return message.channel.send('**该频道和角色已经设置为验证频道和验证角色!**'); } else if (channel.id === a) { return message.channel.send("**该频道已经设置为验证频道!**"); } else if (role.id === b) { return message.channel.send("**该角色已经设置为验证角色!**"); } else { message.guild.channels.cache.forEach(channel => { if (channel.type === 'category' && channel.id === verifiedchannel.id) return; let r = channel.permissionOverwrites.get(role.id); if (!r) return; if (r.deny.has("VIEW_CHANNEL") || r.deny.has("SEND_MESSAGES")) return; channel.createOverwrite(message.guild.id, { VIEW_CHANNEL: false }); channel.updateOverwrite(role, { VIEW_CHANNEL: true, SEND_MESSAGES: true }); }); verifiedchannel.updateOverwrite(role, { SEND_MESSAGES: false, VIEW_CHANNEL: false }); bot.guilds.cache.get(message.guild.id).channels.cache.get(channel.id).send(`**欢迎来到 ${message.guild.name}!\n输入 \`${prefix}verify\` 完成验证**`); db.set(`verificationchannel_${message.guild.id}`, channel.id); db.set(`verificationrole_${message.guild.id}`, role.id); return message.channel.send(`**验证频道和角色已成功设置,验证频道为 \`${channel.name}\`!**`); }; } catch (err) { console.log(err) return message.channel.send("**错误 - `权限不足或频道不是文本频道!`**"); }; } };
若使用Discord.js v13/v14
除上述修改外,还需要调整权限接口和枚举值,同时在机器人初始化时开启GUILDS、GUILD_MESSAGES网关意图:
- 提前从discord.js导入
ChannelType枚举 - 频道类型判断改为
if (!channel || channel.type !== ChannelType.GuildText) - 权限覆写方法替换:
// 原createOverwrite替换为 channel.permissionOverwrites.create(message.guild.id, { ViewChannel: false }); // 原updateOverwrite替换为 channel.permissionOverwrites.edit(role, { ViewChannel: true, SendMessages: true });
内容的提问来源于stack exchange,提问作者Oofin
相关产品推荐
相关产品推荐

