Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions bot/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,6 @@
RE_USER_MENTION = re.compile(r"<@!?([0-9]+)>$")


def allowed_strings(*values, preserve_case: bool = False) -> t.Callable[[str], str]:
"""
Return a converter which only allows arguments equal to one of the given values.

Unless preserve_case is True, the argument is converted to lowercase. All values are then
expected to have already been given in lowercase too.
"""
def converter(arg: str) -> str:
if not preserve_case:
arg = arg.lower()

if arg not in values:
raise BadArgument(f"Only the following values are allowed:\n```{', '.join(values)}```")
else:
return arg

return converter


class ValidDiscordServerInvite(Converter):
"""
A converter that validates whether a given string is a valid Discord server invite.
Expand Down
6 changes: 3 additions & 3 deletions bot/exts/info/doc/_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections import defaultdict
from contextlib import suppress
from types import SimpleNamespace
from typing import Dict, NamedTuple, Optional, Tuple, Union
from typing import Dict, Literal, NamedTuple, Optional, Tuple, Union

import aiohttp
import discord
Expand All @@ -15,7 +15,7 @@
from bot.api import ResponseCodeError
from bot.bot import Bot
from bot.constants import MODERATION_ROLES, RedirectOutput
from bot.converters import Inventory, PackageName, ValidURL, allowed_strings
from bot.converters import Inventory, PackageName, ValidURL
from bot.log import get_logger
from bot.pagination import LinePaginator
from bot.utils import scheduling
Expand Down Expand Up @@ -460,7 +460,7 @@ async def refresh_command(self, ctx: commands.Context) -> None:
async def clear_cache_command(
self,
ctx: commands.Context,
package_name: Union[PackageName, allowed_strings("*")] # noqa: F722
package_name: Union[PackageName, Literal["*"]]
) -> None:
"""Clear the persistent redis cache for `package`."""
if await doc_cache.delete(package_name):
Expand Down
6 changes: 3 additions & 3 deletions bot/exts/moderation/infraction/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from bot import constants
from bot.bot import Bot
from bot.converters import Expiry, Infraction, MemberOrUser, Snowflake, UnambiguousUser, allowed_strings
from bot.converters import Expiry, Infraction, MemberOrUser, Snowflake, UnambiguousUser
from bot.errors import InvalidInfraction
from bot.exts.moderation.infraction.infractions import Infractions
from bot.exts.moderation.modlog import ModLog
Expand Down Expand Up @@ -64,7 +64,7 @@ async def infraction_append(
self,
ctx: Context,
infraction: Infraction,
duration: t.Union[Expiry, allowed_strings("p", "permanent"), None], # noqa: F821
duration: t.Union[Expiry, t.Literal["p", "permanent"], None],
*,
reason: str = None
) -> None:
Expand Down Expand Up @@ -103,7 +103,7 @@ async def infraction_edit(
self,
ctx: Context,
infraction: Infraction,
duration: t.Union[Expiry, allowed_strings("p", "permanent"), None], # noqa: F821
duration: t.Union[Expiry, t.Literal["p", "permanent"], None],
*,
reason: str = None
) -> None:
Expand Down
5 changes: 2 additions & 3 deletions bot/exts/moderation/metabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
from datetime import timedelta
from io import StringIO
from typing import Dict, List, Optional
from typing import Dict, List, Literal, Optional

import arrow
from aiohttp.client_exceptions import ClientResponseError
Expand All @@ -12,7 +12,6 @@

from bot.bot import Bot
from bot.constants import Metabase as MetabaseConfig, Roles
from bot.converters import allowed_strings
from bot.log import get_logger
from bot.utils import scheduling, send_to_paste_service
from bot.utils.channel import is_mod_channel
Expand Down Expand Up @@ -110,7 +109,7 @@ async def metabase_extract(
self,
ctx: Context,
question_id: int,
extension: allowed_strings("csv", "json") = "csv"
extension: Literal["csv", "json"] = "csv"
) -> None:
"""
Extract data from a metabase question.
Expand Down