aboutsummaryrefslogtreecommitdiff
path: root/discord/ext/commands
diff options
context:
space:
mode:
authorAlex Nørgaard <[email protected]>2021-07-05 05:40:57 +0100
committerGitHub <[email protected]>2021-07-05 00:40:57 -0400
commit5a7cfb3ce63c0dc9099b029363b315c942f13d54 (patch)
tree22b62f45e2dd8cb4d306384911b47e9240f7c62b /discord/ext/commands
parentSeparate member_update and presence_update events (diff)
downloaddiscord.py-5a7cfb3ce63c0dc9099b029363b315c942f13d54.tar.xz
discord.py-5a7cfb3ce63c0dc9099b029363b315c942f13d54.zip
[commands] Add ThreadConverter
Diffstat (limited to 'discord/ext/commands')
-rw-r--r--discord/ext/commands/converter.py40
-rw-r--r--discord/ext/commands/errors.py17
2 files changed, 57 insertions, 0 deletions
diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py
index 2516b4fa..c07ad07c 100644
--- a/discord/ext/commands/converter.py
+++ b/discord/ext/commands/converter.py
@@ -71,6 +71,7 @@ __all__ = (
'CategoryChannelConverter',
'IDConverter',
'StoreChannelConverter',
+ 'ThreadConverter',
'GuildChannelConverter',
'clean_content',
'Greedy',
@@ -426,6 +427,28 @@ class GuildChannelConverter(IDConverter[discord.abc.GuildChannel]):
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]):
"""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:
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]):
"""Converts to a :class:`~discord.Colour`.
@@ -947,6 +986,7 @@ CONVERTER_MAPPING: Dict[Type[Any], Any] = {
discord.PartialEmoji: PartialEmojiConverter,
discord.CategoryChannel: CategoryChannelConverter,
discord.StoreChannel: StoreChannelConverter,
+ discord.Thread: ThreadConverter,
discord.abc.GuildChannel: GuildChannelConverter,
}
diff --git a/discord/ext/commands/errors.py b/discord/ext/commands/errors.py
index 901a0e56..0f162cff 100644
--- a/discord/ext/commands/errors.py
+++ b/discord/ext/commands/errors.py
@@ -47,6 +47,7 @@ __all__ = (
'GuildNotFound',
'UserNotFound',
'ChannelNotFound',
+ 'ThreadNotFound',
'ChannelNotReadable',
'BadColourArgument',
'BadColorArgument',
@@ -336,6 +337,22 @@ class ChannelNotFound(BadArgument):
self.argument = argument
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):
"""Exception raised when the colour is not valid.