From 4ef487a8c4610da7dd489bfda9c043c2a3678ab5 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Sat, 23 Jul 2022 14:12:32 -0400 Subject: [PATCH] Saving deleted data from left servers. --- .gitignore | 3 ++- cogs/guild_listener/__init__.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 8e1a147..9a6edea 100644 --- a/.gitignore +++ b/.gitignore @@ -133,4 +133,5 @@ nextcord.log # data file for bot highscores_data.json highscores_message_map.json -highscores_config.json \ No newline at end of file +highscores_config.json +deleted_backups/ \ No newline at end of file diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py index c92129f..59700b7 100644 --- a/cogs/guild_listener/__init__.py +++ b/cogs/guild_listener/__init__.py @@ -2,6 +2,9 @@ Defines cog for guild listeners. Contains functions for various guild events. """ +import os +import json + from nextcord.ext import commands import nextcord @@ -24,7 +27,8 @@ class GuildListener(commands.Cog, name="Guild Listener"): save_highscores_data(highscores_data) welcome_message: str = "Hello, I keep track of Highscores for you!" - welcome_message += " Currently only supporting RuneScape boss highscores, but will be more dynamic support in future." + welcome_message += " Currently only supporting RuneScape boss highscores," + welcome_message += " but will be more dynamic support in future." welcome_message += ' For assistance on getting started, use "?register help".' await guild.system_channel.send(content=welcome_message) @@ -33,9 +37,15 @@ class GuildListener(commands.Cog, name="Guild Listener"): async def on_guild_remove(self, guild: nextcord.Guild): """Listener for Guild Remove. Removes data from highscores_data.""" print(f"Removing data for guild {guild.name} (id {guild.id}).") - highscores_data.pop(str(guild.id)) + old_data = highscores_data.pop(str(guild.id)) save_highscores_data(highscores_data) + if not os.path.exists("deleted_backups"): + os.mkdir("deleted_backups") + + with open(f"deleted_backups/{guild.name}_backup.json", encoding="utf-8", mode="w") as file: + file.write(json.dumps(old_data, indent=4)) + # This function will be called when this extension is loaded. # It is necessary to add these functions to the bot.