1
votes

I have twitch bot and discord bot. I would like to control the twitch bot via discord, and send some data from twitch bot to discord bot. For example, I would type "?unload xx" in discord and it would turn off some feature in the twitch bot. Another example is that after some twitch event, I would send data to discord bot, and discord bot would show it in channel. I was trying to run the discord bot, and then twitch bot in backgrond loop, but that didn't work. I was also trying to setup http server for webhooks, but that didn't work either. Is there something I am missing? I can't solve this problem. Thank's for help guys.

EDIT Both are in python, different files but imported to the main and run in one. I was trying asyncio, that was the discord bot and twitch in background, but I was getting "this event loop is already running" error. I was also trying discord bot and http server in differet threads, but it was working really weirdly, not responding sometimes, sometimes not event starting, turning off after while.

Here is threading way I've tried but didn't work

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)
2
Please add more information. Are both in the same file? Different threads? Both in python? Be more specific about your setup and what you're tryingDaniel Gomez
I have edited the problemLokils

2 Answers

2
votes

According to the discord.Client.run section from the discord.py API Reference:

This function must be the last function to call due to the fact that it is blocking. That means that registration of events or anything being called after this function call will not execute until it returns.

I recommend you to use discord.Client.start instead of discord.Client.run. It should work with asyncio, assuming that twitch_bot.run wouldn't block like how discord.Client.run does.

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

There are two ways you can aproach this.

One is using a background task.

The other(which I will focus on) is using threading - a builtin(I belive) python library.


import threading
import twitch_bot, discord_bot

#remember not to add brackets after the funcion name, I do that way too often.
#Setting `daemon` to `True` will make the Twitch bot stay online if the discord one crashes.
t = threading.Thread(target = twitch_bot.main,daemon=True)
t.start()

#here we are calling the main funtion of the discord bot in the current thread; there is no need to create a new one.
discord_bot.main()