diff options
| -rw-r--r-- | bot/exts/filtering/_filters/domain.py | 3 | ||||
| -rw-r--r-- | bot/exts/filtering/_filters/filter.py | 2 | ||||
| -rw-r--r-- | bot/exts/filtering/_filters/invite.py | 7 | ||||
| -rw-r--r-- | bot/exts/filtering/_filters/token.py | 4 | ||||
| -rw-r--r-- | bot/exts/filtering/_ui/filter.py | 4 | ||||
| -rw-r--r-- | bot/exts/filtering/filtering.py | 20 | 
6 files changed, 27 insertions, 13 deletions
| diff --git a/bot/exts/filtering/_filters/domain.py b/bot/exts/filtering/_filters/domain.py index eed2b6721..4976198cd 100644 --- a/bot/exts/filtering/_filters/domain.py +++ b/bot/exts/filtering/_filters/domain.py @@ -3,6 +3,7 @@ from typing import ClassVar, Optional  from urllib.parse import urlparse  import tldextract +from discord.ext.commands import BadArgument  from pydantic import BaseModel  from bot.exts.filtering._filter_context import FilterContext @@ -57,5 +58,5 @@ class DomainFilter(Filter):          """          match = URL_RE.fullmatch(content)          if not match or not match.group(1): -            raise ValueError(f"`{content}` is not a URL.") +            raise BadArgument(f"`{content}` is not a URL.")          return match.group(1) diff --git a/bot/exts/filtering/_filters/filter.py b/bot/exts/filtering/_filters/filter.py index 957957d83..b4a2bfe5e 100644 --- a/bot/exts/filtering/_filters/filter.py +++ b/bot/exts/filtering/_filters/filter.py @@ -53,7 +53,7 @@ class Filter(FieldRequiring):          """          Process the content into a form which will work with the filtering. -        A ValueError should be raised if the content can't be used. +        A BadArgument should be raised if the content can't be used.          """          return content diff --git a/bot/exts/filtering/_filters/invite.py b/bot/exts/filtering/_filters/invite.py index e0f469520..0463b0032 100644 --- a/bot/exts/filtering/_filters/invite.py +++ b/bot/exts/filtering/_filters/invite.py @@ -1,5 +1,6 @@  from botcore.utils.regex import DISCORD_INVITE  from discord import NotFound +from discord.ext.commands import BadArgument  import bot  from bot.exts.filtering._filter_context import FilterContext @@ -32,12 +33,12 @@ class InviteFilter(Filter):          """          match = DISCORD_INVITE.fullmatch(content)          if not match or not match.group("invite"): -            raise ValueError(f"`{content}` is not a valid Discord invite.") +            raise BadArgument(f"`{content}` is not a valid Discord invite.")          invite_code = match.group("invite")          try:              invite = await bot.instance.fetch_invite(invite_code)          except NotFound: -            raise ValueError(f"`{invite_code}` is not a valid Discord invite code.") +            raise BadArgument(f"`{invite_code}` is not a valid Discord invite code.")          if not invite.guild: -            raise ValueError("Did you just try to add a group DM?") +            raise BadArgument("Did you just try to add a group DM?")          return str(invite.guild.id) diff --git a/bot/exts/filtering/_filters/token.py b/bot/exts/filtering/_filters/token.py index a4c646c5a..04e30cb03 100644 --- a/bot/exts/filtering/_filters/token.py +++ b/bot/exts/filtering/_filters/token.py @@ -1,5 +1,7 @@  import re +from discord.ext.commands import BadArgument +  from bot.exts.filtering._filter_context import FilterContext  from bot.exts.filtering._filters.filter import Filter @@ -29,5 +31,5 @@ class TokenFilter(Filter):          try:              re.compile(content)          except re.error as e: -            raise ValueError(str(e)) +            raise BadArgument(str(e))          return content diff --git a/bot/exts/filtering/_ui/filter.py b/bot/exts/filtering/_ui/filter.py index b372cac3e..e6a568ad0 100644 --- a/bot/exts/filtering/_ui/filter.py +++ b/bot/exts/filtering/_ui/filter.py @@ -214,9 +214,9 @@ class FilterEditView(EditBaseView):          except ResponseCodeError as e:              await interaction.message.reply(embed=format_response_error(e))              await interaction.message.edit(view=self) -        except ValueError as e: +        except BadArgument as e:              await interaction.message.reply( -                embed=Embed(colour=discord.Colour.red(), title="Bad Content", description=str(e)) +                embed=Embed(colour=discord.Colour.red(), title="Bad Argument", description=str(e))              )              await interaction.message.edit(view=self)          else: diff --git a/bot/exts/filtering/filtering.py b/bot/exts/filtering/filtering.py index 6bc665973..eb1615b28 100644 --- a/bot/exts/filtering/filtering.py +++ b/bot/exts/filtering/filtering.py @@ -7,6 +7,7 @@ from io import BytesIO  from typing import Literal, Optional, get_type_hints  import discord +from botcore.site_api import ResponseCodeError  from discord import Colour, Embed, HTTPException, Message  from discord.ext import commands  from discord.ext.commands import BadArgument, Cog, Context, has_any_role @@ -25,7 +26,7 @@ from bot.exts.filtering._ui.filter import (      build_filter_repr_dict, description_and_settings_converter, filter_overrides, populate_embed_from_dict  )  from bot.exts.filtering._ui.filter_list import FilterListAddView, FilterListEditView, settings_converter -from bot.exts.filtering._ui.ui import ArgumentCompletionView, DeleteConfirmationView +from bot.exts.filtering._ui.ui import ArgumentCompletionView, DeleteConfirmationView, format_response_error  from bot.exts.filtering._utils import past_tense, starting_value, to_serializable  from bot.log import get_logger  from bot.pagination import LinePaginator @@ -403,9 +404,12 @@ class Filtering(Cog):          patch_func = partial(self._patch_filter, filter_)          if noui: -            await patch_func( -                ctx.message, filter_list, list_type, filter_type, content, description, settings, filter_settings -            ) +            try: +                await patch_func( +                    ctx.message, filter_list, list_type, filter_type, content, description, settings, filter_settings +                ) +            except ResponseCodeError as e: +                await ctx.reply(embed=format_response_error(e))              return          embed = Embed(colour=Colour.blue()) @@ -578,7 +582,11 @@ class Filtering(Cog):          list_type, filter_list = result          settings = settings_converter(self.loaded_settings, settings)          if noui: -            await self._patch_filter_list(ctx.message, filter_list, list_type, settings) +            try: +                await self._patch_filter_list(ctx.message, filter_list, list_type, settings) +            except ResponseCodeError as e: +                await ctx.reply(embed=format_response_error(e)) +            return          embed = Embed(colour=Colour.blue())          embed.set_author(name=f"{filter_list[list_type].label.title()} Filter List") @@ -782,6 +790,8 @@ class Filtering(Cog):                  await self._post_new_filter(                      ctx.message, filter_list, list_type, filter_type, content, description, settings, filter_settings                  ) +            except ResponseCodeError as e: +                await ctx.reply(embed=format_response_error(e))              except ValueError as e:                  raise BadArgument(str(e))              return | 
