mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-15 14:22:13 +00:00
Tag: 2.0
This commit is contained in:
+44
-24
@@ -2,17 +2,19 @@
|
||||
# trunk-ignore-all(bandit/B311)
|
||||
# pylint: disable=line-too-long
|
||||
# pylint: disable=too-many-lines
|
||||
"""
|
||||
Module of a python bot named Conjurer - used to work on BDSM discord servers.
|
||||
"""
|
||||
import logging
|
||||
"""Discord entrypoint for the Conjurer bot.
|
||||
|
||||
# *=========================================== Standard Library Imports
|
||||
The legacy bootstrap used threads and synchronous calls that made clean
|
||||
shutdowns difficult. We now run everything from a single asyncio event loop
|
||||
with cooperative shutdown signals so the bot can stop gracefully.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import random
|
||||
import threading
|
||||
from logging import handlers
|
||||
|
||||
# *==============Imported libraries
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
@@ -82,22 +84,40 @@ async def on_ready():
|
||||
logger.info("All systems: operational")
|
||||
|
||||
|
||||
# *================================== Run
|
||||
async def _run_comm_subroutine(stop_event: threading.Event) -> None:
|
||||
await asyncio.to_thread(comm_subroutine, stop_event)
|
||||
|
||||
|
||||
async def _run_bot(token: str, shutdown_event: asyncio.Event) -> None:
|
||||
try:
|
||||
await client.start(token, log_handler=None)
|
||||
finally:
|
||||
shutdown_event.set()
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
if not TOKEN:
|
||||
logger.error("Discord token missing – set DISCORD_TOKEN or configure netrc")
|
||||
return
|
||||
|
||||
shutdown_event = asyncio.Event()
|
||||
comm_stop_event = threading.Event()
|
||||
|
||||
comm_task = asyncio.create_task(_run_comm_subroutine(comm_stop_event))
|
||||
bot_task = asyncio.create_task(_run_bot(TOKEN, shutdown_event))
|
||||
|
||||
try:
|
||||
await shutdown_event.wait()
|
||||
except (KeyboardInterrupt, asyncio.CancelledError):
|
||||
logger.info("Shutdown signal received")
|
||||
comm_stop_event.set()
|
||||
await client.close()
|
||||
finally:
|
||||
comm_stop_event.set()
|
||||
if not client.is_closed():
|
||||
await client.close()
|
||||
await asyncio.gather(bot_task, comm_task, return_exceptions=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logger.info("Starting discord bot")
|
||||
threads = []
|
||||
logger.info("Starting discord bot: Creating threads")
|
||||
threads.append(threading.Thread(target=client.run, args=(TOKEN,),kwargs={"log_handler":None}))
|
||||
threads.append(threading.Thread(target=comm_subroutine))
|
||||
logger.info("Starting discord bot: Starting threads")
|
||||
WRK_CNT = 0
|
||||
for worker in threads:
|
||||
WRK_CNT += 1
|
||||
logger.info("Starting discord bot: Starting thread %s", WRK_CNT)
|
||||
worker.start()
|
||||
logger.info("Starting discord bot: Joining threads")
|
||||
WRK_CNT = 0
|
||||
for worker in threads:
|
||||
WRK_CNT += 1
|
||||
logger.info("Starting discord bot: Joining thread %s", WRK_CNT)
|
||||
worker.join()
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user