Fix circular dependencies after refractor.

This commit is contained in:
alwayscompiling
2022-07-03 16:38:22 -04:00
parent 6d41baef98
commit dc0fd694d3
6 changed files with 62 additions and 54 deletions
+2 -2
View File
@@ -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"):
+1 -1
View File
@@ -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"):
@@ -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"):
+2 -2
View File
@@ -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)
+51
View File
@@ -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))
+5 -48
View File
@@ -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