jarvis-tasks/jarvis_tasks/__init__.py
2023-08-27 14:13:20 -06:00

86 lines
2 KiB
Python

"""JARVIS background tasks."""
import asyncio
from interactions import Client, Intents
from jarvis_core.db import connect
from jarvis_core.log import get_logger
from jarvis_tasks import const
from jarvis_tasks.config import load_config
from jarvis_tasks.tasks import (
autokick,
ban,
lock,
lockdown,
reminder,
temprole,
warning,
)
__version__ = const.__version__
logger = None
async def _start() -> None:
"""
Main start function.
Args:
config: Config path
"""
# Load config
config = load_config()
# Connect to database
logger.debug(f"Connecting to database, environ={config.environment.value}")
await connect(**config.mongo.dict(), testing=config.environment.value == "develop")
# Get event loop
loop = asyncio.get_event_loop()
# Login as bot
logger.debug("Logging in bot")
intents = Intents.DEFAULT | Intents.GUILD_MEMBERS
bot = Client(intents=intents, loop=loop)
await bot.login(config.token)
logger.info(f"Logged in as {bot.user.username}#{bot.user.discriminator}")
# tracker = StatTracker()
# Start tasks
try:
logger.debug("Starting tasks")
functions = [
autokick.autokick,
ban.unban,
lock.unlock,
lockdown.lift,
reminder.remind,
temprole.remove,
warning.unwarn,
]
tasks = [loop.create_task(f(bot)) for f in functions] + [
# loop.create_task(tracker.start())
]
for task in tasks:
await task
except KeyboardInterrupt:
for task in tasks:
task.cancel()
def start() -> None:
"""
Start the background tasks.
Args:
config: Config path
"""
global logger, debug
# Set log level
_config = load_config()
logger = get_logger(__name__)
logger.setLevel(_config.log_level)
# Run the main tasks
logger.debug("Starting asyncio")
asyncio.run(_start())