Skip to content

Commit 664e009

Browse files
authored
Update SlimChannelConverter (#221)
* 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
1 parent fc89e12 commit 664e009

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

jishaku/features/invocation.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
from jishaku.types import ContextA, ContextT
3030

3131
UserIDConverter = commands.IDConverter[typing.Union[discord.Member, discord.User]]
32-
ChannelIDConverter = commands.IDConverter[discord.TextChannel]
33-
ThreadIDConverter = commands.IDConverter[discord.Thread]
32+
ChannelIDConverter = commands.IDConverter[typing.Union[discord.abc.GuildChannel, discord.abc.PrivateChannel, discord.Thread]]
3433

3534

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

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

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

6868
if match is not None:
6969
channel_id = int(match.group(1))
70-
result = ctx.bot.get_channel(channel_id) or discord.utils.get(ctx.message.channel_mentions, id=channel_id)
70+
if ctx.guild:
71+
result = ctx.guild.get_channel_or_thread(channel_id)
72+
if not result:
73+
result = ctx.bot.get_channel(channel_id)
7174
if result is not None:
7275
return result
7376
raise commands.ChannelNotFound(argument)
7477

7578

76-
class SlimThreadConverter(ThreadIDConverter): # pylint: disable=too-few-public-methods
77-
"""
78-
Identical to the stock ThreadConverter, but does not perform plaintext name checks.
79-
"""
80-
81-
async def convert(self, ctx: ContextA, argument: str) -> discord.Thread:
82-
"""Converter method"""
83-
match = self._get_id_match(argument) or re.match(r'<@!?([0-9]{15,20})>$', argument)
84-
85-
if match is not None:
86-
thread_id = int(match.group(1))
87-
result = ctx.guild.get_thread(thread_id)
88-
if result is not None:
89-
return result
90-
raise commands.ThreadNotFound(argument)
91-
92-
9379
class InvocationFeature(Feature):
9480
"""
9581
Feature containing the command invocation related commands
9682
"""
9783

98-
OVERRIDE_SIGNATURE = typing.Union[SlimUserConverter, SlimChannelConverter, SlimThreadConverter]
84+
OVERRIDE_SIGNATURE = typing.Union[SlimUserConverter, SlimChannelConverter]
9985

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

0 commit comments

Comments
 (0)