Create function to send message. Function handles editing message if exists.
This commit is contained in:
@@ -16,7 +16,8 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
|
|||||||
|
|
||||||
@commands.command(name="post-scores")
|
@commands.command(name="post-scores")
|
||||||
async def post_highscores(self, ctx: commands.Context):
|
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:
|
Usage:
|
||||||
```
|
```
|
||||||
?post-scores
|
?post-scores
|
||||||
@@ -24,24 +25,20 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
|
|||||||
"""
|
"""
|
||||||
# temporarily sending config data...
|
# temporarily sending config data...
|
||||||
channel_id = highscores.highscores_data["channel_id"]
|
channel_id = highscores.highscores_data["channel_id"]
|
||||||
if (channel_id != -1):
|
if "channel_id" in highscores.highscores_data:
|
||||||
print(
|
channel_id = highscores.highscores_data["channel_id"]
|
||||||
f"Printing out highscores information in channel {channel_id}")
|
print(f"Printing out highscores information in channel {channel_id}")
|
||||||
channel = self.bot.get_channel(channel_id)
|
channel = self.bot.get_channel(channel_id)
|
||||||
|
if channel is None:
|
||||||
|
response = 'Registered Highscores channel does not exist. Register again with "?register" command.'
|
||||||
|
await ctx.send(response)
|
||||||
|
return
|
||||||
for boss in highscores.highscores_config["bosses"]:
|
for boss in highscores.highscores_config["bosses"]:
|
||||||
highscore_string = highscore_message.format_highscore_message(
|
await highscore_message.send_highscore_message(channel, boss)
|
||||||
boss)
|
break
|
||||||
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:
|
else:
|
||||||
response = "Highscores channel is not registered. Register with \"?register\" command."
|
response = 'Highscores channel is not registered. Register with "?register" command.'
|
||||||
await ctx.send(response)
|
await ctx.send(response)
|
||||||
|
|
||||||
data_storage.save_highscores_data(highscores.highscores_data)
|
data_storage.save_highscores_data(highscores.highscores_data)
|
||||||
|
|||||||
+3
-6
@@ -2,7 +2,6 @@
|
|||||||
main file for pvm-highscores bot
|
main file for pvm-highscores bot
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -12,12 +11,10 @@ from nextcord.ext import commands
|
|||||||
import config
|
import config
|
||||||
from utils import data_storage
|
from utils import data_storage
|
||||||
|
|
||||||
logger = logging.getLogger('nextcord')
|
logger = logging.getLogger("nextcord")
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
handler = logging.FileHandler(
|
handler = logging.FileHandler(filename="nextcord.log", encoding="utf-8", mode="w")
|
||||||
filename='nextcord.log', encoding='utf-8', mode='w')
|
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
|
||||||
handler.setFormatter(logging.Formatter(
|
|
||||||
'%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
|
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ def open_highscores_config():
|
|||||||
def save_highscores_data(data):
|
def save_highscores_data(data):
|
||||||
"""
|
"""
|
||||||
Saves the highscores data dict into a json file.
|
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))
|
file.write(json.dumps(data, indent=4))
|
||||||
|
|||||||
@@ -2,13 +2,15 @@
|
|||||||
Contains various functions for sending/editing the highscore messages.
|
Contains various functions for sending/editing the highscore messages.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import nextcord
|
||||||
|
from nextcord.ext import commands
|
||||||
import highscores # pylint: disable=import-error
|
import highscores # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
def format_highscore_message(boss):
|
def format_highscore_message(boss):
|
||||||
"""
|
"""
|
||||||
returns a formattedd string for a boss's highscore
|
returns a formattedd string for a boss's highscore
|
||||||
@param the dictionary object for the boss
|
@param boss: the dictionary object for the boss
|
||||||
@return the formatted string
|
@return the formatted string
|
||||||
"""
|
"""
|
||||||
boss_name = boss["boss"]
|
boss_name = boss["boss"]
|
||||||
@@ -17,7 +19,7 @@ def format_highscore_message(boss):
|
|||||||
# Gather all categories and add to string
|
# Gather all categories and add to string
|
||||||
for category in highscores.highscores_config["categories"]:
|
for category in highscores.highscores_config["categories"]:
|
||||||
category_name = category["category"]
|
category_name = category["category"]
|
||||||
ret_string += f"{category_name}".ljust(30, ' ')
|
ret_string += f"{category_name}".ljust(30, " ")
|
||||||
ret_string += "\n"
|
ret_string += "\n"
|
||||||
|
|
||||||
highscore_size = highscores.highscores_config["highscore_size"]
|
highscore_size = highscores.highscores_config["highscore_size"]
|
||||||
@@ -25,7 +27,7 @@ def format_highscore_message(boss):
|
|||||||
# list rankings
|
# list rankings
|
||||||
for i in range(highscore_size):
|
for i in range(highscore_size):
|
||||||
for category in highscores.highscores_config["categories"]:
|
for category in highscores.highscores_config["categories"]:
|
||||||
ret_string += f"{i+1}: (name here)".ljust(30, ' ')
|
ret_string += f"{i+1}: submit your score".ljust(30, " ")
|
||||||
ret_string += "\n"
|
ret_string += "\n"
|
||||||
|
|
||||||
# if boss is hardmode, repeat above for hardmode applicable categories
|
# if boss is hardmode, repeat above for hardmode applicable categories
|
||||||
@@ -33,13 +35,48 @@ def format_highscore_message(boss):
|
|||||||
for category in highscores.highscores_config["categories"]:
|
for category in highscores.highscores_config["categories"]:
|
||||||
if category["hardmode"]:
|
if category["hardmode"]:
|
||||||
category_name = category["category"]
|
category_name = category["category"]
|
||||||
ret_string += f"{category_name}".ljust(30, ' ')
|
ret_string += f"{category_name}".ljust(30, " ")
|
||||||
|
|
||||||
ret_string += "\n"
|
ret_string += "\n"
|
||||||
|
|
||||||
for i in range(highscore_size):
|
for i in range(highscore_size):
|
||||||
for category in highscores.highscores_config["categories"]:
|
for category in highscores.highscores_config["categories"]:
|
||||||
if category["hardmode"]:
|
if category["hardmode"]:
|
||||||
ret_string += f"{i+1}: (name here)".ljust(30, ' ')
|
ret_string += f"{i+1}: (name here)".ljust(30, " ")
|
||||||
ret_string += "\n"
|
ret_string += "\n"
|
||||||
return ret_string
|
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)
|
||||||
|
|
||||||
|
if (boss["boss"] in highscores.highscores_data) and (
|
||||||
|
"message_id" in highscores.highscores_data[boss["boss"]]
|
||||||
|
):
|
||||||
|
message_id = highscores.highscores_data[boss["boss"]]["message_id"]
|
||||||
|
try:
|
||||||
|
message = await channel.fetch_message(message_id)
|
||||||
|
await message.edit(highscore_string)
|
||||||
|
except nextcord.NotFound:
|
||||||
|
# message doesn't exist.
|
||||||
|
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:
|
||||||
|
# make a new message
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user