Skip to content

Commit da9a9ba

Browse files
committed
fix: robust channel deletion handling, skip audit log attribution if permission is missing
1 parent 337123f commit da9a9ba

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

bot.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,12 +1773,30 @@ async def on_guild_channel_delete(self, channel):
17731773
await self.config.update()
17741774
return
17751775

1776-
audit_logs = self.modmail_guild.audit_logs(limit=10, action=discord.AuditLogAction.channel_delete)
1777-
found_entry = False
1778-
async for entry in audit_logs:
1779-
if int(entry.target.id) == channel.id:
1780-
found_entry = True
1781-
break
1776+
# Attempt to attribute channel deletion to a moderator via audit logs.
1777+
# This requires the "View Audit Log" permission; if missing, skip silently.
1778+
if not self.modmail_guild.me.guild_permissions.view_audit_log:
1779+
logger.debug(
1780+
"Skipping audit log lookup for deleted channel %d: missing view_audit_log permission.",
1781+
channel.id,
1782+
)
1783+
return
1784+
1785+
try:
1786+
audit_logs = self.modmail_guild.audit_logs(limit=10, action=discord.AuditLogAction.channel_delete)
1787+
found_entry = False
1788+
async for entry in audit_logs:
1789+
if int(entry.target.id) == channel.id:
1790+
found_entry = True
1791+
break
1792+
except discord.Forbidden:
1793+
logger.debug(
1794+
"Forbidden when fetching audit logs for deleted channel %d (missing permission).", channel.id
1795+
)
1796+
return
1797+
except discord.HTTPException as e:
1798+
logger.debug("HTTPException when fetching audit logs for deleted channel %d: %s", channel.id, e)
1799+
return
17821800

17831801
if not found_entry:
17841802
logger.debug("Cannot find the audit log entry for channel delete of %d.", channel.id)

0 commit comments

Comments
 (0)