Squashed commit of the following:

commit 3627419e12
Merge: 2db744d 3446ce9
Author: alwayscompiling <[email protected]>
Date:   Tue Dec 6 22:36:51 2022 -0500

    Merge branch '45-sql-connection-timing-out' into develop

commit 3446ce9ca2
Author: alwayscompiling <[email protected]>
Date:   Tue Dec 6 22:27:05 2022 -0500

    Change session usage to on use basis.

commit 2db744d170
Author: alwayscompiling <[email protected]>
Date:   Sun Dec 4 21:44:54 2022 -0500

    Add restart policy.

commit fa33be1b4a
Author: alwayscompiling <[email protected]>
Date:   Sun Dec 4 21:25:57 2022 -0500

    Renamed docker compose file.
This commit is contained in:
alwayscompiling
2022-12-06 23:02:01 -05:00
parent 582b88e248
commit d04b8c5e32
12 changed files with 359 additions and 307 deletions
+3 -1
View File
@@ -6,9 +6,10 @@ from enum import Enum
from nextcord import Embed
from sqlalchemy import select
from sqlalchemy.orm import Session
# pylint: disable=import-error
from utilities.sql_alchemy_connection import Category, CategoryGroup, session
from utilities.sql_data_classes import Category, CategoryGroup, engine
class SubmissionState(Enum):
@@ -199,6 +200,7 @@ class Submission:
.where(CategoryGroup.GuildId == self.guild_id)
.where(CategoryGroup.GroupId == self.group_id)
)
with Session(engine) as session:
embed.add_field(name="Group", value=session.scalar(stmt), inline=False)
if self.category_id:
stmt = (
+13 -5
View File
@@ -7,9 +7,10 @@ from typing import Optional
import nextcord
from nextcord.ext import commands
from sqlalchemy import distinct, select
from sqlalchemy.orm import Session
# pylint: disable=import-error
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, Score, session
from utilities.sql_data_classes import Category, CategoryGroup, Guild, Score, engine
from utilities.utils import (
autofill_categories,
autofill_groups,
@@ -70,6 +71,7 @@ class DataManagementCog(commands.Cog, name="Table Management"):
user_name (str): User to remove from highscores
"""
with Session(engine) as session:
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == user_name)
category_ids: "list[int]" = list(session.scalars(stmt))
@@ -83,7 +85,7 @@ class DataManagementCog(commands.Cog, name="Table Management"):
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
for group in session.scalars(stmt):
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
await send_highscore_message(session, interaction.guild.get_channel(group.ChannelId), group)
await interaction.send(f'User "{user_name}" was removed from all tables.', ephemeral=True)
@@ -107,6 +109,7 @@ class DataManagementCog(commands.Cog, name="Table Management"):
user_name (str): The user to remove. Is SlashOption.
"""
with Session(engine) as session:
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
group = session.scalar(stmt)
@@ -119,8 +122,10 @@ class DataManagementCog(commands.Cog, name="Table Management"):
session.delete(session.scalar(stmt))
session.commit()
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
await interaction.response.send_message(f'User "{user_name}" removed from "{category_name}".', ephemeral=True)
await send_highscore_message(session, interaction.guild.get_channel(group.ChannelId), group)
await interaction.response.send_message(
f'User "{user_name}" removed from "{category_name}".', ephemeral=True
)
@user.subcommand(description="Deletes a user's scores from entire group.")
async def from_group(
@@ -138,6 +143,7 @@ class DataManagementCog(commands.Cog, name="Table Management"):
user_name (str): The user to remove. Is SlashOption.
"""
with Session(engine) as session:
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
group = session.scalar(stmt)
category_ids = select(Category.CategoryId).where(Category.GroupId == int(group_id))
@@ -147,7 +153,7 @@ class DataManagementCog(commands.Cog, name="Table Management"):
session.delete(score)
session.commit()
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
await send_highscore_message(session, interaction.guild.get_channel(group.ChannelId), group)
await interaction.response.send_message(
f'User "{user_name}" removed from all categories in {group.GroupName}.', ephemeral=True
)
@@ -215,6 +221,8 @@ class DataManagementCog(commands.Cog, name="Table Management"):
old_username (str): Username to replace.
new_username (str): Username to replace old with.
"""
with Session(engine) as session:
stmt = select(Guild.VerificationChannel).where(Guild.GuildId == interaction.guild_id)
verify_channel = interaction.guild.get_channel(session.scalar(stmt))
+8 -3
View File
@@ -7,10 +7,11 @@ from typing import Optional
import nextcord
from nextcord.ext import commands
from sqlalchemy import select
from sqlalchemy.orm import Session
# pylint: disable=import-error
from cogs.submission.submission_views import SubmissionCreateButton
from utilities.sql_alchemy_connection import CategoryGroup, session
from utilities.sql_data_classes import CategoryGroup, engine
from utilities.utils import autofill_groups, send_highscore_message
@@ -55,6 +56,7 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
if not channel:
channel = interaction.channel
with Session(engine) as session:
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
@@ -90,6 +92,8 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
Args:
interaction (nextcord.Interaction): Interaction that invoked the command.
"""
with Session(engine) as session:
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == interaction.guild_id)
# awaiting response because posting scores takes a while
@@ -103,7 +107,7 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
error_response += 'Use "/register" to register channels.'
assert channel is not None, await interaction.response(error_response, ephemeral=True)
message: nextcord.Message = await send_highscore_message(channel, group)
message: nextcord.Message = await send_highscore_message(session, channel, group)
await message.edit(view=SubmissionCreateButton(self.bot))
await interaction.send(f"Posted all groups in {interaction.guild.name}'s highscores.", ephemeral=True)
@@ -125,6 +129,7 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
group_id (str): Str representation of Group id to refresh. Is SlashOption.
"""
with Session(engine) as session:
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
@@ -134,7 +139,7 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
channel = interaction.guild.get_channel(group.ChannelId)
message: nextcord.Message = await send_highscore_message(channel, group)
message: nextcord.Message = await send_highscore_message(session, channel, group)
await message.edit(view=SubmissionCreateButton(self.bot))
await interaction.response.send_message(f"Updated table {group.GroupName}!", ephemeral=True)
+11 -1
View File
@@ -7,9 +7,10 @@ from typing import Optional
import nextcord
from nextcord.ext import commands
from sqlalchemy import select
from sqlalchemy.orm import Session
# pylint: disable=import-error
from utilities.sql_alchemy_connection import CategoryGroup, Guild, session
from utilities.sql_data_classes import CategoryGroup, Guild, engine
from utilities.utils import autofill_groups
@@ -49,6 +50,7 @@ class SetCog(commands.Cog, name="Set command."):
length (int): Number of characters. Is SlashOption. Defaults to 26.
"""
with Session(engine) as session:
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
@@ -84,9 +86,11 @@ class SetCog(commands.Cog, name="Set command."):
channel (nextcord.abc.GuildChannel, optional): Channel to register
Is a SlashOption. Limited to text channels, Defaults to None.
"""
if not channel:
channel = interaction.channel
channel_id = channel.id
with Session(engine) as session:
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
@@ -116,8 +120,11 @@ class SetCog(commands.Cog, name="Set command."):
channel Optional[nextcord.abc.GuildChannel], optional): Channel to register
Is a SlashOption. Limited to text channels, Defaults to None.
"""
if not channel:
channel = interaction.channel
with Session(engine) as session:
channel_id = channel.id
stmt = select(Guild).where(Guild.GuildId == interaction.guild_id)
guild = session.scalar(stmt)
@@ -142,6 +149,7 @@ class SetCog(commands.Cog, name="Set command."):
length (int): Number of characters. Is SlashOption. Defaults to 12.
"""
with Session(engine) as session:
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
@@ -171,9 +179,11 @@ class SetCog(commands.Cog, name="Set command."):
channel (Optional[nextcord.abc.GuildChannel]): Channel to register
Is a SlashOption. Limited to text channels, Defaults to None.
"""
if not channel:
channel = interaction.channel
channel_id = channel.id
with Session(engine) as session:
stmt = select(Guild).where(Guild.GuildId == interaction.guild_id)
guild = session.scalar(stmt)
guild.VerificationChannel = channel_id
@@ -2,9 +2,10 @@
import nextcord
from sqlalchemy import distinct, exc, select
from sqlalchemy.orm import Session
# pylint: disable=import-error
from utilities.sql_alchemy_connection import Category, CategoryGroup, Score, session
from utilities.sql_data_classes import Category, CategoryGroup, Score, engine
from utilities.utils import send_highscore_message
@@ -43,6 +44,7 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
interaction (nextcord.Interaction): Interaction with the button
"""
with Session(engine) as session:
try:
embed_content = interaction.message.embeds[0].to_dict()
old_username: str = embed_content["fields"][0]["value"]
@@ -61,7 +63,7 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
for group in session.scalars(stmt):
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
await send_highscore_message(session, interaction.guild.get_channel(group.ChannelId), group)
content = interaction.message.content
content += f"\nApproved by <@{interaction.user.id}>."
+5 -1
View File
@@ -4,9 +4,10 @@ Defines cog for guild listeners. Contains functions for various guild events.
import nextcord
from nextcord.ext import commands
from sqlalchemy.orm import Session
# pylint: disable=import-error
from utilities.sql_alchemy_connection import Guild, session
from utilities.sql_data_classes import Guild, engine
from utilities.utils import add_preset_configuration
@@ -19,7 +20,10 @@ class GuildListener(commands.Cog, name="Guild Listener"):
@commands.Cog.listener()
async def on_guild_join(self, guild: nextcord.Guild):
"""Listener for Guild Join. Creates skeleton for new guild joined and saves it."""
new_guild = Guild(GuildId=guild.id)
with Session(engine) as session:
session.add(new_guild)
session.commit()
# This add config will be removed eventually. Config adding will have to be controlled by commands.
+4 -2
View File
@@ -7,12 +7,12 @@ import re
import nextcord
from nextcord.ext import commands
from sqlalchemy import select
from sqlalchemy.orm import aliased
from sqlalchemy.orm import Session
# pylint: disable=import-error
from classes.submission import SubmissionState
from highscores import submission_objects
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, session
from utilities.sql_data_classes import Category, Guild, engine
class SubmissionListener(commands.Cog, name="Message Listener"):
@@ -33,6 +33,8 @@ class SubmissionListener(commands.Cog, name="Message Listener"):
if submission_objects.has_user(message.author.id):
submission = submission_objects.get_user_submission(message.author.id)
message_id = submission.message_id
with Session(engine) as session:
stmt = select(Guild.SubmissionChannel).where(Guild.GuildId == guild_id)
submission_channel_id = session.scalar(stmt)
submission_message: nextcord.Message = (
+7 -2
View File
@@ -5,12 +5,12 @@ from typing import Optional
import nextcord
from nextcord.ext import commands
from sqlalchemy import select
from sqlalchemy.orm import aliased
from sqlalchemy.orm import Session, aliased
# pylint: disable=import-error
from classes.submission import Submission, SubmissionState
from utilities.sql_alchemy_connection import Category, Guild, CategoryGroup, session
from highscores import submission_objects
from utilities.sql_data_classes import Category, CategoryGroup, Guild, engine
from .verification_view import VerificationView
@@ -18,6 +18,8 @@ from .verification_view import VerificationView
def category_select_options(group_id: int) -> "list[nextcord.SelectOption]":
"""Returns list of categories for a given boss."""
options: "list[nextcord.SelectOption]" = []
with Session(engine) as session:
subq = select(CategoryGroup).where(CategoryGroup.GroupId == group_id).subquery()
cat_group_alias = aliased(CategoryGroup, subq)
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId)
@@ -110,6 +112,7 @@ class SubmitButton(nextcord.ui.Button):
self._bot = bot
async def callback(self, interaction: nextcord.Interaction):
with Session(engine) as session:
stmt = select(Guild.VerificationChannel).where(Guild.GuildId == interaction.guild_id)
verification_channel_id = session.scalar(stmt)
channel = interaction.guild.get_channel(verification_channel_id)
@@ -148,11 +151,13 @@ class SubmissionCreateButton(nextcord.ui.View):
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
): # pylint: disable=unused-argument
"""Button for score submission."""
# Don't allow a user to start a new submission if one already exists
if submission_objects.has_user(interaction.user.id):
await interaction.send("Please finish in progress submission first.", ephemeral=True)
return
with Session(engine) as session:
stmt = (
select(CategoryGroup.GroupId)
.where(CategoryGroup.GuildId == interaction.guild_id)
@@ -1,6 +1,7 @@
version: "3"
services:
sqldb:
restart: unless-stopped
image: "highscores_db"
container_name: "Highscores-DataBase"
volumes:
+2 -1
View File
@@ -9,7 +9,8 @@ import sys
import nextcord
from nextcord.ext import commands
import utilities.config as config
# pylint: disable=import-error
from utilities import config
from classes.submission import SubmissionMap
logger = logging.getLogger("nextcord")
@@ -3,17 +3,15 @@ Simple file to set up the sqlalchemy connection to database.
"""
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
import utilities.config as config
# pylint: disable=import-error
from utilities import config
# connect with sql alchemy and autoload schema
url = f"mysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
engine = create_engine(url)
connection = engine.connect()
Base = automap_base()
Base.prepare(autoload_with=engine)
session = Session(engine)
# define the classes
Guild = Base.classes.Guilds
+21 -7
View File
@@ -8,10 +8,10 @@ import time
import nextcord
import pandas as pd
from sqlalchemy import distinct, select
from sqlalchemy.orm import aliased
from sqlalchemy.orm import Session, aliased
# pylint: disable=import-error
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, Score, engine, session
from utilities.sql_data_classes import Category, CategoryGroup, Guild, Score, engine
MAX_LINE_LENGTH = 142
@@ -30,6 +30,7 @@ def autofill_categories(guild_id: int, group_id: int, category_begin: str) -> "d
dict[str, str]: List of all category names that start with category_begin.
"""
with Session(engine) as session:
# search database for all category names
subq = (
select(CategoryGroup)
@@ -65,6 +66,7 @@ def autofill_groups(guild_id: int, group_begin: str) -> "dict[str, str]":
dict[str, str]: Dict of group names and ids where group name start with group_begin. Key is name, value is id.
"""
with Session(engine) as session:
# Select all groups in guild.
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == guild_id)
@@ -98,6 +100,7 @@ def autofill_users_in_group_category(guild_id: int, group_id: int, category_id:
list[str]: List of users that start with user_begin.
"""
with Session(engine) as session:
# get all categories in the group
subq = (
select(CategoryGroup)
@@ -146,6 +149,8 @@ def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]":
Returns:
list[str]: All users with submissions in the guild.
"""
with Session(engine) as session:
group_ids = select(CategoryGroup.GroupId).where(CategoryGroup.GuildId == guild_id)
category_ids = select(Category.CategoryId).where(Category.GroupId.in_(group_ids))
stmt = select(distinct(Score.UserName)).where(Score.CategoryId.in_(category_ids))
@@ -161,18 +166,20 @@ def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]":
return users
def format_highscore_message(group: CategoryGroup) -> str:
def format_highscore_message(session: Session, group: CategoryGroup) -> str:
"""
Creates a formatted string for a boss's highscore discord message.
This formatted string is in discord's code block to ensure monospace text.
Loops through each category to build the message one place at a time.
Args:
group (group): group object that contains the contents of the message.
session (Session): the sqlalchemy session used for this function call.
group (CategoryGroup): group object that contains the contents of the message.
Returns:
str: Formatted string for discord message
"""
ret_string = f"**{group.GroupName}**\n```"
stmt = select(Guild).where(Guild.GuildId == group.GuildId)
@@ -291,19 +298,23 @@ def get_time_str(time_in: float) -> str:
return time.strftime(f"%-M:%S.{milliseconds}", time.gmtime(time_in))
async def send_highscore_message(channel: nextcord.TextChannel, group: CategoryGroup) -> nextcord.Message:
async def send_highscore_message(
session: Session, channel: nextcord.TextChannel, group: CategoryGroup
) -> nextcord.Message:
"""
Function takes care of checking if a message exists for a group.
Creates and sends/edits the message.
Args:
session (Session): The sqlalchemy session being used for this function call.
channel (nextcord.TextChannel): Discord channel object where message is sent
group (CategoryGroup): group to post to the message
Returns:
nextcord.Message: Message object that was posted with group contents
"""
highscore_string = format_highscore_message(group)
highscore_string = format_highscore_message(session, group)
try:
message: nextcord.Message = await channel.fetch_message(group.MessageId)
@@ -332,6 +343,7 @@ async def submit_score(
score (str): Score to submit. Need to convert to correct type.
"""
with Session(engine) as session:
stmt = select(Category.IsTimeRecord).where(Category.CategoryId == category_id)
is_time_record = session.scalar(stmt)
@@ -362,7 +374,7 @@ async def submit_score(
error_response += 'Register with "/register" command.'
assert channel is not None, await interaction.send(error_response, ephemeral=True)
await send_highscore_message(channel, group)
await send_highscore_message(session, channel, group)
def add_preset_configuration(guild_id: int, config_str: str) -> None:
@@ -374,6 +386,8 @@ def add_preset_configuration(guild_id: int, config_str: str) -> None:
guild_id (int): Guild id to add config to
config_str (str): Config name to add
"""
with Session(engine) as session:
with open(f"example_config/{config_str}.json", encoding="utf-8", mode="r") as file:
config = json.load(file)