You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

我的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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 19:48:04