From e94f64071259369f6402f7d571cc9df6d019e1ea Mon Sep 17 00:00:00 2001 From: maxigames Date: Wed, 24 Apr 2024 21:03:29 +0000 Subject: [PATCH] Update backup/backup.py --- backup/backup.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/backup/backup.py b/backup/backup.py index 5c6485f..1476478 100644 --- a/backup/backup.py +++ b/backup/backup.py @@ -1,3 +1,10 @@ +# _____ _ +# / ____| (_) +# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __ +# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__| +# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ | +# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_| + import contextlib import json import re @@ -9,6 +16,8 @@ from redbot.core import commands from redbot.core.bot import Red from redbot.core.utils.chat_formatting import error, humanize_list, text_to_file + +# pylint: disable=protected-access class Backup(commands.Cog): """A utility to make reinstalling repositories and cogs after migrating the bot far easier.""" @@ -68,6 +77,9 @@ class Backup(commands.Cog): if cog.repo_name == repo.name: cog_dict = { "name": cog.name, + # "loaded": cog.name in ctx.bot.extensions.keys(), + # this functionality was planned but never implemented due to Red limitations + # and the possibility of restoration functionality being added to Core "pinned": cog.pinned, "commit": cog.commit, } @@ -92,4 +104,39 @@ class Backup(commands.Cog): elif ctx.message.reference and hasattr(ctx.message.reference, 'resolved'): if ctx.message.reference.resolved.attachments: try: - export + export = json.loads(await ctx.message.reference.resolved.attachments[0].read()) + except json.JSONDecodeError: + await ctx.send(error("Invalid JSON in referenced message attachments.")) + if export is None: + await ctx.send(error("Please provide a valid JSON export file.")) + return + + downloader = ctx.bot.get_cog("Downloader") + if downloader is None: + await ctx.send( + error( + f"You do not have the `Downloader` cog loaded. Please run `{ctx.prefix}load downloader` and try again." + ) + ) + return + + all_repos = list(downloader._repo_manager.repos) + + for repo in export: + if repo["name"] not in [r.name for r in all_repos]: + try: + await downloader._repo_manager.add_repo( + repo["url"], repo["name"], repo["branch"] + ) + except errors.ExistingGitRepo: + pass + + for cog in repo["cogs"]: + try: + await downloader._cog_install_interface.install_cog( + cog["name"], cog["commit"] + ) + except errors.CogNotFoundError: + pass + except errors.DownloaderError: + pass \ No newline at end of file