与 twitch bot 一起运行 discord bot

问题描述

我有 twitch bot 和 discord bot。我想通过 discord 控制 twitch bot,并将一些数据从 twitch bot 发送到 discord bot。例如,我会在 discord 中输入“?unload xx”,它会关闭 twitch bot 中的某些功能。另一个例子是在一些抽搐事件之后,我会将数据发送到 discord bot,discord bot 会在频道中显示它。我试图运行discord bot,然后在backgrond循环中twitch bot,但这不起作用。我也试图为 webhooks 设置 http 服务器,但这也不起作用。有什么我想念的吗?我无法解决这个问题。感谢大家的帮助。

编辑 两者都在 python 中,不同的文件但导入到主文件并在一个文件中运行。 我正在尝试 asyncio,这是不和谐机器人和后台抽搐,但我收到“此事件循环已在运行”错误。 我也在不同的线程中尝试 discord bot 和 http 服务器,但它的工作非常奇怪,有时没有响应,有时没有事件开始,一段时间后关闭

这是我尝试过但没有用的线程方法

discord_bot = TestBot(command_prefix="?",intents=discord.Intents.default())
twitch_bot = Bot()

twitch_thread = threading.Thread(target=twitch_bot.run,daemon=True)

twitch_thread.start()
discord_bot.run(TOKEN)

解决方法

根据 discord.py API 参考中的 discord.Client.run 部分:

这个函数必须是最后一个调用的函数,因为它是阻塞的。这意味着在此函数调用之后注册的事件或任何被调用的东西在它返回之前不会执行。

我建议您使用 discord.Client.start 而不是 discord.Client.run。它应该适用于 asyncio,假设 twitch_bot.run 不会像 discord.Client.run 那样阻塞。

loop = asyncio.get_event_loop()
loop.create_task(discord_bot.start(TOKEN))
loop.create_task(twitch_bot.run())
loop.run_forever()
,

有两种方法可以解决这个问题。

一个是使用 background task.

另一个(我将重点关注)是使用线程 - 一个内置(我相信)python 库。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="JS" maxlength="8" class="form-control" id="js_leading_zero" placeholder="JS000000">