Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IdConverter for bare ID conversion #84

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions docs/ext/commands/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,8 @@ MemberConverterError
~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.MemberConverterError
:members:

IdConverterError
~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.IdConverterError
:members:
17 changes: 15 additions & 2 deletions revolt/ext/commands/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
import re
from typing import TYPE_CHECKING, Annotated, TypeVar

import ulid
from revolt import Category, Channel, Member, User, utils

from .context import Context
from .errors import (BadBoolArgument, CategoryConverterError,
ChannelConverterError, MemberConverterError, ServerOnly,
UserConverterError)
UserConverterError, IdConverterError)

if TYPE_CHECKING:
from .client import CommandsClient

T = TypeVar("T")

__all__: tuple[str, ...] = ("bool_converter", "category_converter", "channel_converter", "user_converter", "member_converter", "IntConverter", "BoolConverter", "CategoryConverter", "UserConverter", "MemberConverter", "ChannelConverter", "Greedy")
__all__: tuple[str, ...] = ("id_converter", "int_converter", "bool_converter", "category_converter", "channel_converter", "user_converter", "member_converter", "IdConverter", "IntConverter", "BoolConverter", "CategoryConverter", "UserConverter", "MemberConverter", "ChannelConverter", "Greedy")

channel_regex: re.Pattern[str] = re.compile("<#([A-z0-9]{26})>")
user_regex: re.Pattern[str] = re.compile("<@([A-z0-9]{26})>")
Expand Down Expand Up @@ -116,6 +117,18 @@ 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]) -> utils.Ulid:
if len(arg) != 26:
raise IdConverterError("An ID was not provided.")

try:
ulid.parse(arg) # validate
except Exception as err:
raise IdConverterError("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]
CategoryConverter = Annotated[Category, category_converter]
Expand Down
4 changes: 4 additions & 0 deletions revolt/ext/commands/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"BadBoolArgument",
"CategoryConverterError",
"ChannelConverterError",
"IdConverterError",
"UserConverterError",
"MemberConverterError",
"MissingSetup",
Expand Down Expand Up @@ -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):
Expand Down