Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SlimChannel and SlimThread converters #218

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions jishaku/features/invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
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]


class SlimUserConverter(UserIDConverter): # pylint: disable=too-few-public-methods
Expand All @@ -54,15 +56,46 @@ async def convert(self, ctx: ContextA, argument: str) -> typing.Union[discord.Me
raise commands.UserNotFound(argument)


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

async def convert(self, ctx: ContextA, argument: str) -> discord.TextChannel:
"""Converter method"""
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 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
"""

if typing.TYPE_CHECKING or hasattr(discord, 'Thread'):
OVERRIDE_SIGNATURE = typing.Union[SlimUserConverter, discord.TextChannel, discord.Thread] # pylint: disable=no-member
else:
OVERRIDE_SIGNATURE = typing.Union[SlimUserConverter, discord.TextChannel]
OVERRIDE_SIGNATURE = typing.Union[SlimUserConverter, SlimChannelConverter, SlimThreadConverter]

@Feature.Command(parent="jsk", name="override", aliases=["execute", "exec", "override!", "execute!", "exec!"])
async def jsk_override(
Expand Down Expand Up @@ -160,7 +193,7 @@ async def jsk_debug(self, ctx: ContextT, *, command_string: str):

start = time.perf_counter()

async with ReplResponseReactor(ctx.message):
async with ReplResponseReactor(ctx):
with self.submit(ctx):
await alt_ctx.command.invoke(alt_ctx)

Expand Down