Update send highscore message to be more robust.

This commit is contained in:
2025-03-12 09:57:17 -04:00
parent 304b089113
commit ed68c4174f
+11 -2
View File
@@ -10,6 +10,7 @@ from sqlmodel import col
from database.db import engine
from database.models import Category, CategoryGroup, Guild, Score
from main import agon_logger
MAX_LINE_LENGTH = 142
@@ -327,7 +328,9 @@ def get_time_str(time_in: float) -> str:
return time.strftime(f"%-M:%S.{milliseconds}", time.gmtime(time_in))
async def send_highscore_message(session: Session, guild: nextcord.Guild, group: CategoryGroup) -> nextcord.Message:
async def send_highscore_message(
session: Session, guild: nextcord.Guild, group: CategoryGroup
) -> nextcord.Message | None:
"""
Function takes care of checking if a message exists for a group.
@@ -339,11 +342,17 @@ async def send_highscore_message(session: Session, guild: nextcord.Guild, group:
group (CategoryGroup): group to post to the message.
Returns:
nextcord.Message: Message object that was posted with group contents.
nextcord.Message: Message object that was posted with group contents. None if cannot send message.
"""
highscore_string = format_highscore_message(session, group)
channel = guild.get_channel(group.channel_id)
if not isinstance(channel, nextcord.TextChannel):
agon_logger.error(
"Channel id %s in guild %s is does not correspond to a TextChannel.", channel.id, channel.guild.id
)
return None
if group.message_id is None:
# message doesn't exist.
message: nextcord.Message = await channel.send(highscore_string)