Discord验证码机器人分配角色后崩溃问题求助
Discord机器人验证通过分配角色后崩溃
我开发了一款Discord机器人,功能是给新成员发送验证码,验证通过后分配角色。功能能正常运行,但完成角色分配后机器人会崩溃,错误信息如下:
我的代码如下:
const { Client, IntentsBitField, EmbedBuilder } = require("discord.js"); const client = new Client({ intents: [ IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.MessageContent, //IMPORTANT: make sure you enable "Message Content Intent" in the dev portal! IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.DirectMessages, ] }); client.login("bot tk"); const { Captcha } = require("discord.js-captcha"); client.on("ready", () => { console.log("Bot opérationnel"); }); const captcha = new Captcha(client, { roleID: "role id", //optional channelID: "channel id", //optional sendToTextChannel: true, //optional, defaults to false addRoleOnSuccess: true, //optional, defaults to true. whether you want the bot to add the role to the user if the captcha is solved kickOnFailure: true, //optional, defaults to true. whether you want the bot to kick the user if the captcha is failed caseSensitive: true, //optional, defaults to true. whether you want the captcha responses to be case-sensitive attempts: 3, //optional, defaults to 1. number of attempts before captcha is considered to be failed timeout: 300000, //optional, defaults to 60000. time the user has to solve the captcha on each attempt in milliseconds showAttemptCount: true, //optional, defaults to true. whether to show the number of attempts left in embed footer customPromptEmbed: new EmbedBuilder(), //customise the embed that will be sent to the user when the captcha is requested customSuccessEmbed: new EmbedBuilder(), //customise the embed that will be sent to the user when the captcha is solved customFailureEmbed: new EmbedBuilder(), //customise the embed that will be sent to the user when they fail to solve the captcha }); client.on("guildMemberAdd", async member => { //in your bot application in the dev portal, make sure you have intents turned on! captcha.present(member); //captcha is created by the package, and sent to the member }); captcha.on("success", data => { console.log(`Un membre a réussie le captcha !`); console.log(data); }); captcha.on("failure", data => { console.log(`Un membre a raté le captcha !`); console.log(data); });
问题原因
从错误截图可知,崩溃源于discord.js-captcha包内部尝试访问null对象的属性,核心原因是你传入了空的EmbedBuilder实例作为自定义Embed配置,导致包在处理时无法读取预期的Embed属性。
解决方案
方案1:移除空自定义Embed配置(推荐)
直接删掉三个空的Embed配置项,让包使用默认样式:
const captcha = new Captcha(client, { roleID: "role id", channelID: "channel id", sendToTextChannel: true, addRoleOnSuccess: true, kickOnFailure: true, caseSensitive: true, attempts: 3, timeout: 300000, showAttemptCount: true, // 移除以下空Embed配置 // customPromptEmbed: new EmbedBuilder(), // customSuccessEmbed: new EmbedBuilder(), // customFailureEmbed: new EmbedBuilder(), });
方案2:为自定义Embed添加必要内容
如果需要自定义Embed样式,必须为每个Embed添加标题、描述等基础内容,不能传空实例:
// 示例:创建带内容的提示Embed const customPromptEmbed = new EmbedBuilder() .setTitle("验证码验证") .setDescription("请输入下方显示的验证码,完成后即可获得服务器访问权限") .setColor("#2f3136"); // 同理创建成功、失败Embed const customSuccessEmbed = new EmbedBuilder() .setTitle("验证成功!") .setDescription("已为你分配角色,现在可以正常访问服务器了") .setColor("#00ff00"); const customFailureEmbed = new EmbedBuilder() .setTitle("验证失败") .setDescription("你已被移出服务器,若有疑问请联系管理员") .setColor("#ff0000"); const captcha = new Captcha(client, { // ...其他配置 customPromptEmbed: customPromptEmbed, customSuccessEmbed: customSuccessEmbed, customFailureEmbed: customFailureEmbed, });
额外检查
- 确保机器人拥有管理角色的权限
- 确认分配的角色ID正确,且该角色的位置低于机器人的最高角色位置
内容的提问来源于stack exchange,提问作者Marcel
相关产品推荐
相关产品推荐

