在Pterodactyl面板开发Telegram Bot遇409 ETELEGRAM冲突错误求助
解决Telegram机器人409 Conflict错误
错误原因
这个错误是因为同一个Telegram Bot被多个实例同时运行,多个进程都在调用getUpdates接口拉取消息,Telegram服务器会终止冲突的请求。
看你的代码,问题出在自动重启逻辑:当文件更新时,你直接require(file)重新加载脚本,但旧的Bot实例没有被停止,仍然在后台运行并轮询消息,导致新旧实例冲突。
解决方案
1. 重启前停止旧Bot实例
修改自动重启代码,在重新加载前关闭旧的轮询进程:
process.env.NTBA_FIX_319 = 1 require('./settings.js') const TelegramBot = require('node-telegram-bot-api'); const CFonts = require('cfonts'); const chalk = require('chalk') const axios = require('axios'); const fs = require('fs'); const path = require('path'); // 声明bot变量,让它在全局作用域可访问 let bot; // 封装启动逻辑 function startBot() { bot = new TelegramBot(botkey, { polling: true }); console.log(chalk.red(`BOT TELEGRAM IS ACTIVE`)) bot.sendMessage(ownerid, `BOT ACTIVE`) //MAIN bot.onText(/\/start/, (msg, match) => { const chatId = msg.chat.id; bot.sendMessage(chatId, `Hello! I am Leonn, a BOT made by Raeonn to process in-game recharges!\nYour Telegram ID is : ${chatId}\nMessage Raeonn to get verified.`); }); } startBot(); let file = require.resolve(__filename) fs.watchFile(file, () => { fs.unwatchFile(file) console.log(chalk.blue(`Update ${__filename}`)) // 停止旧实例的轮询,避免残留进程 if (bot) { bot.stopPolling({ cancel: true }); } delete require.cache[file] require(file) })
2. 改用nodemon实现自动重启
手动写的文件监听逻辑容易出现实例残留,推荐用nodemon替代:
- 安装nodemon:
npm install -g nodemon - 启动命令改为:
nodemon your-bot-file.js - 它会在文件更新时完全终止旧进程再启动新进程,从根源避免多实例冲突。
3. 清理Pterodactyl面板的残留进程
登录Pterodactyl面板检查机器人服务器:
- 进入控制台,执行
ps aux | grep node查看所有Node进程 - 若有多余进程,手动终止:
kill -9 [进程ID] - 确认面板的自动重启设置不会同时启动多个实例。
内容的提问来源于stack exchange,提问作者Lyraeon
相关产品推荐
相关产品推荐

