mirror of
https://github.com/pacnpal/Pac-cogs.git
synced 2025-12-20 02:41:06 -05:00
Added proper command validation in the on_message event handler to prevent interference with command processing
Added missing asyncio import in events.py Ensured all long-running operations are handled in background tasks Fixed task initialization and management in base.py
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import logging
|
||||
import discord
|
||||
import asyncio
|
||||
import traceback
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
@@ -16,7 +17,7 @@ logger = logging.getLogger("VideoArchiver")
|
||||
def setup_events(cog: "VideoArchiver") -> None:
|
||||
"""Set up event handlers for the cog"""
|
||||
|
||||
@cog.bot.event
|
||||
@cog.listener()
|
||||
async def on_guild_join(guild: discord.Guild) -> None:
|
||||
"""Handle bot joining a new guild"""
|
||||
if not cog.ready.is_set():
|
||||
@@ -28,7 +29,7 @@ def setup_events(cog: "VideoArchiver") -> None:
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize new guild {guild.id}: {str(e)}")
|
||||
|
||||
@cog.bot.event
|
||||
@cog.listener()
|
||||
async def on_guild_remove(guild: discord.Guild) -> None:
|
||||
"""Handle bot leaving a guild"""
|
||||
try:
|
||||
@@ -36,47 +37,22 @@ def setup_events(cog: "VideoArchiver") -> None:
|
||||
except Exception as e:
|
||||
logger.error(f"Error cleaning up removed guild {guild.id}: {str(e)}")
|
||||
|
||||
@cog.bot.event
|
||||
@cog.listener()
|
||||
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:
|
||||
# Skip if not ready or if message is from DM/bot
|
||||
if not cog.ready.is_set() or message.guild is None or message.author.bot:
|
||||
return
|
||||
|
||||
# Check if this is a command by checking against all possible prefixes
|
||||
prefixes = await cog.bot.get_prefix(message)
|
||||
if isinstance(prefixes, str):
|
||||
prefixes = [prefixes]
|
||||
|
||||
# Check if message starts with any of the prefixes
|
||||
if any(message.content.startswith(prefix) for prefix in prefixes):
|
||||
# Skip if message is a command
|
||||
ctx = await cog.bot.get_context(message)
|
||||
if ctx.valid:
|
||||
return
|
||||
|
||||
# Only process videos if initialization is complete
|
||||
if not cog.ready.is_set():
|
||||
logger.debug("Skipping video processing - initialization not complete")
|
||||
return
|
||||
# Process message in background task to avoid blocking
|
||||
asyncio.create_task(process_message_background(cog, message))
|
||||
|
||||
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
|
||||
@cog.listener()
|
||||
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent) -> None:
|
||||
"""Handle reactions to messages"""
|
||||
if payload.user_id == cog.bot.user.id:
|
||||
@@ -96,7 +72,29 @@ def setup_events(cog: "VideoArchiver") -> None:
|
||||
# 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)
|
||||
# Process reaction in background task
|
||||
asyncio.create_task(handle_archived_reaction(message, user, cog.db))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error handling reaction: {e}")
|
||||
|
||||
async def process_message_background(cog: "VideoArchiver", message: discord.Message) -> None:
|
||||
"""Process message in background to avoid blocking"""
|
||||
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)}")
|
||||
|
||||
Reference in New Issue
Block a user