diff --git a/cogs/post_highscores/__init__.py b/cogs/post_highscores/__init__.py index 67d5300..9349cd1 100644 --- a/cogs/post_highscores/__init__.py +++ b/cogs/post_highscores/__init__.py @@ -5,8 +5,8 @@ Bot command to post the highscores from nextcord.ext import commands from highscores import highscores_config # pylint: disable=import-error from highscores import highscores_data # pylint: disable=import-error -from utils.utils import save_highscores_data # pylint: disable=import-error -from utils.utils import send_highscore_message # pylint: disable=import-error +from utils.data_storage import save_highscores_data # pylint: disable=import-error +from utils.data_storage import send_highscore_message # pylint: disable=import-error class PostHighscores(commands.Cog, name="Post Highscores"): diff --git a/cogs/register_highscore_channel/__init__.py b/cogs/register_highscore_channel/__init__.py index 761a1c2..008054e 100644 --- a/cogs/register_highscore_channel/__init__.py +++ b/cogs/register_highscore_channel/__init__.py @@ -4,7 +4,7 @@ Bot command to register the highscores channel from nextcord.ext import commands from highscores import highscores_data # pylint: disable=import-error -from utils.utils import save_highscores_data # pylint: disable=import-error +from utils.data_storage import save_highscores_data # pylint: disable=import-error class RegisterHighscoreChannel(commands.Cog, name="Register HighScore Channel"): diff --git a/cogs/register_verification_channel/__init__.py b/cogs/register_verification_channel/__init__.py index b5040f3..4227fc5 100644 --- a/cogs/register_verification_channel/__init__.py +++ b/cogs/register_verification_channel/__init__.py @@ -4,7 +4,7 @@ Bot command to register the verification channel from nextcord.ext import commands from highscores import highscores_data # pylint: disable=import-error -from utils.utils import save_highscores_data # pylint: disable=import-error +from utils.data_storage import save_highscores_data # pylint: disable=import-error class RegisterVerificationChannel(commands.Cog, name="Register Verify Channel"): diff --git a/highscores.py b/highscores.py index 4e4ebf5..b35619f 100644 --- a/highscores.py +++ b/highscores.py @@ -9,8 +9,8 @@ import nextcord from nextcord.ext import commands import config -from utils.utils import open_highscores_config -from utils.utils import open_highscores_data +from utils.data_storage import open_highscores_config +from utils.data_storage import open_highscores_data logger = logging.getLogger("nextcord") logger.setLevel(logging.DEBUG) diff --git a/utils/data_storage.py b/utils/data_storage.py new file mode 100644 index 0000000..ff4015c --- /dev/null +++ b/utils/data_storage.py @@ -0,0 +1,51 @@ +"""Functions for retreiving and storing highscores data and configurations.""" + +import os +import json + +HIGHSCORES_CONFIG_FILE = "highscores_config.json" +HIGHSCORES_DATA_FILE = "highscores_data.json" + + +def open_highscores_config(): + """ + Opens the highscores config file and returns. + @return: dict of highscores config. + """ + # Load bot config + with open(HIGHSCORES_CONFIG_FILE, encoding="utf-8", mode="r") as file: + return json.load(file) + + +def open_highscores_data(): + """ + Opens a highscores_data.json file if one exists, otherwise creates a skeleton of the data. + @return: dict of highscores data. + """ + if os.path.exists(HIGHSCORES_DATA_FILE): + with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="r") as file: + data = json.load(file) + else: + # create the json object + data = {"highscore_channel_id": -1, "verification_channel_id": -1} + config = open_highscores_config() + for boss in config["bosses"]: + data[boss["boss"]] = {"message_id": 0} + boss_categories = {} + for key, value in config["categories"].items(): + rank_list = [] + boss_categories[key] = rank_list + if boss["hardmode"] and value["hardmode"]: + rank_list = [] + boss_categories["Hardmode " + key] = rank_list + data[boss["boss"]]["categories"] = boss_categories + return data + + +def save_highscores_data(data): + """ + Saves the highscores data dict into a json file. + @param data: dict of highscores data. + """ + with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="w") as file: + file.write(json.dumps(data, indent=4)) diff --git a/utils/utils.py b/utils/utils.py index 96ce4d5..e0760d1 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -2,15 +2,16 @@ Contains various utlity functions for highscores bot """ -import json -import os import nextcord from nextcord.ext import commands from highscores import highscores_config # pylint: disable=import-error from highscores import highscores_data # pylint: disable=import-error +from utils.data_storage import save_highscores_data -HIGHSCORES_CONFIG_FILE = "highscores_config.json" -HIGHSCORES_DATA_FILE = "highscores_data.json" + +def create_verification_id(user: str, boss_name: str, category: str, score: str) -> str: + """Returns a verification id string for buttons.""" + return f"{user}:{boss_name}:{category}:{score}" def format_highscore_message(boss_name: str): @@ -42,50 +43,6 @@ def format_highscore_message(boss_name: str): return ret_string + "```" -def open_highscores_config(): - """ - Opens the highscores config file and returns. - @return: dict of highscores config. - """ - # Load bot config - with open(HIGHSCORES_CONFIG_FILE, encoding="utf-8", mode="r") as file: - return json.load(file) - - -def open_highscores_data(): - """ - Opens a highscores_data.json file if one exists, otherwise creates a skeleton of the data. - @return: dict of highscores data. - """ - if os.path.exists(HIGHSCORES_DATA_FILE): - with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="r") as file: - data = json.load(file) - else: - # create the json object - data = {"highscore_channel_id": -1, "verification_channel_id": -1} - config = open_highscores_config() - for boss in config["bosses"]: - data[boss["boss"]] = {"message_id": 0} - boss_categories = {} - for key, value in config["categories"].items(): - rank_list = [] - boss_categories[key] = rank_list - if boss["hardmode"] and value["hardmode"]: - rank_list = [] - boss_categories["Hardmode " + key] = rank_list - data[boss["boss"]]["categories"] = boss_categories - return data - - -def save_highscores_data(data): - """ - Saves the highscores data dict into a json file. - @param data: dict of highscores data. - """ - with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="w") as file: - file.write(json.dumps(data, indent=4)) - - async def send_highscore_message(channel, boss_name: str): """ Function takes care of checking if a message exists for a boss, then