Remove multiple paths (#5859)

Co-authored-by: Kreusada <67752638+Kreusada@users.noreply.github.com>
Co-authored-by: Jakub Kuczys <me@jacken.men>
This commit is contained in:
Julien Mauroy 2022-12-27 21:51:13 +01:00 committed by GitHub
parent 1ab303bce7
commit d9dd37b867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 25 deletions

View File

@ -171,17 +171,17 @@ removepath
.. code-block:: none .. code-block:: none
[p]removepath <path_number> [p]removepath <path_numbers...>
**Description** **Description**
Removes a path from the list of available paths. Its cogs won't be accessible Removes one or more paths from the list of available paths. Its cogs won't be
anymore. accessible anymore.
**Arguments** **Arguments**
* ``<path_number>``: The number of the path to remove. You can get it with * ``<path_numbers>``: The number of the path(s) to remove. You can get it with
the :ref:`paths <cogmanagerui>` command. the :ref:`paths <cogmanagerui-command-paths>` command.
.. _cogmanagerui-command-reorderpath: .. _cogmanagerui-command-reorderpath:

View File

@ -4,9 +4,10 @@ import pkgutil
from importlib import import_module, invalidate_caches from importlib import import_module, invalidate_caches
from importlib.machinery import ModuleSpec from importlib.machinery import ModuleSpec
from pathlib import Path from pathlib import Path
from typing import Union, List, Optional from typing import TYPE_CHECKING, Union, List, Optional
import redbot.cogs import redbot.cogs
from redbot.core.commands import BadArgument
from redbot.core.utils import deduplicate_iterables from redbot.core.utils import deduplicate_iterables
import discord import discord
@ -15,11 +16,26 @@ from .config import Config
from .i18n import Translator, cog_i18n from .i18n import Translator, cog_i18n
from .data_manager import cog_data_path from .data_manager import cog_data_path
from .utils.chat_formatting import box, pagify from .utils.chat_formatting import box, pagify, humanize_list, inline
__all__ = ["CogManager"] __all__ = ["CogManager"]
# Duplicate of redbot.cogs.cleanup.converters.positive_int
if TYPE_CHECKING:
positive_int = int
else:
def positive_int(arg: str) -> int:
try:
ret = int(arg)
except ValueError:
raise BadArgument(_("{arg} is not an integer.").format(arg=inline(arg)))
if ret <= 0:
raise BadArgument(_("{arg} is not a positive integer.").format(arg=inline(arg)))
return ret
class NoSuchCog(ImportError): class NoSuchCog(ImportError):
"""Thrown when a cog is missing. """Thrown when a cog is missing.
@ -358,39 +374,53 @@ class CogManagerUI(commands.Cog):
else: else:
await ctx.send(_("Path successfully added.")) await ctx.send(_("Path successfully added."))
@commands.command() @commands.command(require_var_positional=True)
@checks.is_owner() @checks.is_owner()
async def removepath(self, ctx: commands.Context, path_number: int): async def removepath(self, ctx: commands.Context, *path_numbers: positive_int):
""" """
Removes a path from the available cog paths given the `path_number` from `[p]paths`. Removes one or more paths from the available cog paths given the `path_numbers` from `[p]paths`.
""" """
path_number -= 1 valid: List[Path] = []
if path_number < 0: invalid: List[int] = []
await ctx.send(_("Path numbers must be positive."))
return
cog_paths = await ctx.bot._cog_mgr.user_defined_paths() cog_paths = await ctx.bot._cog_mgr.user_defined_paths()
# dict.fromkeys removes duplicates while preserving the order
for path_number in dict.fromkeys(sorted(path_numbers)):
idx = path_number - 1
try: try:
to_remove = cog_paths.pop(path_number) to_remove = cog_paths[idx]
except IndexError: except IndexError:
await ctx.send(_("That is an invalid path number.")) invalid.append(path_number)
return else:
await ctx.bot._cog_mgr.remove_path(to_remove) await ctx.bot._cog_mgr.remove_path(to_remove)
await ctx.send(_("Path successfully removed.")) valid.append(to_remove)
parts = []
if valid:
parts.append(
_("The following paths were removed: {paths}").format(
paths=humanize_list([inline(str(path)) for path in valid])
)
)
if invalid:
parts.append(
_("The following path numbers did not exist: {path_numbers}").format(
path_numbers=humanize_list([inline(str(path)) for path in invalid])
)
)
for page in pagify("\n\n".join(parts), ["\n", " "]):
await ctx.send(page)
@commands.command() @commands.command()
@checks.is_owner() @checks.is_owner()
async def reorderpath(self, ctx: commands.Context, from_: int, to: int): async def reorderpath(self, ctx: commands.Context, from_: positive_int, to: positive_int):
""" """
Reorders paths internally to allow discovery of different cogs. Reorders paths internally to allow discovery of different cogs.
""" """
# Doing this because in the paths command they're 1 indexed # Doing this because in the paths command they're 1 indexed
from_ -= 1 from_ -= 1
to -= 1 to -= 1
if from_ < 0 or to < 0:
await ctx.send(_("Path numbers must be positive."))
return
all_paths = await ctx.bot._cog_mgr.user_defined_paths() all_paths = await ctx.bot._cog_mgr.user_defined_paths()
try: try: