"""Discord bot lifecycle, class definition, and supervisor.""" import asyncio import json import logging import threading import time from datetime import datetime, timedelta from zoneinfo import ZoneInfo from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.triggers.cron import CronTrigger from discord import ( Forbidden, HTTPException, Intents, LoginFailure, NotFound, PrivilegedIntentsRequired, ) from discord.ext import commands from sqlalchemy.exc import SQLAlchemyError from app.discord.constants import ( BOT_RESTART_DELAY_SECONDS, BOT_RESTART_MAX_DELAY_SECONDS, BOT_RESTART_RESET_SECONDS, CHECK_EMOJI, CROSS_EMOJI, DISCORD_BOT_TOKEN, REMINDER_GRACE_SECONDS, TIMEZONE, USER_CACHE_MAX, ) from app.discord.pending import ( drop_expired, forget_pending, load_pending, save_pending, ) from app.discord.queue import message_queue from app.time_utils import utc_now_naive _logger = logging.getLogger(__name__) bot_instance = None bot_thread = None bot_flask_app = None _stop_bot = threading.Event()