aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Chris Lovering <[email protected]>2023-04-11 16:34:18 +0100
committerGravatar Chris Lovering <[email protected]>2023-04-11 16:55:15 +0100
commit2a3ed04cad3dd917d1573a9c8448d6d5e8f99723 (patch)
treed156f9d7c8dbdc89642e161105766cd3644b931f
parentAdd a git-blame-ignore-revs file (diff)
Use commands.Converter subclass for d.py converters
This is required as | is not supported between a function and NoneType
-rw-r--r--bot/exts/filtering/_filter_lists/__init__.py4
-rw-r--r--bot/exts/filtering/_filter_lists/filter_list.py16
-rw-r--r--bot/exts/filtering/_settings_types/validations/bypass_roles.py4
-rw-r--r--bot/exts/filtering/_ui/ui.py8
-rw-r--r--bot/exts/filtering/filtering.py19
5 files changed, 28 insertions, 23 deletions
diff --git a/bot/exts/filtering/_filter_lists/__init__.py b/bot/exts/filtering/_filter_lists/__init__.py
index 82e0452f9..1273e5588 100644
--- a/bot/exts/filtering/_filter_lists/__init__.py
+++ b/bot/exts/filtering/_filter_lists/__init__.py
@@ -1,9 +1,9 @@
from os.path import dirname
-from bot.exts.filtering._filter_lists.filter_list import FilterList, ListType, list_type_converter
+from bot.exts.filtering._filter_lists.filter_list import FilterList, ListType, ListTypeConverter
from bot.exts.filtering._utils import subclasses_in_package
filter_list_types = subclasses_in_package(dirname(__file__), f"{__name__}.", FilterList)
filter_list_types = {filter_list.name: filter_list for filter_list in filter_list_types}
-__all__ = [filter_list_types, FilterList, ListType, list_type_converter]
+__all__ = [filter_list_types, FilterList, ListType, ListTypeConverter]
diff --git a/bot/exts/filtering/_filter_lists/filter_list.py b/bot/exts/filtering/_filter_lists/filter_list.py
index 3fdc4fb20..e5b6b2a65 100644
--- a/bot/exts/filtering/_filter_lists/filter_list.py
+++ b/bot/exts/filtering/_filter_lists/filter_list.py
@@ -9,7 +9,7 @@ from functools import reduce
from typing import Any
import arrow
-from discord.ext.commands import BadArgument
+from discord.ext.commands import BadArgument, Context, Converter
from bot.exts.filtering._filter_context import Event, FilterContext
from bot.exts.filtering._filters.filter import Filter, UniqueFilter
@@ -37,13 +37,15 @@ aliases = (
)
-def list_type_converter(argument: str) -> ListType:
+class ListTypeConverter(Converter):
"""A converter to get the appropriate list type."""
- argument = argument.lower()
- for list_type, list_aliases in aliases:
- if argument in list_aliases or argument in map(past_tense, list_aliases):
- return list_type
- raise BadArgument(f"No matching list type found for {argument!r}.")
+
+ async def convert(self, ctx: Context, argument: str) -> ListType:
+ argument = argument.lower()
+ for list_type, list_aliases in aliases:
+ if argument in list_aliases or argument in map(past_tense, list_aliases):
+ return list_type
+ raise BadArgument(f"No matching list type found for {argument!r}.")
# AtomicList and its subclasses must have eq=False, otherwise the dataclass deco will replace the hash function.
diff --git a/bot/exts/filtering/_settings_types/validations/bypass_roles.py b/bot/exts/filtering/_settings_types/validations/bypass_roles.py
index 7427b10c7..50fb1e650 100644
--- a/bot/exts/filtering/_settings_types/validations/bypass_roles.py
+++ b/bot/exts/filtering/_settings_types/validations/bypass_roles.py
@@ -1,4 +1,4 @@
-from typing import ClassVar
+from typing import ClassVar, Union
from discord import Member
@@ -12,7 +12,7 @@ class RoleBypass(ValidationEntry):
name: ClassVar[str] = "bypass_roles"
description: ClassVar[str] = "A list of role IDs or role names. Users with these roles will not trigger the filter."
- bypass_roles: set[int | str]
+ bypass_roles: set[Union[int, str]] # noqa: UP007
def triggers_on(self, ctx: FilterContext) -> bool:
"""Return whether the filter should be triggered on this user given their roles."""
diff --git a/bot/exts/filtering/_ui/ui.py b/bot/exts/filtering/_ui/ui.py
index b2de71b9c..4e1e61148 100644
--- a/bot/exts/filtering/_ui/ui.py
+++ b/bot/exts/filtering/_ui/ui.py
@@ -9,7 +9,7 @@ from typing import Any, TypeVar, get_origin
import discord
from discord import Embed, Interaction
-from discord.ext.commands import Context
+from discord.ext.commands import Context, Converter
from discord.ui.select import MISSING as SELECT_MISSING, SelectOption
from discord.utils import escape_markdown
from pydis_core.site_api import ResponseCodeError
@@ -182,7 +182,7 @@ class ArgumentCompletionSelect(discord.ui.Select):
arg_name: str,
options: list[str],
position: int,
- converter: Callable | None = None
+ converter: Converter | None = None
):
super().__init__(
placeholder=f"Select a value for {arg_name!r}",
@@ -198,7 +198,7 @@ class ArgumentCompletionSelect(discord.ui.Select):
await interaction.response.defer()
value = interaction.data["values"][0]
if self.converter:
- value = self.converter(value)
+ value = await self.converter().convert(self.ctx, value)
args = self.args.copy() # This makes the view reusable.
args.insert(self.position, value)
log.trace(f"Argument filled with the value {value}. Re-invoking command")
@@ -215,7 +215,7 @@ class ArgumentCompletionView(discord.ui.View):
arg_name: str,
options: list[str],
position: int,
- converter: Callable | None = None
+ converter: Converter | None = None
):
super().__init__()
log.trace(f"The {arg_name} argument was designated missing in the invocation {ctx.view.buffer!r}")
diff --git a/bot/exts/filtering/filtering.py b/bot/exts/filtering/filtering.py
index c3f1060d8..8e7ba7476 100644
--- a/bot/exts/filtering/filtering.py
+++ b/bot/exts/filtering/filtering.py
@@ -25,7 +25,7 @@ from bot.bot import Bot
from bot.constants import Channels, Guild, MODERATION_ROLES, Roles
from bot.exts.backend.branding._repository import HEADERS, PARAMS
from bot.exts.filtering._filter_context import Event, FilterContext
-from bot.exts.filtering._filter_lists import FilterList, ListType, filter_list_types, list_type_converter
+from bot.exts.filtering._filter_lists import FilterList, ListType, ListTypeConverter, filter_list_types
from bot.exts.filtering._filter_lists.filter_list import AtomicList
from bot.exts.filtering._filters.filter import Filter, UniqueFilter
from bot.exts.filtering._settings import ActionSettings
@@ -414,7 +414,10 @@ class Filtering(Cog):
@filter.command(name="list", aliases=("get",))
async def f_list(
- self, ctx: Context, list_type: list_type_converter | None = None, list_name: str | None = None
+ self,
+ ctx: Context,
+ list_type: ListTypeConverter | None = None,
+ list_name: str | None = None,
) -> None:
"""List the contents of a specified list of filters."""
result = await self._resolve_list_type_and_name(ctx, list_type, list_name)
@@ -449,7 +452,7 @@ class Filtering(Cog):
self,
ctx: Context,
noui: Literal["noui"] | None,
- list_type: list_type_converter | None,
+ list_type: ListTypeConverter | None,
list_name: str | None,
content: str,
*,
@@ -727,7 +730,7 @@ class Filtering(Cog):
@filterlist.command(name="describe", aliases=("explain", "manual", "id"))
async def fl_describe(
- self, ctx: Context, list_type: list_type_converter | None = None, list_name: str | None = None
+ self, ctx: Context, list_type: ListTypeConverter | None = None, list_name: str | None = None
) -> None:
"""Show a description of the specified filter list, or a list of possible values if no values are provided."""
if not list_type and not list_name:
@@ -758,7 +761,7 @@ class Filtering(Cog):
@filterlist.command(name="add", aliases=("a",))
@has_any_role(Roles.admins)
- async def fl_add(self, ctx: Context, list_type: list_type_converter, list_name: str) -> None:
+ async def fl_add(self, ctx: Context, list_type: ListTypeConverter, list_name: str) -> None:
"""Add a new filter list."""
# Check if there's an implementation.
if list_name.lower() not in filter_list_types:
@@ -796,7 +799,7 @@ class Filtering(Cog):
self,
ctx: Context,
noui: Literal["noui"] | None,
- list_type: list_type_converter | None = None,
+ list_type: ListTypeConverter | None = None,
list_name: str | None = None,
*,
settings: str | None
@@ -839,7 +842,7 @@ class Filtering(Cog):
@filterlist.command(name="delete", aliases=("remove",))
@has_any_role(Roles.admins)
async def fl_delete(
- self, ctx: Context, list_type: list_type_converter | None = None, list_name: str | None = None
+ self, ctx: Context, list_type: ListTypeConverter | None = None, list_name: str | None = None
) -> None:
"""Remove the filter list and all of its filters from the database."""
async def delete_list() -> None:
@@ -1024,7 +1027,7 @@ class Filtering(Cog):
await ctx.send(
"The **list_type** argument is unspecified. Please pick a value from the options below:",
view=ArgumentCompletionView(
- ctx, args, "list_type", [option.name for option in ListType], 0, list_type_converter
+ ctx, args, "list_type", [option.name for option in ListType], 0, ListTypeConverter
)
)
return None