diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py index 59700b7..8729bef 100644 --- a/cogs/guild_listener/__init__.py +++ b/cogs/guild_listener/__init__.py @@ -9,8 +9,6 @@ from nextcord.ext import commands import nextcord from highscores import highscores_data # pylint: disable=import-error -from utilities.data_storage import create_highscore_skeleton # pylint: disable=import-error -from utilities.data_storage import save_highscores_data # pylint: disable=import-error class GuildListener(commands.Cog, name="Guild Listener"): @@ -23,8 +21,7 @@ class GuildListener(commands.Cog, name="Guild Listener"): async def on_guild_join(self, guild: nextcord.Guild): """Listener for Guild Join. Creates skeleton for new guild joined and saves it.""" # turning guild id into string because json doesn't support ints as object keys. - highscores_data[str(guild.id)] = create_highscore_skeleton() - save_highscores_data(highscores_data) + highscores_data.add_new_server(guild.id) welcome_message: str = "Hello, I keep track of Highscores for you!" welcome_message += " Currently only supporting RuneScape boss highscores," @@ -37,14 +34,7 @@ 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}).") - 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)) + highscores_data.remove_guild(guild.id, guild.name) # This function will be called when this extension is loaded. diff --git a/utilities/highscores_data.py b/utilities/highscores_data.py index c199be4..f130dad 100644 --- a/utilities/highscores_data.py +++ b/utilities/highscores_data.py @@ -149,6 +149,22 @@ class HighscoresData: """ return self._data[str(guild_id)]["tables"][table]["message_id"] + def remove_guild(self, guild_id: int, guild_name: str) -> None: + """ + Removes a guild's data from object. Saves backup. + + @param guild_id: guild id to remove. + @param guild_name: name of guild for backup. + """ + old_data = self._data.pop(str(guild_id)) + save_highscores_data(self._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)) + def reset_guild_data(self, guild_id: int) -> None: """ Resets highscore data for a guild. Should only be used for development really...