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 support to jsk sync for clearing application commands #229

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 24 additions & 12 deletions jishaku/features/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,38 +212,50 @@ async def jsk_sync(self, ctx: ContextA, *targets: str):
paginator = commands.Paginator(prefix='', suffix='')

guilds_set: typing.Set[typing.Optional[int]] = set()
clear_guilds_set: typing.Set[typing.Optional[int]] = set()
for target in targets:
if target == '$':
guilds_set.add(None)
active_set = guilds_set
if target[0] == '-':
active_set = clear_guilds_set
target = target[1:]
if target in ('', '$'):
active_set.add(None)
Comment on lines +217 to +222
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like there's gotta be a cleaner way of doing this. The plain method of parsing was probably fine when it was just a few special cases, but maybe it needs a bit of a do-over if we want it to handle removals and transferring of commands. Not sure of the approach though.

Copy link
Contributor Author

@clari7744 clari7744 Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered a couple things but really not sure of the cleanest method here. We could use a match/case, but that's not compatible with <3.10, and there's still the issue of separating guilds-to-clear and guilds-to-add. If you have a general idea of what a cleaner method would be I could look into fleshing out/implementing something like it, but can't think of anything right now.

elif target == '*':
guilds_set |= set(self.bot.tree._guild_commands.keys()) # type: ignore # pylint: disable=protected-access
active_set |= set(self.bot.tree._guild_commands.keys()) # type: ignore # pylint: disable=protected-access
elif target == '.':
if ctx.guild:
guilds_set.add(ctx.guild.id)
active_set.add(ctx.guild.id)
else:
await ctx.send("Can't sync guild commands without guild information")
return
else:
try:
guilds_set.add(int(target))
active_set.add(int(target))
except ValueError as error:
raise commands.BadArgument(f"{target} is not a valid guild ID") from error

if not targets:
guilds_set.add(None)

guilds: typing.List[typing.Optional[int]] = list(guilds_set)
guilds.sort(key=lambda g: (g is not None, g))
guilds: typing.List[typing.Tuple[typing.Optional[int], bool]] = []
for guild in guilds_set:
guilds.append((guild, False))
for guild in clear_guilds_set:
guilds.append((guild, True))
guilds.sort(key=lambda g: (g[0] is not None, g))

for guild in guilds:
for guild, clear in guilds:
slash_commands = self.bot.tree._get_all_commands( # type: ignore # pylint: disable=protected-access
guild=discord.Object(guild) if guild else None
)
translator = getattr(self.bot.tree, 'translator', None)
if translator:
payload = [await command.get_translated_payload(translator) for command in slash_commands]
if clear:
payload = []
else:
payload = [command.to_dict() for command in slash_commands]
translator = getattr(self.bot.tree, 'translator', None)
if translator:
payload = [await command.get_translated_payload(translator) for command in slash_commands]
else:
payload = [command.to_dict() for command in slash_commands]

try:
if guild is None:
Expand Down