import contextlib import json import re from red_commons.logging import getLogger from redbot.cogs.downloader import errors from redbot.cogs.downloader.converters import InstalledCog from redbot.core import commands from redbot.core.bot import Red from redbot.core.utils.chat_formatting import error, humanize_list, text_to_file class Backup(commands.Cog): """A utility to make reinstalling repositories and cogs after migrating the bot far easier.""" __author__ = ["SeaswimmerTheFsh"] __version__ = "1.1.0" __documentation__ = "https://seacogs.coastalcommits.com/backup/" def __init__(self, bot: Red): super().__init__() self.bot = bot self.logger = getLogger("red.SeaCogs.Backup") def format_help_for_context(self, ctx: commands.Context) -> str: pre_processed = super().format_help_for_context(ctx) or "" n = "\n" if "\n\n" not in pre_processed else "" text = [ f"{pre_processed}{n}", f"Cog Version: **{self.__version__}**", f"Author: {humanize_list(self.__author__)}", f"Documentation: {self.__documentation__}", ] return "\n".join(text) @commands.group(autohelp=True) @commands.is_owner() async def backup(self, ctx: commands.Context): """Backup your installed cogs.""" @backup.command(name="export") @commands.is_owner() async def backup_export(self, ctx: commands.Context): """Export your installed repositories and cogs to a file.""" 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) export_data = [] for repo in all_repos: repo_dict = { "name": repo.name, "url": repo.url, "branch": repo.branch, "cogs": [], } cogs = await downloader.installed_cogs() for cog in cogs: if cog.repo_name == repo.name: cog_dict = { "name": cog.name, "pinned": cog.pinned, "commit": cog.commit, } repo_dict["cogs"].append(cog_dict) export_data.append(repo_dict) await ctx.send( file=text_to_file(json.dumps(export_data, indent=4), "backup.json") ) @backup.command(name="import") @commands.is_owner() async def backup_import(self, ctx: commands.Context): """Import your installed repositories and cogs from an export file.""" export = None if ctx.message.attachments: try: export = json.loads(await ctx.message.attachments[0].read()) except json.JSONDecodeError: await ctx.send(error("Invalid JSON in message attachments.")) elif ctx.message.reference and hasattr(ctx.message.reference, 'resolved'): if ctx.message.reference.resolved.attachments: try: export