Telegram转Discord机器人报错:'Updater'对象无'dispatcher'属性
解决Telegram转Discord机器人的AttributeError: 'Updater' object has no attribute 'dispatcher'错误
我正在开发一款同步指定用户消息的Telegram转Discord机器人,但运行时总是触发错误:AttributeError: 'Updater' object has no attribute 'dispatcher'。这是我第一次开发这类机器人,找不到dispatcher的替代方案,当前代码如下:
import telegram from telegram.ext import * from discord.ext import commands import discord import asyncio telegram_token = "" discord_token = "" # Discord Channel ID discord_channel_id = "112865" telegram_bot = telegram.Bot(token=telegram_token) intents = discord.Intents.default() discord_bot = discord.Client(intents=intents) def telegram_message_handler(update, context): message = update.effective_message text = message.text username = message.from_user.username # Send the message to Discord discord_message = f"[@{username}]: {text}" discord_channel = discord_bot.get_channel(int(discord_channel_id)) asyncio.run_coroutine_threadsafe(discord_channel.send(discord_message), discord_bot.loop) updater = Updater(telegram_token, update_queue=None) dispatcher = updater.dispatcher message_handler = MessageHandler(filters.text & ~filters.command, telegram_message_handler) dispatcher.add_handler(message_handler) @discord_bot.event async def on_ready(): print(f"Connected to Discord as {discord_bot.user.name}") @discord_bot.event async def on_message(message): if message.author == discord_bot.user: return # Send the message to Telegram telegram_chat_id = "dav52" await telegram_bot.send_message(chat_id=telegram_chat_id, text=message.content) # Start the Telegram bot updater.start_polling() # Start the Discord bot discord_bot.run(discord_token)
问题原因及解决方案
这个错误源于你使用的是python-telegram-bot v20+版本,该版本彻底废弃了旧的Updater和dispatcher架构,改用基于Application的异步API。以下是适配新版本的完整代码:
import asyncio from telegram import Update from telegram.ext import ApplicationBuilder, MessageHandler, filters, ContextTypes import discord telegram_token = "" discord_token = "" # Discord Channel ID discord_channel_id = 112865 # 直接用整数类型 # Telegram目标聊天ID telegram_chat_id = "dav52" # 初始化Discord客户端 intents = discord.Intents.default() intents.message_content = True # 必须开启此权限才能读取消息内容 discord_bot = discord.Client(intents=intents) # Telegram消息处理异步函数 async def telegram_message_handler(update: Update, context: ContextTypes.DEFAULT_TYPE): message = update.effective_message if not message.text: return username = update.effective_user.username discord_message = f"[@{username}]: {message.text}" # 获取Discord频道并发送消息 discord_channel = discord_bot.get_channel(discord_channel_id) if discord_channel: await discord_channel.send(discord_message) # Discord消息处理事件 @discord_bot.event async def on_ready(): print(f"Connected to Discord as {discord_bot.user.name}") @discord_bot.event async def on_message(message): if message.author == discord_bot.user: return # 发送消息到Telegram try: # 复用Telegram应用的bot实例发送消息 telegram_bot = context.bot if 'context' in locals() else await ApplicationBuilder().token(telegram_token).build().bot await telegram_bot.send_message(chat_id=telegram_chat_id, text=message.content) except Exception as e: print(f"发送Telegram消息失败: {e}") async def main(): # 初始化Telegram应用 telegram_app = ApplicationBuilder().token(telegram_token).build() # 添加消息处理器 telegram_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, telegram_message_handler)) # 同时运行Telegram和Discord bot,避免阻塞 await asyncio.gather( telegram_app.run_polling(), discord_bot.start(discord_token) ) if __name__ == "__main__": asyncio.run(main())
关键修改说明
- 替换
Updater为ApplicationBuilder,构建Telegram应用实例 - 将Telegram消息处理器改为异步函数,适配v20+版本的API要求
- 开启Discord的
message_content权限,否则无法读取用户消息内容 - 使用
asyncio.gather同时启动两个bot,解决原代码中阻塞运行的问题 - 移除线程安全的异步调用,直接在异步上下文内处理跨平台消息发送
内容的提问来源于stack exchange,提问作者David Henson
相关产品推荐
相关产品推荐

