From 84cf3781ee468afec41d5d00cfe97aeb894cdff0 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Sun, 3 Jul 2022 23:01:24 -0400 Subject: [PATCH] Adding map to track message id to boss. --- .gitignore | 3 ++- highscores.py | 4 +++- utilities/data_storage.py | 24 ++++++++++++++++++++++++ utilities/utils.py | 6 ++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 3d52792..43bb43d 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,5 @@ dmypy.json nextcord.log # data file for bot -highscores_data.json \ No newline at end of file +highscores_data.json +highscores_message_map.json \ No newline at end of file diff --git a/highscores.py b/highscores.py index 0831883..b424c36 100644 --- a/highscores.py +++ b/highscores.py @@ -11,6 +11,7 @@ from nextcord.ext import commands import config from utilities.data_storage import open_highscores_config from utilities.data_storage import open_highscores_data +from utilities.data_storage import open_message_map logger = logging.getLogger("nextcord") logger.setLevel(logging.DEBUG) @@ -21,6 +22,7 @@ logger.addHandler(handler) highscores_config = open_highscores_config() highscores_data = open_highscores_data() +highscores_message_map = open_message_map() def main(): @@ -30,7 +32,7 @@ def main(): intents = nextcord.Intents.default() # Required in order to read messages (eg. prefix commands) - intents.message_content = True + intents.message_content = True # pylint: disable=assigning-non-slot # To enable the guilds priveleged intent: # intents.guilds = True diff --git a/utilities/data_storage.py b/utilities/data_storage.py index ff4015c..51bf03c 100644 --- a/utilities/data_storage.py +++ b/utilities/data_storage.py @@ -5,6 +5,7 @@ import json HIGHSCORES_CONFIG_FILE = "highscores_config.json" HIGHSCORES_DATA_FILE = "highscores_data.json" +HIGHSCORES_MESSAGE_MAP_FILE = "highscores_message_map.json" def open_highscores_config(): @@ -42,6 +43,20 @@ def open_highscores_data(): return data +def open_message_map(): + """ + Opens a highscore_message_map.json file if one exists. Otherwise returns empty dict. + @return: dict message_map of message to boss. + """ + if os.path.exists(HIGHSCORES_MESSAGE_MAP_FILE): + with open(HIGHSCORES_MESSAGE_MAP_FILE, encoding="utf-8", mode="r") as file: + message_map = json.load(file) + else: + # create the json object + message_map = {} + return message_map + + def save_highscores_data(data): """ Saves the highscores data dict into a json file. @@ -49,3 +64,12 @@ def save_highscores_data(data): """ with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="w") as file: file.write(json.dumps(data, indent=4)) + + +def save_message_map(message_map): + """ + Saves the highscores message mao data dict into a json file. + @param map: dict map of message to boss. + """ + with open(HIGHSCORES_MESSAGE_MAP_FILE, encoding="utf-8", mode="w") as file: + file.write(json.dumps(message_map, indent=4)) diff --git a/utilities/utils.py b/utilities/utils.py index 3974c46..10e05c6 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -5,7 +5,9 @@ Contains various utlity functions for highscores bot import nextcord from highscores import highscores_config # pylint: disable=import-error from highscores import highscores_data # pylint: disable=import-error +from highscores import highscores_message_map # pylint: disable=import-error from utilities.data_storage import save_highscores_data # pylint: disable=import-error +from utilities.data_storage import save_message_map # pylint: disable=import-error def format_highscore_message(boss_name: str): @@ -56,6 +58,10 @@ async def send_highscore_message(channel, boss_name: str): message_id = message.id highscores_data[boss_name]["message_id"] = message_id + # save message -> boss_name in message_map + highscores_message_map[message_id] = boss_name + save_message_map(highscores_message_map) + async def submit_score( interaction: nextcord.Interaction, user: str, boss_name: str, category: str, score: str