Skip to content

Commit f10aea9

Browse files
Handle adding reactions to messages from users who have blocked the bot (#2580)
Co-authored-by: Xithrius <15021300+Xithrius@users.noreply.github.com> Co-authored-by: Xithrius <xithrius@gmail.com>
1 parent 88f020a commit f10aea9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

bot/bot.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import asyncio
2+
import contextlib
3+
from sys import exception
24

35
import aiohttp
6+
from discord.errors import Forbidden
47
from pydis_core import BotBase
8+
from pydis_core.utils.error_handling import handle_forbidden_from_block
59
from sentry_sdk import push_scope
610

711
from bot import constants, exts
@@ -48,6 +52,18 @@ async def setup_hook(self) -> None:
4852

4953
async def on_error(self, event: str, *args, **kwargs) -> None:
5054
"""Log errors raised in event listeners rather than printing them to stderr."""
55+
e_val = exception()
56+
57+
if isinstance(e_val, Forbidden):
58+
message = args[0] if event == "on_message" else args[1] if event == "on_message_edit" else None
59+
60+
with contextlib.suppress(Forbidden):
61+
# Attempt to handle the error. This reraises the error if's not due to a block,
62+
# in which case the error is suppressed and handled normally. Otherwise, it was
63+
# handled so return.
64+
await handle_forbidden_from_block(e_val, message)
65+
return
66+
5167
self.stats.incr(f"errors.event.{event}")
5268

5369
with push_scope() as scope:

bot/exts/backend/error_handler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import copy
22
import difflib
33

4-
from discord import Embed, Member
4+
from discord import Embed, Forbidden, Member
55
from discord.ext.commands import ChannelNotFound, Cog, Context, TextChannelConverter, VoiceChannelConverter, errors
66
from pydis_core.site_api import ResponseCodeError
7+
from pydis_core.utils.error_handling import handle_forbidden_from_block
78
from sentry_sdk import push_scope
89

910
from bot.bot import Bot
@@ -98,6 +99,11 @@ async def on_command_error(self, ctx: Context, e: errors.CommandError) -> None:
9899
await ctx.send(f"{e.original} Please wait for it to finish and try again later.")
99100
elif isinstance(e.original, InvalidInfractedUserError):
100101
await ctx.send(f"Cannot infract that user. {e.original.reason}")
102+
elif isinstance(e.original, Forbidden):
103+
try:
104+
await handle_forbidden_from_block(e.original, ctx.message)
105+
except Forbidden:
106+
await self.handle_unexpected_error(ctx, e.original)
101107
else:
102108
await self.handle_unexpected_error(ctx, e.original)
103109
elif isinstance(e, errors.ConversionError):

0 commit comments

Comments
 (0)