我的Discord bot的captcha功能触发后无响应无报错,该如何排查解决?
问题修复方案
核心故障原因
你在verification指令的判断分支内,仅完成了verifyHuman验证码逻辑函数的定义,没有实际调用该函数,因此指令触发后没有任何代码执行,自然没有响应和日志输出。
具体修复步骤
- 在
if (command === 'verification')代码块的末尾添加verifyHuman(message),触发验证码逻辑 - 调整discord.js v12的消息发送格式,附件必须放在
files数组参数中才能正常发送 - 建议给消息收集器添加超时逻辑,避免资源长期占用
- 确保你初始化Client时已经开启了接收消息的对应网关意图,否则无法触发
message事件
修复后完整代码
const Discord = require('discord.js-12'); // 初始化时添加必要的网关意图 const client = new Discord.Client({ intents: [ Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES ] }); const prefix = 'ri-'; const Captcha = require("@haileybot/captcha-generator"); const path = require("path"), fs = require("fs") client.once('ready', () => { console.log('Ready!'); }); // 全局的captcha生成和本地保存逻辑如果不需要可以直接删除 let captcha = new Captcha(); console.log(captcha.value); captcha.PNGStream.pipe(fs.createWriteStream(path.join(__dirname, `${captcha.value}.png`))); captcha.JPEGStream.pipe(fs.createWriteStream(path.join(__dirname, `${captcha.value}.jpeg`))); client.on('message', async message => { if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).trim().split(/ +/); const command = args.shift().toLowerCase(); if (command === 'verification') { function verifyHuman(msg) { let captcha = new Captcha(); // 调整附件发送格式 msg.channel.send( "**请输入下图中显示的文本:**", { files: [new Discord.MessageAttachment(captcha.JPEGStream, "captcha.jpeg")] } ); // 给收集器添加1分钟超时 let collector = msg.channel.createMessageCollector( m => m.author.id === msg.author.id, { time: 60000 } ); collector.on("collect", m => { if (m.content.toUpperCase() === captcha.value) { msg.channel.send("验证成功!"); } else { msg.channel.send("验证失败!"); } collector.stop(); }); // 超时提示 collector.on("end", (collected, reason) => { if(reason === 'time') { msg.channel.send("验证超时,请重新发送指令申请验证") } }) }; // 调用函数执行逻辑 verifyHuman(message); } }); client.login('你的Bot Token');
内容的提问来源于stack exchange,提问作者Aki
相关产品推荐
相关产品推荐

