Allow passing multiple cogs to slash enablecog and slash disablecog (#6001)

Co-authored-by: Michael Oliveira <34169552+Flame442@users.noreply.github.com>
This commit is contained in:
Karlo Prikratki 2024-12-24 04:49:19 +01:00 committed by GitHub
parent 1f48919005
commit 9392077434
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 85 additions and 42 deletions

View File

@ -4185,7 +4185,7 @@ slash disablecog
.. code-block:: none .. code-block:: none
[p]slash disablecog <cog_name> [p]slash disablecog <cog_names...>
**Description** **Description**
@ -4195,7 +4195,7 @@ This command does NOT sync the enabled commands with Discord, that must be done
with ``[p]slash sync`` for commands to appear in users' clients. with ``[p]slash sync`` for commands to appear in users' clients.
**Arguments:** **Arguments:**
- ``<cog_name>`` - The cog to disable commands from. This argument is case sensitive. - ``<cog_names>`` - The cogs to disable commands from. This argument is case sensitive.
.. _core-command-slash-enable: .. _core-command-slash-enable:
@ -4230,7 +4230,7 @@ slash enablecog
.. code-block:: none .. code-block:: none
[p]slash enablecog <cog_name> [p]slash enablecog <cog_names...>
**Description** **Description**
@ -4240,7 +4240,7 @@ This command does NOT sync the enabled commands with Discord, that must be done
with ``[p]slash sync`` for commands to appear in users' clients. with ``[p]slash sync`` for commands to appear in users' clients.
**Arguments:** **Arguments:**
- ``<cog_name>`` - The cog to enable commands from. This argument is case sensitive. - ``<cog_names>`` - The cogs to enable commands from. This argument is case sensitive.
.. _core-command-slash-list: .. _core-command-slash-list:

View File

@ -2081,9 +2081,9 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
) )
) )
@slash.command(name="enablecog") @slash.command(name="enablecog", require_var_positional=True)
@commands.max_concurrency(1, wait=True) @commands.max_concurrency(1, wait=True)
async def slash_enablecog(self, ctx: commands.Context, cog_name: str): async def slash_enablecog(self, ctx: commands.Context, *cog_names: str):
"""Marks all application commands in a cog as being enabled, allowing them to be added to the bot. """Marks all application commands in a cog as being enabled, allowing them to be added to the bot.
See a list of cogs with application commands with `[p]slash list`. See a list of cogs with application commands with `[p]slash list`.
@ -2091,32 +2091,42 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
This command does NOT sync the enabled commands with Discord, that must be done manually with `[p]slash sync` for commands to appear in users' clients. This command does NOT sync the enabled commands with Discord, that must be done manually with `[p]slash sync` for commands to appear in users' clients.
**Arguments:** **Arguments:**
- `<cog_name>` - The cog to enable commands from. This argument is case sensitive. - `<cog_names>` - The cogs to enable commands from. This argument is case sensitive.
""" """
enabled_commands = await self.bot.list_enabled_app_commands() enabled_commands = await self.bot.list_enabled_app_commands()
to_add_slash = [] to_add_slash = []
to_add_message = [] to_add_message = []
to_add_user = [] to_add_user = []
successful_cogs = set()
# Fetch a list of command names to enable # Fetch a list of command names to enable
for name, com in self.bot.tree._disabled_global_commands.items(): for name, com in self.bot.tree._disabled_global_commands.items():
for cog_name in cog_names:
if self._is_submodule(cog_name, com.module): if self._is_submodule(cog_name, com.module):
to_add_slash.append(name) to_add_slash.append(name)
successful_cogs.add(cog_name)
for key, com in self.bot.tree._disabled_context_menus.items(): for key, com in self.bot.tree._disabled_context_menus.items():
for cog_name in cog_names:
if self._is_submodule(cog_name, com.module): if self._is_submodule(cog_name, com.module):
name, guild_id, com_type = key name, guild_id, com_type = key
com_type = discord.AppCommandType(com_type) com_type = discord.AppCommandType(com_type)
if com_type is discord.AppCommandType.message: if com_type is discord.AppCommandType.message:
to_add_message.append(name) to_add_message.append(name)
successful_cogs.add(cog_name)
elif com_type is discord.AppCommandType.user: elif com_type is discord.AppCommandType.user:
to_add_user.append(name) to_add_user.append(name)
successful_cogs.add(cog_name)
failed_cogs = set(cog_names) - successful_cogs
# Check that we are going to enable at least one command, for user feedback # Check that we are going to enable at least one command, for user feedback
if not (to_add_slash or to_add_message or to_add_user): if not (to_add_slash or to_add_message or to_add_user):
await ctx.send( await ctx.send(
_( _(
"Couldn't find any disabled commands from the cog `{cog_name}`. Use `{prefix}slash list` to see all cogs with application commands." "Couldn't find any disabled commands from {cog_names}. Use `{prefix}slash list` to see all cogs with application commands"
).format(cog_name=cog_name, prefix=ctx.prefix) ).format(
cog_names=humanize_list([inline(name) for name in failed_cogs]),
prefix=ctx.clean_prefix,
)
) )
return return
@ -2130,7 +2140,7 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
if total_slash > SLASH_CAP: if total_slash > SLASH_CAP:
await ctx.send( await ctx.send(
_( _(
"Enabling all application commands from that cog would enable a total of {count} " "Enabling all application commands from these cogs would enable a total of {count} "
"commands, exceeding the {cap} command limit for slash commands. " "commands, exceeding the {cap} command limit for slash commands. "
"Disable some commands first." "Disable some commands first."
).format(count=total_slash, cap=SLASH_CAP) ).format(count=total_slash, cap=SLASH_CAP)
@ -2139,7 +2149,7 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
if total_message > CONTEXT_CAP: if total_message > CONTEXT_CAP:
await ctx.send( await ctx.send(
_( _(
"Enabling all application commands from that cog would enable a total of {count} " "Enabling all application commands from these cogs would enable a total of {count} "
"commands, exceeding the {cap} command limit for message commands. " "commands, exceeding the {cap} command limit for message commands. "
"Disable some commands first." "Disable some commands first."
).format(count=total_message, cap=CONTEXT_CAP) ).format(count=total_message, cap=CONTEXT_CAP)
@ -2148,7 +2158,7 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
if total_user > CONTEXT_CAP: if total_user > CONTEXT_CAP:
await ctx.send( await ctx.send(
_( _(
"Enabling all application commands from that cog would enable a total of {count} " "Enabling all application commands from these cogs would enable a total of {count} "
"commands, exceeding the {cap} command limit for user commands. " "commands, exceeding the {cap} command limit for user commands. "
"Disable some commands first." "Disable some commands first."
).format(count=total_user, cap=CONTEXT_CAP) ).format(count=total_user, cap=CONTEXT_CAP)
@ -2172,14 +2182,24 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
names.extend(to_add_message) names.extend(to_add_message)
names.extend(to_add_user) names.extend(to_add_user)
formatted_names = humanize_list([inline(name) for name in names]) formatted_names = humanize_list([inline(name) for name in names])
await ctx.send( formatted_successful_cogs = humanize_list([inline(name) for name in successful_cogs])
_("Enabled {count} commands from `{cog_name}`:\n{names}").format(
count=count, cog_name=cog_name, names=formatted_names
)
)
@slash.command(name="disablecog") output = _("Enabled {count} commands from {cog_names}:\n{names}").format(
async def slash_disablecog(self, ctx: commands.Context, cog_name): count=count, cog_names=formatted_successful_cogs, names=formatted_names
)
if failed_cogs:
output += "\n\n"
output += _(
"Couldn't find any disabled commands from {cog_names}. Use `{prefix}slash list` to see all cogs with application commands."
).format(
cog_names=humanize_list([inline(name) for name in failed_cogs]),
prefix=ctx.clean_prefix,
)
for page in pagify(output):
await ctx.send(page)
@slash.command(name="disablecog", require_var_positional=True)
async def slash_disablecog(self, ctx: commands.Context, *cog_names: str):
"""Marks all application commands in a cog as being disabled, preventing them from being added to the bot. """Marks all application commands in a cog as being disabled, preventing them from being added to the bot.
See a list of cogs with application commands with `[p]slash list`. See a list of cogs with application commands with `[p]slash list`.
@ -2187,32 +2207,55 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
This command does NOT sync the enabled commands with Discord, that must be done manually with `[p]slash sync` for commands to appear in users' clients. This command does NOT sync the enabled commands with Discord, that must be done manually with `[p]slash sync` for commands to appear in users' clients.
**Arguments:** **Arguments:**
- `<cog_name>` - The cog to disable commands from. This argument is case sensitive. - `<cog_names>` - The cogs to disable commands from. This argument is case sensitive.
""" """
removed = [] removed = []
removed_cogs = set()
for name, com in self.bot.tree._global_commands.items(): for name, com in self.bot.tree._global_commands.items():
for cog_name in cog_names:
if self._is_submodule(cog_name, com.module): if self._is_submodule(cog_name, com.module):
await self.bot.disable_app_command(name, discord.AppCommandType.chat_input) await self.bot.disable_app_command(name, discord.AppCommandType.chat_input)
removed.append(name) removed.append(name)
removed_cogs.add(cog_name)
for key, com in self.bot.tree._context_menus.items(): for key, com in self.bot.tree._context_menus.items():
for cog_name in cog_names:
if self._is_submodule(cog_name, com.module): if self._is_submodule(cog_name, com.module):
name, guild_id, com_type = key name, guild_id, com_type = key
await self.bot.disable_app_command(name, discord.AppCommandType(com_type)) await self.bot.disable_app_command(name, discord.AppCommandType(com_type))
removed.append(name) removed.append(name)
removed_cogs.add(cog_name)
failed_cogs = set(cog_names) - removed_cogs
if not removed: if not removed:
await ctx.send( await ctx.send(
_( _(
"Couldn't find any enabled commands from the `{cog_name}` cog. Use `{prefix}slash list` to see all cogs with application commands." "Couldn't find any enabled commands from {cog_names}. Use `{prefix}slash list` to see all cogs with application commands."
).format(cog_name=cog_name, prefix=ctx.prefix) ).format(
cog_names=humanize_list([inline(name) for name in failed_cogs]),
prefix=ctx.clean_prefix,
)
) )
return return
await self.bot.tree.red_check_enabled() await self.bot.tree.red_check_enabled()
formatted_names = humanize_list([inline(name) for name in removed]) formatted_names = humanize_list([inline(name) for name in removed])
await ctx.send( formatted_removed_cogs = humanize_list([inline(name) for name in removed_cogs])
_("Disabled {count} commands from `{cog_name}`:\n{names}").format(
count=len(removed), cog_name=cog_name, names=formatted_names output = _("Disabled {count} commands from {cog_names}:\n{names}").format(
count=len(removed),
cog_names=formatted_removed_cogs,
names=formatted_names,
) )
if failed_cogs:
output += "\n\n"
output += _(
"Couldn't find any enabled commands from {cog_names}. Use `{prefix}slash list` to see all cogs with application commands."
).format(
cog_names=humanize_list([inline(name) for name in failed_cogs]),
prefix=ctx.clean_prefix,
) )
for page in pagify(output):
await ctx.send(page)
@slash.command(name="list") @slash.command(name="list")
async def slash_list(self, ctx: commands.Context): async def slash_list(self, ctx: commands.Context):