From 814baa27d6563a4c45d5caabe85b1af02f4a7ebf Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Wed, 30 Nov 2022 21:54:05 -0500 Subject: [PATCH] Update message listener to new database. --- cogs/message_listener/__init__.py | 53 +++++++++++++++---------------- cogs/presence/__init__.py | 3 +- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/cogs/message_listener/__init__.py b/cogs/message_listener/__init__.py index bd21f77..c8971c6 100644 --- a/cogs/message_listener/__init__.py +++ b/cogs/message_listener/__init__.py @@ -2,15 +2,16 @@ Defines cog for on message listener. """ -from datetime import date -import random import re -from nextcord.ext import commands import nextcord +from nextcord.ext import commands +from sqlalchemy import select +from sqlalchemy.orm import aliased -from classes.submission import SubmissionState # pylint: disable=import-error -from highscores import highscores_data, submission_objects # pylint: disable=import-error +# pylint: disable=import-error +from classes.submission import SubmissionState +from highscores import Category, CategoryGroup, Guild, session, submission_objects class SubmissionListener(commands.Cog, name="Message Listener"): @@ -28,21 +29,13 @@ class SubmissionListener(commands.Cog, name="Message Listener"): guild_id = message.guild.id - # troll addition for friends birthday - if guild_id == 239758371358638080 and date.today() == date(2022, 7, 31): - # 5% chance to send the message - chance = random.randint(1, 20) - if chance == 1: - await message.channel.send("Happy Birthday Chris <@642330132958281750>") - if submission_objects.has_user(message.author.id): submission = submission_objects.get_user_submission(message.author.id) message_id = submission.message_id - submission_channel_id = highscores_data.get_guild(guild_id).submission_channel + stmt = select(Guild.SubmissionChannel).where(Guild.GuildId == guild_id) + submission_channel_id = session.scalar(stmt) submission_message: nextcord.Message = ( - await self.bot.get_guild(guild_id) - .get_channel(submission_channel_id) - .fetch_message(message_id) + await self.bot.get_guild(guild_id).get_channel(submission_channel_id).fetch_message(message_id) ) # need check if correct user and correct channel @@ -50,17 +43,23 @@ class SubmissionListener(commands.Cog, name="Message Listener"): state = submission.submission_state if state == SubmissionState.SCORE and message.content: - # check with regex if score matches correctly - is_time_record = ( - highscores_data.get_guild(guild_id) - .get_table(submission.table) - .get_category(submission.category) - .time_record + subq = ( + select(CategoryGroup) + .where(CategoryGroup.GuildId == guild_id) + .where(CategoryGroup.GroupName == submission.table) + .subquery() + ) + aliased_category_group = aliased(CategoryGroup, subq) + stmt = ( + select(Category.IsTimeRecord) + .where(Category.GroupId == aliased_category_group.GroupId) + .where(Category.CategoryName == submission.category) ) - time_regex = re.search( - "^([0-9]{1,2}:[0-9]|[0-9])?[0-9](\\.[0-9])?$", message.content - ) + # check with regex if score matches correctly + is_time_record = session.scalar(stmt) + + time_regex = re.search("^([0-9]{1,2}:[0-9]|[0-9])?[0-9](\\.[0-9])?$", message.content) int_regex = re.search("^[0-9]+$", message.content) if time_regex is None and is_time_record: embed = submission_message.embeds[0] @@ -97,9 +96,7 @@ class SubmissionListener(commands.Cog, name="Message Listener"): files=[await attch.to_file() for attch in message.attachments], ) elif state == SubmissionState.NAME and message.content: - submission.username = message.content[ - : highscores_data.get_guild(guild_id).username_length - ] + submission.username = message.content embed = submission.get_embed() await submission_message.edit( embed=embed, diff --git a/cogs/presence/__init__.py b/cogs/presence/__init__.py index 4ccf622..6646e08 100644 --- a/cogs/presence/__init__.py +++ b/cogs/presence/__init__.py @@ -4,9 +4,8 @@ Functions for controlling the bot's presence. import random -from nextcord.ext import commands -from nextcord.ext import tasks import nextcord +from nextcord.ext import commands, tasks class BotPresence(commands.Cog, name="Bot Presence"):