Update message listener to new database.

This commit is contained in:
alwayscompiling
2022-11-30 21:54:05 -05:00
parent 9531dca892
commit 814baa27d6
2 changed files with 26 additions and 30 deletions
+25 -28
View File
@@ -2,15 +2,16 @@
Defines cog for on message listener. Defines cog for on message listener.
""" """
from datetime import date
import random
import re import re
from nextcord.ext import commands
import nextcord 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 # pylint: disable=import-error
from highscores import highscores_data, submission_objects # 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"): class SubmissionListener(commands.Cog, name="Message Listener"):
@@ -28,21 +29,13 @@ class SubmissionListener(commands.Cog, name="Message Listener"):
guild_id = message.guild.id 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): if submission_objects.has_user(message.author.id):
submission = submission_objects.get_user_submission(message.author.id) submission = submission_objects.get_user_submission(message.author.id)
message_id = submission.message_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 = ( submission_message: nextcord.Message = (
await self.bot.get_guild(guild_id) await self.bot.get_guild(guild_id).get_channel(submission_channel_id).fetch_message(message_id)
.get_channel(submission_channel_id)
.fetch_message(message_id)
) )
# need check if correct user and correct channel # need check if correct user and correct channel
@@ -50,17 +43,23 @@ class SubmissionListener(commands.Cog, name="Message Listener"):
state = submission.submission_state state = submission.submission_state
if state == SubmissionState.SCORE and message.content: if state == SubmissionState.SCORE and message.content:
# check with regex if score matches correctly subq = (
is_time_record = ( select(CategoryGroup)
highscores_data.get_guild(guild_id) .where(CategoryGroup.GuildId == guild_id)
.get_table(submission.table) .where(CategoryGroup.GroupName == submission.table)
.get_category(submission.category) .subquery()
.time_record )
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( # check with regex if score matches correctly
"^([0-9]{1,2}:[0-9]|[0-9])?[0-9](\\.[0-9])?$", message.content 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) int_regex = re.search("^[0-9]+$", message.content)
if time_regex is None and is_time_record: if time_regex is None and is_time_record:
embed = submission_message.embeds[0] 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], files=[await attch.to_file() for attch in message.attachments],
) )
elif state == SubmissionState.NAME and message.content: elif state == SubmissionState.NAME and message.content:
submission.username = message.content[ submission.username = message.content
: highscores_data.get_guild(guild_id).username_length
]
embed = submission.get_embed() embed = submission.get_embed()
await submission_message.edit( await submission_message.edit(
embed=embed, embed=embed,
+1 -2
View File
@@ -4,9 +4,8 @@ Functions for controlling the bot's presence.
import random import random
from nextcord.ext import commands
from nextcord.ext import tasks
import nextcord import nextcord
from nextcord.ext import commands, tasks
class BotPresence(commands.Cog, name="Bot Presence"): class BotPresence(commands.Cog, name="Bot Presence"):