From 902ca3dae53b5f033508bf146e132d0cbcaf3c87 Mon Sep 17 00:00:00 2001 From: IAmTomahawkx Date: Sun, 27 Oct 2024 11:45:17 -0700 Subject: [PATCH 1/4] Add IdConverter from ULIDs --- revolt/ext/commands/converters.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/revolt/ext/commands/converters.py b/revolt/ext/commands/converters.py index 88342d3..98accd1 100755 --- a/revolt/ext/commands/converters.py +++ b/revolt/ext/commands/converters.py @@ -3,6 +3,7 @@ import re from typing import TYPE_CHECKING, Annotated, TypeVar +import ulid from revolt import Category, Channel, Member, User, utils from .context import Context @@ -13,7 +14,7 @@ if TYPE_CHECKING: from .client import CommandsClient -__all__: tuple[str, ...] = ("bool_converter", "category_converter", "channel_converter", "user_converter", "member_converter", "IntConverter", "BoolConverter", "CategoryConverter", "UserConverter", "MemberConverter", "ChannelConverter") +__all__: tuple[str, ...] = ("id_converter", "int_converter", "bool_converter", "category_converter", "channel_converter", "user_converter", "member_converter", "IdConverter", "IntConverter", "BoolConverter", "CategoryConverter", "UserConverter", "MemberConverter", "ChannelConverter") channel_regex: re.Pattern[str] = re.compile("<#([A-z0-9]{26})>") user_regex: re.Pattern[str] = re.compile("<@([A-z0-9]{26})>") @@ -114,6 +115,16 @@ def member_converter(arg: str, context: Context[ClientT]) -> Member: def int_converter(arg: str, context: Context[ClientT]) -> int: return int(arg) +def id_converter(arg: str, context: Context[ClientT]) -> ulid.ULID: + if len(arg) != 26: + raise ValueError("An ID was not provided.") + + try: + return ulid.parse(arg) + except Exception as err: + raise ValueError("An invalid ID was provided.") from err + +IdConverter = Annotated[ulid.ULID, id_converter] IntConverter = Annotated[int, int_converter] BoolConverter = Annotated[bool, bool_converter] CategoryConverter = Annotated[Category, category_converter] From 8feaadd34ba9094642451da34a8ca148ec8fae59 Mon Sep 17 00:00:00 2001 From: IAmTomahawkx Date: Sun, 27 Oct 2024 17:17:55 -0700 Subject: [PATCH 2/4] swap to internal Ulid objects --- revolt/ext/commands/converters.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/revolt/ext/commands/converters.py b/revolt/ext/commands/converters.py index 91d3ff0..9b391b4 100755 --- a/revolt/ext/commands/converters.py +++ b/revolt/ext/commands/converters.py @@ -117,15 +117,17 @@ def member_converter(arg: str, context: Context[ClientT]) -> Member: def int_converter(arg: str, context: Context[ClientT]) -> int: return int(arg) -def id_converter(arg: str, context: Context[ClientT]) -> ulid.ULID: +def id_converter(arg: str, context: Context[ClientT]) -> utils.Ulid: if len(arg) != 26: raise ValueError("An ID was not provided.") try: - return ulid.parse(arg) + ulid.parse(arg) # validate except Exception as err: raise ValueError("An invalid ID was provided.") from err + return utils.Object(arg) + IdConverter = Annotated[ulid.ULID, id_converter] IntConverter = Annotated[int, int_converter] BoolConverter = Annotated[bool, bool_converter] From a742a38186d229939a245c78c21b46751d37511b Mon Sep 17 00:00:00 2001 From: IAmTomahawkx Date: Sat, 9 Nov 2024 18:01:31 -0800 Subject: [PATCH 3/4] add custom error to id converter --- revolt/ext/commands/converters.py | 6 +++--- revolt/ext/commands/errors.py | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/revolt/ext/commands/converters.py b/revolt/ext/commands/converters.py index 9b391b4..14beea4 100755 --- a/revolt/ext/commands/converters.py +++ b/revolt/ext/commands/converters.py @@ -9,7 +9,7 @@ from .context import Context from .errors import (BadBoolArgument, CategoryConverterError, ChannelConverterError, MemberConverterError, ServerOnly, - UserConverterError) + UserConverterError, IdConverterError) if TYPE_CHECKING: from .client import CommandsClient @@ -119,12 +119,12 @@ def int_converter(arg: str, context: Context[ClientT]) -> int: def id_converter(arg: str, context: Context[ClientT]) -> utils.Ulid: if len(arg) != 26: - raise ValueError("An ID was not provided.") + raise IdConverterError("An ID was not provided.") try: ulid.parse(arg) # validate except Exception as err: - raise ValueError("An invalid ID was provided.") from err + raise IdConverterError("An invalid ID was provided.") from err return utils.Object(arg) diff --git a/revolt/ext/commands/errors.py b/revolt/ext/commands/errors.py index 173d358..a2b72fc 100755 --- a/revolt/ext/commands/errors.py +++ b/revolt/ext/commands/errors.py @@ -13,6 +13,7 @@ "BadBoolArgument", "CategoryConverterError", "ChannelConverterError", + "IdConverterError", "UserConverterError", "MemberConverterError", "MissingSetup", @@ -71,6 +72,9 @@ class InvalidLiteralArgument(ConverterError): class BadBoolArgument(ConverterError): """Raised when the bool converter fails""" +class IdConverterError(ConverterError): + """Raised when the ID converter fails""" + class CategoryConverterError(ConverterError): """Raised when the Category conveter fails""" def __init__(self, argument: str): From 175052699276c4170a4edcb835d7ad71ac6a4166 Mon Sep 17 00:00:00 2001 From: IAmTomahawkx Date: Sat, 9 Nov 2024 18:03:25 -0800 Subject: [PATCH 4/4] add it to the docs --- docs/ext/commands/api.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/ext/commands/api.rst b/docs/ext/commands/api.rst index 1f473a0..98e1c66 100755 --- a/docs/ext/commands/api.rst +++ b/docs/ext/commands/api.rst @@ -108,3 +108,8 @@ MemberConverterError ~~~~~~~~~~~~~~~~~~~~~ .. autoexception:: revolt.ext.commands.MemberConverterError :members: + +IdConverterError +~~~~~~~~~~~~~~~~~~~~~ +.. autoexception:: revolt.ext.commands.IdConverterError + :members: \ No newline at end of file