Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b134a8107d | ||
|
|
71611502cf | ||
|
|
d30382dd91 | ||
|
|
68c15c48a5 | ||
|
|
46dcb81a9a | ||
|
|
96c6bd7935 |
@@ -5,7 +5,7 @@ Bot command to post the highscores
|
||||
from nextcord.ext import commands
|
||||
import highscores # pylint: disable=import-error
|
||||
from utils import data_storage # pylint: disable=import-error
|
||||
from utils import message_formatter # pylint: disable=import-error
|
||||
from utils import highscore_message # pylint: disable=import-error
|
||||
|
||||
|
||||
class PostHighscores(commands.Cog, name="Post Highscores"):
|
||||
@@ -16,7 +16,8 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
|
||||
|
||||
@commands.command(name="post-scores")
|
||||
async def post_highscores(self, ctx: commands.Context):
|
||||
"""A command which posts the highscore in the designated channel. Sends warning if no channel has been registered.
|
||||
"""
|
||||
A command which posts the highscore in the designated channel. Sends warning if no channel has been registered.
|
||||
Usage:
|
||||
```
|
||||
?post-scores
|
||||
@@ -24,25 +25,14 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
|
||||
"""
|
||||
# temporarily sending config data...
|
||||
channel_id = highscores.highscores_data["channel_id"]
|
||||
if (channel_id != -1):
|
||||
print(
|
||||
f"Printing out highscores information in channel {channel_id}")
|
||||
channel = self.bot.get_channel(channel_id)
|
||||
for boss in highscores.highscores_config["bosses"]:
|
||||
highscore_string = message_formatter.format_boss_highscore(
|
||||
boss)
|
||||
message = await channel.send(highscore_string)
|
||||
message_id = message.id
|
||||
|
||||
# check if there is a dict for the boss in the highscores data
|
||||
if not boss["boss"] in highscores.highscores_data:
|
||||
highscores.highscores_data[boss["boss"]] = {}
|
||||
|
||||
highscores.highscores_data[boss["boss"]
|
||||
]["message_id"] = message_id
|
||||
else:
|
||||
response = "Highscores channel is not registered. Register with \"?register\" command."
|
||||
print(f"Printing out highscores information in channel {channel_id}")
|
||||
channel = self.bot.get_channel(channel_id)
|
||||
if channel is None:
|
||||
response = 'Registered Highscores channel does not exist or was never registered. Register with "?register" command.'
|
||||
await ctx.send(response)
|
||||
return
|
||||
for boss in highscores.highscores_config["bosses"]:
|
||||
await highscore_message.send_highscore_message(channel, boss)
|
||||
|
||||
data_storage.save_highscores_data(highscores.highscores_data)
|
||||
|
||||
|
||||
+3
-6
@@ -2,7 +2,6 @@
|
||||
main file for pvm-highscores bot
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
|
||||
@@ -12,12 +11,10 @@ from nextcord.ext import commands
|
||||
import config
|
||||
from utils import data_storage
|
||||
|
||||
logger = logging.getLogger('nextcord')
|
||||
logger = logging.getLogger("nextcord")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
handler = logging.FileHandler(
|
||||
filename='nextcord.log', encoding='utf-8', mode='w')
|
||||
handler.setFormatter(logging.Formatter(
|
||||
'%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
|
||||
handler = logging.FileHandler(filename="nextcord.log", encoding="utf-8", mode="w")
|
||||
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
|
||||
logger.addHandler(handler)
|
||||
|
||||
|
||||
|
||||
+45
-45
@@ -2,183 +2,183 @@
|
||||
"bosses": [
|
||||
{
|
||||
"boss": "The Ambassador",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Araxxi",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Arch-Glacor",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Astellarn",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "The Barrows Brothers",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "The Barrows: Rise of the Six",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Beastmaster Durzag",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Black Stone Dragon",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Chaos Elemental",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Commander Zilyana",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Corporeal Beast",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Crassian Leviathan",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Croesus",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Dagannoth Kings",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "General Graardor",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Giant Mole",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Gregorovic",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Har-Aken",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Helwyr",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Kalphite King",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Kalphite Queen",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Kerapac",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "King Black Dragon",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Kree'arra",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "K'ril Tsutsaroth",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Legiones",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "The Magister",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Masuta the Ascended",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Nex",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Nex - Angel of Death",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Queen Black Dragon",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Raksha",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Rex Matriarchs",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "The Sanctum Guardian",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Seiryu the Azure Serpent",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Solak",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Taraket the Necromancer",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Telos",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "The Twin Furies",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "TzKal-Zuk",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "TzTok-Jad",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
},
|
||||
{
|
||||
"boss": "Verak Lith",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Vindicta & Gorvek",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Vorago",
|
||||
"hardmode": "true"
|
||||
"hardmode": true
|
||||
},
|
||||
{
|
||||
"boss": "Yakamaru",
|
||||
"hardmode": "false"
|
||||
"hardmode": false
|
||||
}
|
||||
],
|
||||
"categories": [
|
||||
|
||||
+22
-7
@@ -14,13 +14,28 @@ 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:
|
||||
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 = {}
|
||||
|
||||
data = {"channel_id": -1}
|
||||
config = open_highscores_config()
|
||||
size = config["highscore_size"]
|
||||
for boss in config["bosses"]:
|
||||
data[boss["boss"]] = {"message_id": -1}
|
||||
for category in config["categories"]:
|
||||
rank_list = []
|
||||
for i in range(size): # pylint: disable=unused-variable
|
||||
rank = {"player": "", "score": -1}
|
||||
rank_list.append(rank)
|
||||
data[boss["boss"]][category["category"]] = rank_list
|
||||
if boss["hardmode"] and category["hardmode"]:
|
||||
rank_list = []
|
||||
for i in range(size): # pylint: disable=unused-variable
|
||||
rank = {"player": "", "score": -1}
|
||||
rank_list.append(rank)
|
||||
data[boss["boss"]][category["category"] + "_hardmode"] = rank_list
|
||||
return data
|
||||
|
||||
|
||||
@@ -30,14 +45,14 @@ def open_highscores_config():
|
||||
@return: dict of highscores config.
|
||||
"""
|
||||
# Load bot config
|
||||
with open(HIGHSCORES_CONFIG_FILE, encoding='utf-8', mode='r') as file:
|
||||
with open(HIGHSCORES_CONFIG_FILE, encoding="utf-8", mode="r") as file:
|
||||
return json.load(file)
|
||||
|
||||
|
||||
def save_highscores_data(data):
|
||||
"""
|
||||
Saves the highscores data dict into a json file.
|
||||
@param: dict of highscores data.
|
||||
@param data: dict of highscores data.
|
||||
"""
|
||||
with open(HIGHSCORES_DATA_FILE, encoding='utf-8', mode='w') as file:
|
||||
with open(HIGHSCORES_DATA_FILE, encoding="utf-8", mode="w") as file:
|
||||
file.write(json.dumps(data, indent=4))
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
"""
|
||||
Contains various functions for sending/editing the highscore messages.
|
||||
"""
|
||||
|
||||
import nextcord
|
||||
import highscores # pylint: disable=import-error
|
||||
|
||||
|
||||
def format_highscore_message(boss):
|
||||
"""
|
||||
returns a formattedd string for a boss's highscore
|
||||
@param boss: the dictionary object for the boss
|
||||
@return the formatted string
|
||||
"""
|
||||
boss_name = boss["boss"]
|
||||
ret_string = f"**{boss_name}**\n"
|
||||
|
||||
# Gather all categories and add to string
|
||||
for category in highscores.highscores_config["categories"]:
|
||||
category_name = category["category"]
|
||||
ret_string += f"{category_name}".ljust(30, " ")
|
||||
ret_string += "\n"
|
||||
|
||||
highscore_size = highscores.highscores_config["highscore_size"]
|
||||
|
||||
# list rankings
|
||||
for i in range(highscore_size):
|
||||
for category in highscores.highscores_config["categories"]:
|
||||
ret_string += f"{i+1}: submit your score".ljust(30, " ")
|
||||
ret_string += "\n"
|
||||
|
||||
# if boss is hardmode, repeat above for hardmode applicable categories
|
||||
if boss["hardmode"]:
|
||||
for category in highscores.highscores_config["categories"]:
|
||||
if category["hardmode"]:
|
||||
category_name = category["category"]
|
||||
ret_string += f"{category_name}".ljust(30, " ")
|
||||
|
||||
ret_string += "\n"
|
||||
|
||||
for i in range(highscore_size):
|
||||
for category in highscores.highscores_config["categories"]:
|
||||
if category["hardmode"]:
|
||||
ret_string += f"{i+1}: (name here)".ljust(30, " ")
|
||||
ret_string += "\n"
|
||||
return ret_string
|
||||
|
||||
|
||||
async def send_highscore_message(channel, boss):
|
||||
"""
|
||||
Function takes care of checking if a message exists for a boss, then creating and sending/editing the message
|
||||
@param channel: the channel to send the message in/search for message id
|
||||
@param boss: the boss to send the message about
|
||||
"""
|
||||
highscore_string = format_highscore_message(boss)
|
||||
|
||||
message_id = highscores.highscores_data[boss["boss"]]["message_id"]
|
||||
try:
|
||||
message = await channel.fetch_message(message_id)
|
||||
await message.edit(content=highscore_string)
|
||||
except nextcord.NotFound:
|
||||
# message doesn't exist.
|
||||
message = await channel.send(highscore_string)
|
||||
message_id = message.id
|
||||
highscores.highscores_data[boss["boss"]]["message_id"] = message_id
|
||||
@@ -1,45 +0,0 @@
|
||||
"""
|
||||
Contains various function to format the highscore messages
|
||||
"""
|
||||
|
||||
import highscores # pylint: disable=import-error
|
||||
|
||||
|
||||
def format_boss_highscore(boss):
|
||||
"""
|
||||
returns a formattedd string for a boss's highscore
|
||||
@param the dictionary object for the boss
|
||||
@return the formatted string
|
||||
"""
|
||||
boss_name = boss["boss"]
|
||||
ret_string = f"**{boss_name}**\n"
|
||||
|
||||
# Gather all categories and add to string
|
||||
for category in highscores.highscores_config["categories"]:
|
||||
category_name = category["category"]
|
||||
ret_string += f"{category_name}\t\t"
|
||||
ret_string += "\n"
|
||||
|
||||
highscore_size = highscores.highscores_config["highscore_size"]
|
||||
|
||||
# list rankings
|
||||
for i in range(highscore_size):
|
||||
for category in highscores.highscores_config["categories"]:
|
||||
ret_string += f"{i+1}: (name here)\t\t"
|
||||
ret_string += "\n"
|
||||
|
||||
# if boss is hardmode, repeat above for hardmode applicable categories
|
||||
if boss["hardmode"]:
|
||||
for category in highscores.highscores_config["categories"]:
|
||||
if category["hardmode"]:
|
||||
category_name = category["category"]
|
||||
ret_string += f"{category_name}\t\t"
|
||||
|
||||
ret_string += "\n"
|
||||
|
||||
for i in range(highscore_size):
|
||||
for category in highscores.highscores_config["categories"]:
|
||||
if category["hardmode"]:
|
||||
ret_string += f"{i+1}: (name here)\t\t"
|
||||
ret_string += "\n"
|
||||
return ret_string
|
||||
Reference in New Issue
Block a user