From 2dd8663e78f19926ed20a9b2c5f61e7e3f1a771c Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Wed, 31 Jan 2024 13:07:10 -0500 Subject: [PATCH] fix(backup): support codeblocks --- backup/backup.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backup/backup.py b/backup/backup.py index bc2ef0b..f499064 100644 --- a/backup/backup.py +++ b/backup/backup.py @@ -6,6 +6,7 @@ # |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_| import json +import re from redbot.core import commands from redbot.core.bot import Red @@ -63,7 +64,7 @@ class Backup(commands.Cog): if export is None and not len(ctx.message.attachments) == 0: export = await ctx.message.attachments[0].read() try: - export = json.loads(export) + export = json.loads(self.extract_json_from_codeblock(export)) except json.JSONDecodeError: await ctx.send(error("Please provide a valid JSON export.")) return @@ -72,3 +73,10 @@ class Backup(commands.Cog): 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 + + def extract_json_from_codeblock(text): + match = re.match(r"```(?:\w+)?\n(.*?)\n```", text, re.DOTALL) + if match: + return match.group(1).strip() + else: + return text