Запускай не фласк, а Quart - тот же фласк, только async.
Бота через Thead, а Quart в основном потоке.
Примерно так реализовано у меня:
# main.py
bot = NoirBot(debug=True)
thread_bot = threading.Thread(target=bot.run, name="bot")
try:
thread_bot.start()
asyncio.run(bot.serve_app())
except Exception as exp:
lprint(f"Connection failed: {exp}", Color.red)
finally:
lprint(f"Stopped", Color.yellow)
exit()
# Bot.py
# Run & Stop funcs
def run(self) -> None:
try:
self.loop.run_until_complete(self.start())
except KeyboardInterrupt:
self.node.disconnect()
self.loop.run_until_complete(self.close())
# cancel all tasks lingering
finally:
self.loop.close()
def start(self):
if self._debug:
return super().start(self._config.get("altsecrets", "token"))
return super().start(self._config.get("secrets", "token"))
# Bot.py
# App
async def serve_app(self):
lprint("Checking routers", Color.blue, worker="APP")
routersLoad(self)
lprint("Loading Quart", Color.blue, worker="APP")
config = hypercorn.Config()
config.bind = ["0.0.0.0:5001"]
config.use_reloader = True
lprint("Done", Color.green, worker="APP")
await serve(self._app, config)