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

Discord机器人运行一日后Token被重置,请求排查原因

Discord机器人Token因高频连接被重置的解决思路

核心问题排查方向

  • 过度申请Intent:你的机器人申请了几乎所有Discord Intent,其中很多是当前功能不需要的(如GuildMessageTyping、DirectMessageTyping、GuildPresences等)。多余的Intent会让机器人接收大量无意义的事件,大幅增加与Discord服务器的交互次数,最终触发限流导致Token被重置。
  • 潜在异常重连:虽然你排除了client.login重复调用,但如果机器人因事件过载频繁断开,discord.js的自动重连机制可能多次发起连接请求,累计触发连接次数阈值。
  • 部署环境问题:检查是否有多个进程同时运行机器人,或部署平台存在自动重启逻辑导致重复登录。

具体解决步骤

  1. 精简Intent列表
    只保留机器人实际需要的Intent,大幅减少事件接收量:
const client = new Client({
  intents: [
    IntentsBitField.Flags.Guilds, // 服务器基础信息
    IntentsBitField.Flags.GuildMessages, // 服务器消息事件
    IntentsBitField.Flags.MessageContent, // 读取消息内容(需在Discord开发者后台启用)
    IntentsBitField.Flags.GuildMessageReactions, // 消息反应事件
  ],
});
  1. 添加连接状态监控
    在代码中加入连接日志,方便排查异常重连情况:
client.on('connect', () => {
  console.log('已连接到Discord服务器');
});

client.on('disconnect', () => {
  console.log('与Discord服务器断开连接');
});

client.on('reconnecting', () => {
  console.log('正在重新连接到Discord服务器');
});
  1. 优化事件处理逻辑
    将独立的if改为else if,减少不必要的条件判断(当前代码的return已避免重复响应,此优化进一步提升效率):
// 替换原消息内容判断部分
if (message.content.toLowerCase().includes('uod')) {
  message.react('👍');
  message.react('👎');
} else if (message.content.toLowerCase().includes('conga')) {
  message.reply('<a:conga:1109250474982506630>');
} else if (message.content.toLowerCase() === 'wow' || message.content.toLowerCase().includes(':0')) {
  message.reply('https://media.discordapp.net/attachments/1108404891145220268/1108405967344893992/wow.mov');
} else if (message.content.toLowerCase().includes('idk') || message.content.toLowerCase().includes('nie wiem')) {
  message.reply('https://cdn.discordapp.com/attachments/1108404891145220268/1108405993471225896/idk.mp4');
}
  1. 检查部署环境
    确认部署平台(如VPS、容器服务)没有重复启动机器人进程,且不存在异常重启机制导致机器人反复登录。

内容的提问来源于stack exchange,提问作者nielot

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 22:27:06