Files
Pac-cogs/videoarchiver/core/events.py
pacnpal 18b5d305f3 Commands will now work even during initialization
Only video processing features are blocked until initialization completes
The on_message event handler now checks if a message is a command first and lets it process normally
2024-11-16 00:39:40 +00:00

98 lines
3.5 KiB
Python

"""Event handlers for VideoArchiver"""
import logging
import discord
import traceback
from typing import TYPE_CHECKING
from ..processor.reactions import REACTIONS, handle_archived_reaction
from .guild import initialize_guild_components, cleanup_guild_components
if TYPE_CHECKING:
from .base import VideoArchiver
logger = logging.getLogger("VideoArchiver")
def setup_events(cog: "VideoArchiver") -> None:
"""Set up event handlers for the cog"""
@cog.bot.event
async def on_guild_join(guild: discord.Guild) -> None:
"""Handle bot joining a new guild"""
if not cog.ready.is_set():
return
try:
await initialize_guild_components(cog, guild.id)
logger.info(f"Initialized components for new guild {guild.id}")
except Exception as e:
logger.error(f"Failed to initialize new guild {guild.id}: {str(e)}")
@cog.bot.event
async def on_guild_remove(guild: discord.Guild) -> None:
"""Handle bot leaving a guild"""
try:
await cleanup_guild_components(cog, guild.id)
except Exception as e:
logger.error(f"Error cleaning up removed guild {guild.id}: {str(e)}")
@cog.bot.event
async def on_message(message: discord.Message) -> None:
"""Handle new messages for video processing"""
# Only block video processing features, not command processing
if message.guild is None or message.author.bot:
return
# Check if this is a command - if so, let it process normally
if message.content.startswith(await cog.bot.get_prefix(message)):
return
# Only process videos if initialization is complete
if not cog.ready.is_set():
logger.debug("Skipping video processing - initialization not complete")
return
try:
await cog.processor.process_message(message)
except Exception as e:
logger.error(
f"Error processing message {message.id}: {traceback.format_exc()}"
)
try:
log_channel = await cog.config_manager.get_channel(
message.guild, "log"
)
if log_channel:
await log_channel.send(
f"Error processing message: {str(e)}\n"
f"Message ID: {message.id}\n"
f"Channel: {message.channel.mention}"
)
except Exception as log_error:
logger.error(f"Failed to log error to guild: {str(log_error)}")
@cog.bot.event
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent) -> None:
"""Handle reactions to messages"""
if payload.user_id == cog.bot.user.id:
return
try:
# Get the channel and message
channel = cog.bot.get_channel(payload.channel_id)
if not channel:
return
message = await channel.fetch_message(payload.message_id)
if not message:
return
# Check if it's the archived reaction
if str(payload.emoji) == REACTIONS["archived"]:
# Only process if database is enabled
if cog.db:
user = cog.bot.get_user(payload.user_id)
await handle_archived_reaction(message, user, cog.db)
except Exception as e:
logger.error(f"Error handling reaction: {e}")