Adding map to track message id to boss.

This commit is contained in:
alwayscompiling
2022-07-03 23:01:24 -04:00
parent 8a20c1f50a
commit 84cf3781ee
4 changed files with 35 additions and 2 deletions
+24
View File
@@ -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))
+6
View File
@@ -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