Merge pull request 'Fix logging spam for moved channels and roles' (#4) from pr/log-spam-fix into master

Reviewed-on: #4
This commit is contained in:
slipstream 2025-06-08 20:12:47 -06:00
commit be3a8998e6

View File

@ -369,6 +369,28 @@ class LoggingCog(commands.Cog):
# log.debug(f"Logging disabled for event '{event_key}' in guild {guild_id}")
return enabled
async def _is_recent_audit_log_for_target(
self,
guild: discord.Guild,
action: discord.AuditLogAction,
target_id: int,
max_age: float = 5.0,
) -> bool:
"""Return True if the latest audit log entry matches the target within ``max_age`` seconds."""
try:
async for entry in guild.audit_logs(limit=1, action=action):
if (
entry.target.id == target_id
and (discord.utils.utcnow() - entry.created_at).total_seconds()
<= max_age
):
return True
return False
except discord.Forbidden:
return True
except Exception:
return False
# --- Log Command Group ---
@commands.group(name="log", invoke_without_command=True)
@ -996,6 +1018,14 @@ class LoggingCog(commands.Cog):
changes.append(f"**Position:** `{before.position}` → `{after.position}`")
if changes:
if (
len(changes) == 1
and changes[0].startswith("**Position:")
and not await self._is_recent_audit_log_for_target(
guild, discord.AuditLogAction.role_update, after.id
)
):
return
embed = self._create_log_embed(
title="🔧 Role Updated (Event)",
description=f"Role {after.mention} updated.\n*Audit log may contain updater and specific permission changes.*\n"
@ -1121,6 +1151,14 @@ class LoggingCog(commands.Cog):
changes.append(f"**Category:** {before_cat}{after_cat}")
if changes:
if (
len(changes) == 1
and changes[0].startswith("**Position:")
and not await self._is_recent_audit_log_for_target(
guild, discord.AuditLogAction.channel_update, after.id
)
):
return
embed = self._create_log_embed(
title=f"📝 {ch_type} Channel Updated (Event)",
description=f"Channel {after.mention} updated.\n*Audit log may contain updater and specific permission changes.*\n"