Compare commits

...
3 changed files with 25 additions and 41 deletions
+6
View File
@@ -4,6 +4,7 @@ main file for pvm-highscores bot
import logging
import os
import sys
import nextcord
from nextcord.ext import commands
@@ -32,6 +33,11 @@ submission_objects: dict = {}
def main():
"""Main function for bot."""
min_python_version = (3, 7)
if sys.version_info < min_python_version:
print("ERROR: Python version must be >= 3.7")
sys.exit()
# Allows privledged intents for monitoring members joining, roles editing, and role assignments
# These need to be enabled in the developer portal as well
intents = nextcord.Intents.default()
+9 -32
View File
@@ -32,8 +32,8 @@ class HighscoresData:
skeleton["tables"][boss] = {"message_id": 0}
boss_categories = {}
for category in categories:
rank_list = []
boss_categories[category] = rank_list
scores_list = {}
boss_categories[category] = scores_list
skeleton["tables"][boss]["categories"] = boss_categories
self._data[str(guild_id)] = skeleton
@@ -66,40 +66,17 @@ class HighscoresData:
minutes = score.split(":")[0]
seconds = score.split(":")[1]
score_seconds = int(minutes) * 60 + float(seconds)
score_tuple = (user, score, score_seconds)
sort_index = 2
else:
score_tuple = (user, int(score))
score_list = [score, score_seconds]
sort_index = 1
else:
score_list = [int(score)]
sort_index = 0
# if user is in an existing tuple, delete it.
users_scores: "list[tuple]" = [score_tuple]
for tup in scores:
if user == tup[0]:
# Add tup to users_scores, remove from scores
users_scores.append(tup)
# Will not ensure new score is better than old one. Allows for correcting mistakes.
# sort users_scores
users_scores.sort(
key=lambda x: x[sort_index],
reverse=not ascending,
)
scores[user] = score_list
for index, tup in enumerate(users_scores):
if index == 0:
# first score is the best score.
if tup not in scores:
scores.append(tup)
else:
# discard all other scores from scores list.
if tup in scores:
scores.remove(tup)
# sort scores.
scores.sort(
key=lambda x: x[sort_index],
reverse=not ascending,
)
scores = dict(sorted(scores.items(), key=lambda x: x[1][sort_index], reverse=not ascending))
self._data[str(guild_id)]["tables"][boss_name]["categories"][category] = scores
save_highscores_data(self._data)
+10 -9
View File
@@ -17,27 +17,28 @@ def format_highscore_message(boss_name: str, guild_id: int):
ret_string = f"**{boss_name}**\n```"
# Gather all categories and add to string
for key, value in highscores_data.get_categories_data(guild_id, boss_name):
ret_string += f"{key}".ljust(26, " ")
for category_name in highscores_data.get_table_categories(guild_id, boss_name):
ret_string += f"{category_name}".ljust(26, " ")
ret_string += "\n"
highscore_size = highscores_data.get_config_highscore_size()
# list rankings in a single line. Start with first place for each category and then continue
for i in range(highscore_size):
for key, value in highscores_data.get_categories_data(guild_id, boss_name):
if len(value) > i:
score_pair = value[i]
for category_name, category_data in highscores_data.get_categories_data(
guild_id, boss_name
):
if len(category_data) > i:
# pad username to max length.
username: str = score_pair[0].ljust(
username: str = list(category_data.keys())[i].ljust(
highscores_data.get_guild_username_length(guild_id), " "
)
score: str = str(score_pair[1])
# turns category data value into a string. Very janky.
score: str = str(list(category_data.values())[i][0])
# pad score to correct length
# MM:SS.ms is 7 characters long
if highscores_data.get_config_time_record(key):
if highscores_data.get_config_time_record(category_name):
# if there are single digit minutes, add a space for formatting.
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
score = " " + score