[commands] Add ThreadConverter
This commit is contained in:
parent
c1c6457598
commit
5a7cfb3ce6
@ -71,6 +71,7 @@ __all__ = (
|
|||||||
'CategoryChannelConverter',
|
'CategoryChannelConverter',
|
||||||
'IDConverter',
|
'IDConverter',
|
||||||
'StoreChannelConverter',
|
'StoreChannelConverter',
|
||||||
|
'ThreadConverter',
|
||||||
'GuildChannelConverter',
|
'GuildChannelConverter',
|
||||||
'clean_content',
|
'clean_content',
|
||||||
'Greedy',
|
'Greedy',
|
||||||
@ -426,6 +427,28 @@ class GuildChannelConverter(IDConverter[discord.abc.GuildChannel]):
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _resolve_thread(ctx: Context, argument: str, attribute: str, type: Type[CT]) -> CT:
|
||||||
|
bot = ctx.bot
|
||||||
|
|
||||||
|
match = IDConverter._get_id_match(argument) or re.match(r'<#([0-9]{15,20})>$', argument)
|
||||||
|
result = None
|
||||||
|
guild = ctx.guild
|
||||||
|
|
||||||
|
if match is None:
|
||||||
|
# not a mention
|
||||||
|
if guild:
|
||||||
|
iterable: Iterable[CT] = getattr(guild, attribute)
|
||||||
|
result: Optional[CT] = discord.utils.get(iterable, name=argument)
|
||||||
|
else:
|
||||||
|
thread_id = int(match.group(1))
|
||||||
|
if guild:
|
||||||
|
result = guild.get_thread(thread_id)
|
||||||
|
|
||||||
|
if not result or not isinstance(result, type):
|
||||||
|
raise ThreadNotFound(argument)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
class TextChannelConverter(IDConverter[discord.TextChannel]):
|
class TextChannelConverter(IDConverter[discord.TextChannel]):
|
||||||
"""Converts to a :class:`~discord.TextChannel`.
|
"""Converts to a :class:`~discord.TextChannel`.
|
||||||
@ -524,6 +547,22 @@ class StoreChannelConverter(IDConverter[discord.StoreChannel]):
|
|||||||
async def convert(self, ctx: Context, argument: str) -> discord.StoreChannel:
|
async def convert(self, ctx: Context, argument: str) -> discord.StoreChannel:
|
||||||
return GuildChannelConverter._resolve_channel(ctx, argument, 'channels', discord.StoreChannel)
|
return GuildChannelConverter._resolve_channel(ctx, argument, 'channels', discord.StoreChannel)
|
||||||
|
|
||||||
|
class ThreadConverter(IDConverter[discord.Thread]):
|
||||||
|
"""Coverts to a :class:`~discord.Thread`.
|
||||||
|
|
||||||
|
All lookups are via the local guild.
|
||||||
|
|
||||||
|
The lookup strategy is as follows (in order):
|
||||||
|
|
||||||
|
1. Lookup by ID.
|
||||||
|
2. Lookup by mention.
|
||||||
|
3. Lookup by name.
|
||||||
|
|
||||||
|
.. versionadded: 2.0
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def convert(self, ctx: Context, argument: str) -> discord.Thread:
|
||||||
|
return GuildChannelConverter._resolve_thread(ctx, argument, 'threads', discord.Thread)
|
||||||
|
|
||||||
class ColourConverter(Converter[discord.Colour]):
|
class ColourConverter(Converter[discord.Colour]):
|
||||||
"""Converts to a :class:`~discord.Colour`.
|
"""Converts to a :class:`~discord.Colour`.
|
||||||
@ -947,6 +986,7 @@ CONVERTER_MAPPING: Dict[Type[Any], Any] = {
|
|||||||
discord.PartialEmoji: PartialEmojiConverter,
|
discord.PartialEmoji: PartialEmojiConverter,
|
||||||
discord.CategoryChannel: CategoryChannelConverter,
|
discord.CategoryChannel: CategoryChannelConverter,
|
||||||
discord.StoreChannel: StoreChannelConverter,
|
discord.StoreChannel: StoreChannelConverter,
|
||||||
|
discord.Thread: ThreadConverter,
|
||||||
discord.abc.GuildChannel: GuildChannelConverter,
|
discord.abc.GuildChannel: GuildChannelConverter,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +47,7 @@ __all__ = (
|
|||||||
'GuildNotFound',
|
'GuildNotFound',
|
||||||
'UserNotFound',
|
'UserNotFound',
|
||||||
'ChannelNotFound',
|
'ChannelNotFound',
|
||||||
|
'ThreadNotFound',
|
||||||
'ChannelNotReadable',
|
'ChannelNotReadable',
|
||||||
'BadColourArgument',
|
'BadColourArgument',
|
||||||
'BadColorArgument',
|
'BadColorArgument',
|
||||||
@ -336,6 +337,22 @@ class ChannelNotFound(BadArgument):
|
|||||||
self.argument = argument
|
self.argument = argument
|
||||||
super().__init__(f'Channel "{argument}" not found.')
|
super().__init__(f'Channel "{argument}" not found.')
|
||||||
|
|
||||||
|
class ThreadNotFound(BadArgument):
|
||||||
|
"""Exception raised when the bot can not find the thread.
|
||||||
|
|
||||||
|
This inherits from :exc:`BadArgument`
|
||||||
|
|
||||||
|
..versionadded:: 2.0
|
||||||
|
|
||||||
|
Attributes
|
||||||
|
-----------
|
||||||
|
argument: :class:`str`
|
||||||
|
The thread supplied by the caller that was not found
|
||||||
|
"""
|
||||||
|
def __init__(self, argument):
|
||||||
|
self.argument = argument
|
||||||
|
super().__init__(f'Thread "{argument}" not found.')
|
||||||
|
|
||||||
class BadColourArgument(BadArgument):
|
class BadColourArgument(BadArgument):
|
||||||
"""Exception raised when the colour is not valid.
|
"""Exception raised when the colour is not valid.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user