diff options
| author | 2020-07-17 21:34:40 +0200 | |
|---|---|---|
| committer | 2020-07-17 21:34:40 +0200 | |
| commit | d83417432324019b16d0450cdb0c71db9452c52f (patch) | |
| tree | a7bf5208737215510e2fed770cb8cbd052ae3f48 | |
| parent | Cache AllowDenyList data at bot startup. (diff) | |
Add ValidAllowDenyListType converter.
We'll use this to ensure the input is valid when people try to whitelist
or blacklist stuff. It will fetch its data from an Enum maintained on
the site, so that the types of lists we support will only need to be
maintained in a single place, instead of duplicating that data in the
bot and the site.
| -rw-r--r-- | bot/converters.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/bot/converters.py b/bot/converters.py index 406fd0d68..4d2acb910 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -7,7 +7,7 @@ from ssl import CertificateError import dateutil.parser import dateutil.tz import discord -from aiohttp import ClientConnectorError +from aiohttp import ClientConnectorError, ContentTypeError from dateutil.relativedelta import relativedelta from discord.ext.commands import BadArgument, Context, Converter, UserConverter @@ -34,6 +34,32 @@ def allowed_strings(*values, preserve_case: bool = False) -> t.Callable[[str], s return converter +class ValidAllowDenyListType(Converter): + """ + A converter that checks whether the given string is a valid AllowDenyList type. + + Raises `BadArgument` if the argument is not a valid AllowDenyList type, and simply + passes through the given argument otherwise. + """ + + async def convert(self, ctx: Context, list_type: str) -> str: + """Checks whether the given string is a valid AllowDenyList type.""" + try: + valid_types = await ctx.bot.api_client.get('bot/allow_deny_lists/get_types') + except ContentTypeError: + raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.") + + valid_types = [enum for enum, classname in valid_types] + list_type = list_type.upper() + + if list_type not in valid_types: + raise BadArgument( + f"You have provided an invalid AllowDenyList type!\n\n" + f"Please provide one of the following: \n{', '.join(valid_types)}." + ) + return list_type + + class ValidPythonIdentifier(Converter): """ A converter that checks whether the given string is a valid Python identifier. |