Add datetime converter and util #54

Open
Daggy1234 wants to merge 3 commits from Daggy1234/patch-1 into 2.0
Showing only changes of commit f4191ebf30 - Show all commits

View File

@ -26,6 +26,7 @@ from __future__ import annotations
import re
import inspect
import datetime
from typing import (
Any,
Dict,
@ -823,7 +824,23 @@ class PartialEmojiConverter(Converter[discord.PartialEmoji]):
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]):
"""Converts to a :class:`~discord.GuildSticker`.