fix(backup): support codeblocks

This commit is contained in:
Seaswimmer 2024-01-31 13:07:10 -05:00
parent 702de21448
commit 2dd8663e78
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -6,6 +6,7 @@
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_| # |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
import json import json
import re
from redbot.core import commands from redbot.core import commands
from redbot.core.bot import Red 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: if export is None and not len(ctx.message.attachments) == 0:
export = await ctx.message.attachments[0].read() export = await ctx.message.attachments[0].read()
try: try:
export = json.loads(export) export = json.loads(self.extract_json_from_codeblock(export))
except json.JSONDecodeError: except json.JSONDecodeError:
await ctx.send(error("Please provide a valid JSON export.")) await ctx.send(error("Please provide a valid JSON export."))
return return
@ -72,3 +73,10 @@ class Backup(commands.Cog):
if downloader is None: 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.")) await ctx.send(error(f"You do not have the `Downloader` cog loaded. Please run `{ctx.prefix}load downloader` and try again."))
return 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