Skip to content

Commit

Permalink
Allow passing multiple cogs to slash enablecog and `slash disableco…
Browse files Browse the repository at this point in the history
…g` (Cog-Creators#6001)

Co-authored-by: Michael Oliveira <[email protected]>
  • Loading branch information
karlsbjorn and Flame442 authored Dec 24, 2024
1 parent 1f48919 commit 9392077
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 41 deletions.
8 changes: 4 additions & 4 deletions docs/cog_guides/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4185,7 +4185,7 @@ slash disablecog

.. code-block:: none
[p]slash disablecog <cog_name>
[p]slash disablecog <cog_names...>
**Description**

Expand All @@ -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.

**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:

Expand Down Expand Up @@ -4230,7 +4230,7 @@ slash enablecog

.. code-block:: none
[p]slash enablecog <cog_name>
[p]slash enablecog <cog_names...>
**Description**

Expand All @@ -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.

**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:

Expand Down
117 changes: 80 additions & 37 deletions redbot/core/core_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,42 +2081,52 @@ async def slash_disable(
)
)

@slash.command(name="enablecog")
@slash.command(name="enablecog", require_var_positional=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.
See a list of cogs with application commands with `[p]slash list`.
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:**
- `<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()
to_add_slash = []
to_add_message = []
to_add_user = []

successful_cogs = set()
# Fetch a list of command names to enable
for name, com in self.bot.tree._disabled_global_commands.items():
if self._is_submodule(cog_name, com.module):
to_add_slash.append(name)
for cog_name in cog_names:
if self._is_submodule(cog_name, com.module):
to_add_slash.append(name)
successful_cogs.add(cog_name)
for key, com in self.bot.tree._disabled_context_menus.items():
if self._is_submodule(cog_name, com.module):
name, guild_id, com_type = key
com_type = discord.AppCommandType(com_type)
if com_type is discord.AppCommandType.message:
to_add_message.append(name)
elif com_type is discord.AppCommandType.user:
to_add_user.append(name)
for cog_name in cog_names:
if self._is_submodule(cog_name, com.module):
name, guild_id, com_type = key
com_type = discord.AppCommandType(com_type)
if com_type is discord.AppCommandType.message:
to_add_message.append(name)
successful_cogs.add(cog_name)
elif com_type is discord.AppCommandType.user:
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
if not (to_add_slash or to_add_message or to_add_user):
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."
).format(cog_name=cog_name, prefix=ctx.prefix)
"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,
)
)
return

Expand All @@ -2130,7 +2140,7 @@ async def slash_enablecog(self, ctx: commands.Context, cog_name: str):
if total_slash > SLASH_CAP:
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. "
"Disable some commands first."
).format(count=total_slash, cap=SLASH_CAP)
Expand All @@ -2139,7 +2149,7 @@ async def slash_enablecog(self, ctx: commands.Context, cog_name: str):
if total_message > CONTEXT_CAP:
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. "
"Disable some commands first."
).format(count=total_message, cap=CONTEXT_CAP)
Expand All @@ -2148,7 +2158,7 @@ async def slash_enablecog(self, ctx: commands.Context, cog_name: str):
if total_user > CONTEXT_CAP:
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. "
"Disable some commands first."
).format(count=total_user, cap=CONTEXT_CAP)
Expand All @@ -2172,47 +2182,80 @@ async def slash_enablecog(self, ctx: commands.Context, cog_name: str):
names.extend(to_add_message)
names.extend(to_add_user)
formatted_names = humanize_list([inline(name) for name in names])
await ctx.send(
_("Enabled {count} commands from `{cog_name}`:\n{names}").format(
count=count, cog_name=cog_name, names=formatted_names
)
formatted_successful_cogs = humanize_list([inline(name) for name in successful_cogs])

output = _("Enabled {count} commands from {cog_names}:\n{names}").format(
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")
async def slash_disablecog(self, ctx: commands.Context, cog_name):
@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.
See a list of cogs with application commands with `[p]slash list`.
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:**
- `<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_cogs = set()
for name, com in self.bot.tree._global_commands.items():
if self._is_submodule(cog_name, com.module):
await self.bot.disable_app_command(name, discord.AppCommandType.chat_input)
removed.append(name)
for cog_name in cog_names:
if self._is_submodule(cog_name, com.module):
await self.bot.disable_app_command(name, discord.AppCommandType.chat_input)
removed.append(name)
removed_cogs.add(cog_name)
for key, com in self.bot.tree._context_menus.items():
if self._is_submodule(cog_name, com.module):
name, guild_id, com_type = key
await self.bot.disable_app_command(name, discord.AppCommandType(com_type))
removed.append(name)
for cog_name in cog_names:
if self._is_submodule(cog_name, com.module):
name, guild_id, com_type = key
await self.bot.disable_app_command(name, discord.AppCommandType(com_type))
removed.append(name)
removed_cogs.add(cog_name)
failed_cogs = set(cog_names) - removed_cogs

if not removed:
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."
).format(cog_name=cog_name, prefix=ctx.prefix)
"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,
)
)
return

await self.bot.tree.red_check_enabled()
formatted_names = humanize_list([inline(name) for name in removed])
await ctx.send(
_("Disabled {count} commands from `{cog_name}`:\n{names}").format(
count=len(removed), cog_name=cog_name, names=formatted_names
)
formatted_removed_cogs = humanize_list([inline(name) for name in removed_cogs])

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")
async def slash_list(self, ctx: commands.Context):
Expand Down

0 comments on commit 9392077

Please sign in to comment.