Skip to content

Commit 65efe24

Browse files
committed
Add support to jsk sync for clearing application commands
Usage remains largely the same, but prefixing a target with `-` will switch upserting an empty payload `-` alone or `-$` can both be used to clear global commands `-.`, `-*`, or `-{guild_id}` all simply clear the commands for the expected target
1 parent fc89e12 commit 65efe24

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

jishaku/features/management.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -212,38 +212,50 @@ async def jsk_sync(self, ctx: ContextA, *targets: str):
212212
paginator = commands.Paginator(prefix='', suffix='')
213213

214214
guilds_set: typing.Set[typing.Optional[int]] = set()
215+
clear_guilds_set: typing.Set[typing.Optional[int]] = set()
215216
for target in targets:
216-
if target == '$':
217-
guilds_set.add(None)
217+
active_set = guilds_set
218+
if target[0] == '-':
219+
active_set = clear_guilds_set
220+
target = target[1:]
221+
if target == '' or target == '$':
222+
active_set.add(None)
218223
elif target == '*':
219-
guilds_set |= set(self.bot.tree._guild_commands.keys()) # type: ignore # pylint: disable=protected-access
224+
active_set |= set(self.bot.tree._guild_commands.keys()) # type: ignore # pylint: disable=protected-access
220225
elif target == '.':
221226
if ctx.guild:
222-
guilds_set.add(ctx.guild.id)
227+
active_set.add(ctx.guild.id)
223228
else:
224229
await ctx.send("Can't sync guild commands without guild information")
225230
return
226231
else:
227232
try:
228-
guilds_set.add(int(target))
233+
active_set.add(int(target))
229234
except ValueError as error:
230235
raise commands.BadArgument(f"{target} is not a valid guild ID") from error
231236

232237
if not targets:
233238
guilds_set.add(None)
234239

235-
guilds: typing.List[typing.Optional[int]] = list(guilds_set)
236-
guilds.sort(key=lambda g: (g is not None, g))
240+
guilds: typing.List[typing.Tuple[typing.Optional[int], bool]] = []
241+
for guild in guilds_set:
242+
guilds.append((guild, False))
243+
for guild in clear_guilds_set:
244+
guilds.append((guild, True))
245+
guilds.sort(key=lambda g: (g[0] is not None, g))
237246

238-
for guild in guilds:
247+
for guild, clear in guilds:
239248
slash_commands = self.bot.tree._get_all_commands( # type: ignore # pylint: disable=protected-access
240249
guild=discord.Object(guild) if guild else None
241250
)
242-
translator = getattr(self.bot.tree, 'translator', None)
243-
if translator:
244-
payload = [await command.get_translated_payload(translator) for command in slash_commands]
251+
if clear:
252+
payload = []
245253
else:
246-
payload = [command.to_dict() for command in slash_commands]
254+
translator = getattr(self.bot.tree, 'translator', None)
255+
if translator:
256+
payload = [await command.get_translated_payload(translator) for command in slash_commands]
257+
else:
258+
payload = [command.to_dict() for command in slash_commands]
247259

248260
try:
249261
if guild is None:

0 commit comments

Comments
 (0)