Skip to content

Commit

Permalink
[commands] Add support for channel URLs in channel converter
Browse files Browse the repository at this point in the history
Fix #9799
  • Loading branch information
Rapptz committed Jun 2, 2024
1 parent 356474f commit 3a64a5e
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions discord/ext/commands/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,19 +438,36 @@ class GuildChannelConverter(IDConverter[discord.abc.GuildChannel]):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name.
3. Lookup by channel URL.
4. Lookup by name.
.. versionadded:: 2.0
.. versionchanged:: 2.4
Add lookup by channel URL, accessed via "Copy Link" in the Discord client within channels.
"""

async def convert(self, ctx: Context[BotT], argument: str) -> discord.abc.GuildChannel:
return self._resolve_channel(ctx, argument, 'channels', discord.abc.GuildChannel)

@staticmethod
def _parse_from_url(argument: str) -> Optional[re.Match[str]]:
link_regex = re.compile(
r'https?://(?:(?:ptb|canary|www)\.)?discord(?:app)?\.com/channels/'
r'(?:[0-9]{15,20}|@me)'
r'/([0-9]{15,20})(?:/(?:[0-9]{15,20})/?)?$'
)
return link_regex.match(argument)

@staticmethod
def _resolve_channel(ctx: Context[BotT], 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)
match = (
IDConverter._get_id_match(argument)
or re.match(r'<#([0-9]{15,20})>$', argument)
or GuildChannelConverter._parse_from_url(argument)
)
result = None
guild = ctx.guild

Expand Down Expand Up @@ -480,7 +497,11 @@ def check(c):

@staticmethod
def _resolve_thread(ctx: Context[BotT], argument: str, attribute: str, type: Type[TT]) -> TT:
match = IDConverter._get_id_match(argument) or re.match(r'<#([0-9]{15,20})>$', argument)
match = (
IDConverter._get_id_match(argument)
or re.match(r'<#([0-9]{15,20})>$', argument)
or GuildChannelConverter._parse_from_url(argument)
)
result = None
guild = ctx.guild

Expand Down Expand Up @@ -510,10 +531,14 @@ class TextChannelConverter(IDConverter[discord.TextChannel]):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by channel URL.
3. Lookup by name
.. versionchanged:: 1.5
Raise :exc:`.ChannelNotFound` instead of generic :exc:`.BadArgument`
.. versionchanged:: 2.4
Add lookup by channel URL, accessed via "Copy Link" in the Discord client within channels.
"""

async def convert(self, ctx: Context[BotT], argument: str) -> discord.TextChannel:
Expand All @@ -530,10 +555,14 @@ class VoiceChannelConverter(IDConverter[discord.VoiceChannel]):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
3. Lookup by channel URL.
4. Lookup by name
.. versionchanged:: 1.5
Raise :exc:`.ChannelNotFound` instead of generic :exc:`.BadArgument`
.. versionchanged:: 2.4
Add lookup by channel URL, accessed via "Copy Link" in the Discord client within channels.
"""

async def convert(self, ctx: Context[BotT], argument: str) -> discord.VoiceChannel:
Expand All @@ -552,7 +581,11 @@ class StageChannelConverter(IDConverter[discord.StageChannel]):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
3. Lookup by channel URL.
4. Lookup by name
.. versionchanged:: 2.4
Add lookup by channel URL, accessed via "Copy Link" in the Discord client within channels.
"""

async def convert(self, ctx: Context[BotT], argument: str) -> discord.StageChannel:
Expand All @@ -569,7 +602,11 @@ class CategoryChannelConverter(IDConverter[discord.CategoryChannel]):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
3. Lookup by channel URL.
4. Lookup by name
.. versionchanged:: 2.4
Add lookup by channel URL, accessed via "Copy Link" in the Discord client within channels.
.. versionchanged:: 1.5
Raise :exc:`.ChannelNotFound` instead of generic :exc:`.BadArgument`
Expand All @@ -588,9 +625,13 @@ class ThreadConverter(IDConverter[discord.Thread]):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name.
3. Lookup by channel URL.
4. Lookup by name.
.. versionadded: 2.0
.. versionchanged:: 2.4
Add lookup by channel URL, accessed via "Copy Link" in the Discord client within channels.
"""

async def convert(self, ctx: Context[BotT], argument: str) -> discord.Thread:
Expand All @@ -607,9 +648,13 @@ class ForumChannelConverter(IDConverter[discord.ForumChannel]):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
3. Lookup by channel URL.
4. Lookup by name
.. versionadded:: 2.0
.. versionchanged:: 2.4
Add lookup by channel URL, accessed via "Copy Link" in the Discord client within channels.
"""

async def convert(self, ctx: Context[BotT], argument: str) -> discord.ForumChannel:
Expand Down

0 comments on commit 3a64a5e

Please sign in to comment.