Compare commits
1 Commits
Daggy1234/
...
Astrea49/c
Author | SHA1 | Date | |
---|---|---|---|
4baf615cd4 |
@ -26,7 +26,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import inspect
|
import inspect
|
||||||
import datetime
|
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Dict,
|
Dict,
|
||||||
@ -598,7 +597,8 @@ class ColourConverter(Converter[discord.Colour]):
|
|||||||
Add an alias named ColorConverter
|
Add an alias named ColorConverter
|
||||||
|
|
||||||
The following formats are accepted:
|
The following formats are accepted:
|
||||||
|
|
||||||
|
- ``<hex>``
|
||||||
- ``0x<hex>``
|
- ``0x<hex>``
|
||||||
- ``#<hex>``
|
- ``#<hex>``
|
||||||
- ``0x#<hex>``
|
- ``0x#<hex>``
|
||||||
@ -669,7 +669,9 @@ class ColourConverter(Converter[discord.Colour]):
|
|||||||
|
|
||||||
arg = arg.replace(' ', '_')
|
arg = arg.replace(' ', '_')
|
||||||
method = getattr(discord.Colour, arg, None)
|
method = getattr(discord.Colour, arg, None)
|
||||||
if arg.startswith('from_') or method is None or not inspect.ismethod(method):
|
if method is None:
|
||||||
|
return self.parse_hex_number(argument)
|
||||||
|
elif arg.startswith('from_') or not inspect.ismethod(method):
|
||||||
raise BadColourArgument(arg)
|
raise BadColourArgument(arg)
|
||||||
return method()
|
return method()
|
||||||
|
|
||||||
@ -824,23 +826,7 @@ class PartialEmojiConverter(Converter[discord.PartialEmoji]):
|
|||||||
|
|
||||||
raise PartialEmojiConversionFailure(argument)
|
raise PartialEmojiConversionFailure(argument)
|
||||||
|
|
||||||
class FormattedDatetimeConverter(Converter[Tuple[datetime.datetime, Optional[str]]]):
|
|
||||||
|
|
||||||
"""Converts a discord style datetime to a :class:`datetime.datetime`.
|
|
||||||
Also returns the flag used to format the timestamp if present.
|
|
||||||
This is done by extracting the epoch from the string.
|
|
||||||
.. versionadd:: 2.0
|
|
||||||
Raise :exc:`.FormattedDatetimeConversionFailure` instead of generic :exc:`.BadArgument`
|
|
||||||
"""
|
|
||||||
|
|
||||||
async def convert(self, ctx: Context, argument: str) -> Tuple[datetime.datetime, Optional[str]]:
|
|
||||||
|
|
||||||
match = discord.utils.resolve_formatted_dt(argument)
|
|
||||||
if match:
|
|
||||||
return match
|
|
||||||
|
|
||||||
raise FormattedDatetimeConversionFailure(argument)
|
|
||||||
|
|
||||||
class GuildStickerConverter(IDConverter[discord.GuildSticker]):
|
class GuildStickerConverter(IDConverter[discord.GuildSticker]):
|
||||||
"""Converts to a :class:`~discord.GuildSticker`.
|
"""Converts to a :class:`~discord.GuildSticker`.
|
||||||
|
|
||||||
|
@ -98,7 +98,6 @@ __all__ = (
|
|||||||
'MissingFlagArgument',
|
'MissingFlagArgument',
|
||||||
'TooManyFlags',
|
'TooManyFlags',
|
||||||
'MissingRequiredFlag',
|
'MissingRequiredFlag',
|
||||||
'FormattedDatetimeConversionFailure',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
class CommandError(DiscordException):
|
class CommandError(DiscordException):
|
||||||
@ -996,16 +995,3 @@ class MissingFlagArgument(FlagError):
|
|||||||
def __init__(self, flag: Flag) -> None:
|
def __init__(self, flag: Flag) -> None:
|
||||||
self.flag: Flag = flag
|
self.flag: Flag = flag
|
||||||
super().__init__(f'Flag {flag.name!r} does not have an argument')
|
super().__init__(f'Flag {flag.name!r} does not have an argument')
|
||||||
|
|
||||||
class FormattedDatetimeConversionFailure(BadArgument):
|
|
||||||
"""Exception raised when the timestamp provided does not match the correct
|
|
||||||
format.
|
|
||||||
This inherits from :exc:`BadArgument`
|
|
||||||
Attributes
|
|
||||||
-----------
|
|
||||||
argument: :class:`str`
|
|
||||||
The emoji supplied by the caller that did not match the regex
|
|
||||||
"""
|
|
||||||
def __init__(self, argument):
|
|
||||||
self.argument = argument
|
|
||||||
super().__init__(f'Timestamp "{argument}" wasn\'t formatted correctly.')
|
|
||||||
|
@ -1021,24 +1021,3 @@ def raise_expected_coro(coro, error: str)-> TypeError:
|
|||||||
if not asyncio.iscoroutinefunction(coro):
|
if not asyncio.iscoroutinefunction(coro):
|
||||||
raise TypeError(error)
|
raise TypeError(error)
|
||||||
return coro
|
return coro
|
||||||
|
|
||||||
def resolve_formatted_dt(formatted_dt: str) -> Optional[Tuple[datetime.datetime, Optional[str]]]:
|
|
||||||
"""A helper function that can take discord formatted datetimes like those produced by :meth:`~discord.utils.format_dt`
|
|
||||||
and return the relevant datetime.
|
|
||||||
If unable to parse a datetime from formatted_dt, it will return None.
|
|
||||||
.. versionadded:: 2.0
|
|
||||||
Parameters
|
|
||||||
-----------
|
|
||||||
formatted_dt: :class:`str`
|
|
||||||
The formatted datetime string.
|
|
||||||
Returns
|
|
||||||
--------
|
|
||||||
Optional[Tuple[:class:`datetime.datetime`, Optional[:class:`str`]]
|
|
||||||
A tuple containing the datetime and the flag if present. Otherwise None.
|
|
||||||
"""
|
|
||||||
match = re.match(r'<t:(-?[0-9]+)(:[tTdDfFR])?>$', formatted_dt)
|
|
||||||
if match:
|
|
||||||
try:
|
|
||||||
return datetime.datetime.fromtimestamp(int(match.group(1)), tz=datetime.timezone.utc), match.group(2)
|
|
||||||
except (ValueError, OverflowError, OSError):
|
|
||||||
return
|
|
||||||
|
Reference in New Issue
Block a user