Update send highscore message to be more robust.
This commit is contained in:
+11
-2
@@ -10,6 +10,7 @@ from sqlmodel import col
|
|||||||
|
|
||||||
from database.db import engine
|
from database.db import engine
|
||||||
from database.models import Category, CategoryGroup, Guild, Score
|
from database.models import Category, CategoryGroup, Guild, Score
|
||||||
|
from main import agon_logger
|
||||||
|
|
||||||
MAX_LINE_LENGTH = 142
|
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))
|
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.
|
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.
|
group (CategoryGroup): group to post to the message.
|
||||||
|
|
||||||
Returns:
|
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)
|
highscore_string = format_highscore_message(session, group)
|
||||||
channel = guild.get_channel(group.channel_id)
|
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:
|
if group.message_id is None:
|
||||||
# message doesn't exist.
|
# message doesn't exist.
|
||||||
message: nextcord.Message = await channel.send(highscore_string)
|
message: nextcord.Message = await channel.send(highscore_string)
|
||||||
|
|||||||
Reference in New Issue
Block a user