Skip to content

Commit

Permalink
Update SlimChannelConverter (#221)
Browse files Browse the repository at this point in the history
* fix slim channel and thread to work with mentions

* update slimchannelconverter

client.get_channel already resolves threads, don't need a separate thread converter. It also returns other channel types other than TextChannel

* remove whitespace
  • Loading branch information
z03h committed Apr 7, 2024
1 parent fc89e12 commit 664e009
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions jishaku/features/invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
from jishaku.types import ContextA, ContextT

UserIDConverter = commands.IDConverter[typing.Union[discord.Member, discord.User]]
ChannelIDConverter = commands.IDConverter[discord.TextChannel]
ThreadIDConverter = commands.IDConverter[discord.Thread]
ChannelIDConverter = commands.IDConverter[typing.Union[discord.abc.GuildChannel, discord.abc.PrivateChannel, discord.Thread]]


class SlimUserConverter(UserIDConverter): # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -58,44 +57,31 @@ async def convert(self, ctx: ContextA, argument: str) -> typing.Union[discord.Me

class SlimChannelConverter(ChannelIDConverter): # pylint: disable=too-few-public-methods
"""
Identical to the stock TextChannelConverter, but does not perform plaintext name checks.
Similar to Union[GuildChannelConverter, ThreadConverter], but can return PrivateChannels and
does not perform plaintext name or guild checks.
"""

async def convert(self, ctx: ContextA, argument: str) -> discord.TextChannel:
async def convert(self, ctx: ContextA, argument: str) -> typing.Union[discord.abc.GuildChannel, discord.abc.PrivateChannel, discord.Thread]:
"""Converter method"""
match = self._get_id_match(argument) or re.match(r'<@!?([0-9]{15,20})>$', argument)
match = self._get_id_match(argument) or re.match(r'<#([0-9]{15,20})>$', argument)

if match is not None:
channel_id = int(match.group(1))
result = ctx.bot.get_channel(channel_id) or discord.utils.get(ctx.message.channel_mentions, id=channel_id)
if ctx.guild:
result = ctx.guild.get_channel_or_thread(channel_id)
if not result:
result = ctx.bot.get_channel(channel_id)
if result is not None:
return result
raise commands.ChannelNotFound(argument)


class SlimThreadConverter(ThreadIDConverter): # pylint: disable=too-few-public-methods
"""
Identical to the stock ThreadConverter, but does not perform plaintext name checks.
"""

async def convert(self, ctx: ContextA, argument: str) -> discord.Thread:
"""Converter method"""
match = self._get_id_match(argument) or re.match(r'<@!?([0-9]{15,20})>$', argument)

if match is not None:
thread_id = int(match.group(1))
result = ctx.guild.get_thread(thread_id)
if result is not None:
return result
raise commands.ThreadNotFound(argument)


class InvocationFeature(Feature):
"""
Feature containing the command invocation related commands
"""

OVERRIDE_SIGNATURE = typing.Union[SlimUserConverter, SlimChannelConverter, SlimThreadConverter]
OVERRIDE_SIGNATURE = typing.Union[SlimUserConverter, SlimChannelConverter]

@Feature.Command(parent="jsk", name="override", aliases=["execute", "exec", "override!", "execute!", "exec!"])
async def jsk_override(
Expand Down

0 comments on commit 664e009

Please sign in to comment.