Skip to content

Handle adding reactions to messages from users who have blocked the bot #2580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 29, 2024
16 changes: 16 additions & 0 deletions bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import asyncio
import contextlib
from sys import exception

import aiohttp
from discord.errors import Forbidden
from pydis_core import BotBase
from pydis_core.utils.error_handling import handle_forbidden_from_block
from sentry_sdk import push_scope

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

async def on_error(self, event: str, *args, **kwargs) -> None:
"""Log errors raised in event listeners rather than printing them to stderr."""
e_val = exception()

if isinstance(e_val, Forbidden):
message = args[0] if event == "on_message" else args[1] if event == "on_message_edit" else None

with contextlib.suppress(Forbidden):
# Attempt to handle the error. This reraises the error if's not due to a block,
# in which case the error is suppressed and handled normally. Otherwise, it was
# handled so return.
await handle_forbidden_from_block(e_val, message)
return

self.stats.incr(f"errors.event.{event}")

with push_scope() as scope:
Expand Down
8 changes: 7 additions & 1 deletion bot/exts/backend/error_handler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import copy
import difflib

from discord import Embed, Member
from discord import Embed, Forbidden, Member
from discord.ext.commands import ChannelNotFound, Cog, Context, TextChannelConverter, VoiceChannelConverter, errors
from pydis_core.site_api import ResponseCodeError
from pydis_core.utils.error_handling import handle_forbidden_from_block
from sentry_sdk import push_scope

from bot.bot import Bot
Expand Down Expand Up @@ -98,6 +99,11 @@ async def on_command_error(self, ctx: Context, e: errors.CommandError) -> None:
await ctx.send(f"{e.original} Please wait for it to finish and try again later.")
elif isinstance(e.original, InvalidInfractedUserError):
await ctx.send(f"Cannot infract that user. {e.original.reason}")
elif isinstance(e.original, Forbidden):
try:
await handle_forbidden_from_block(e.original, ctx.message)
except Forbidden:
await self.handle_unexpected_error(ctx, e.original)
else:
await self.handle_unexpected_error(ctx, e.original)
elif isinstance(e, errors.ConversionError):
Expand Down