gpt4 book ai didi

python-3.x - 如何从事件循环外部(即来自 python-telegram-bot 线程)使用 discord.py 发送消息?

转载 作者:行者123 更新时间:2023-12-02 17:01:59 31 4
gpt4 key购买 nike

我想使用库 python-telegram-bot 和 discord.py(版本 1.0.0)制作一个在 discord 和 telegram 之间进行通信的机器人。但是问题是 discord.py 使用异步函数和 python-telegram-bot 线程。使用下面的代码,对于在 discord 中发布的消息一切正常(机器人将它们正确发送到电报),但是反之则不起作用(机器人从电报中获取消息并将其发送到 discord)。我以前遇到语法/运行时错误问题,因为我试图在同步函数中运行 discords channel.send 函数(因此要么只返回一个生成器对象,要么提示我不能使用 await 在同步函数中)。然而,同时 python-telegram-bot 的 MessageHandler 需要一个同步函数,所以当给他一个异步函数时,Python 提示从未为异步函数调用“await”。我现在尝试使用 asgiref 库中的 async_to_sync 方法从 MessageHandler 运行我的异步 broadcastMsg,但是代码仍然没有将消息发送到 discord!它似乎正确地调用了该函数,但仅在 print('I get to here') 行之前。不显示任何错误,也不会在不和谐中弹出任何消息。我想这与我必须将该函数注册为 discord.py 事件循环中的任务这一事实有关,但是注册仅在 botDiscord.run(TOKENDISCORD) 之前发生时才有效已经被执行,这当然必须在之前发生。所以把我的问题归结为一个问题:我如何能够与另一个线程(来自电报 MessageHandler)的 discord.py 事件循环交互。或者,如果这不可能:如何在不在 discord.py 事件循环内的情况下使用 discord.py 发送消息?

谢谢你的帮助

import asyncio
from asgiref.sync import async_to_sync
from telegram import Message as TMessage
from telegram.ext import (Updater,Filters,MessageHandler)
from discord.ext import commands
import discord

TChannelID = 'someTelegramChannelID'
DChannel = 'someDiscordChannelObject'

#%% define functions / commands
prefix = "?"
botDiscord = commands.Bot(command_prefix=prefix)
discordChannels = {}


async def broadcastMsg(medium,channel,message):
'''
Function to broadcast a message to all linked channels.
'''
if isinstance(message,TMessage):
fromMedium = 'Telegram'
author = message.from_user.username
channel = message.chat.title
content = message.text
elif isinstance(message,discord.Message):
fromMedium = 'Discord'
author = message.author
channel = message.channel.name
content = message.content
# check where message comes from
textToSend = '%s wrote on %s %s:\n%s'%(author,fromMedium,channel,content)
# go through channels and send the message
if 'telegram' in medium:
# transform channel to telegram chatID and send
updaterTelegram.bot.send_message(channel,textToSend)

elif 'discord' in medium:
print('I get to here')
await channel.send(textToSend)

print("I do not get there")


@botDiscord.event
async def on_message(message):
await broadcastMsg('telegram',TChannelID,message)

def on_TMessage(bot,update):
# check if this chat is already known, else save it
# get channels to send to and send message
async_to_sync(broadcastMsg)('discord',DChannel,update.message)


#%% initialize telegram and discord bot and run them
messageHandler = MessageHandler(Filters.text, on_TMessage)
updaterTelegram = Updater(token = TOKENTELEGRAM, request_kwargs={'read_timeout': 10, 'connect_timeout': 10})
updaterTelegram.dispatcher.add_handler(messageHandler)
updaterTelegram.start_polling()
botDiscord.run(TOKENDISCORD)

最佳答案

How can I send a message with discord.py without being within the discord.py event loop?

要从事件循环线程外部安全地安排协程,请使用 asyncio.run_coroutine_threadsafe :

_loop = asyncio.get_event_loop()

def on_TMessage(bot, update):
asyncio.run_coroutine_threadsafe(
broadcastMsg('discord', DChannel, update.message), _loop)

关于python-3.x - 如何从事件循环外部(即来自 python-telegram-bot 线程)使用 discord.py 发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53722398/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com