Change session usage to on use basis.

This commit is contained in:
alwayscompiling
2022-12-06 22:28:53 -05:00
parent 296e14f0f6
commit 3446ce9ca2
11 changed files with 357 additions and 306 deletions
+42 -37
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,23 +56,24 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
if not channel:
channel = interaction.channel
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.ChannelId == channel.id)
)
# TODO test when nothing in channel
groups = session.scalars(stmt)
with Session(engine) as session:
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.ChannelId == channel.id)
)
# TODO test when nothing in channel
groups = session.scalars(stmt)
await interaction.response.defer(ephemeral=True)
await interaction.response.defer(ephemeral=True)
for group in groups:
try:
table_message: nextcord.Message = await channel.fetch_message(group.MessageId)
await table_message.delete()
except nextcord.NotFound:
# silently ignore that messages are not found. They got deleted already.
pass
for group in groups:
try:
table_message: nextcord.Message = await channel.fetch_message(group.MessageId)
await table_message.delete()
except nextcord.NotFound:
# silently ignore that messages are not found. They got deleted already.
pass
await interaction.send("Table messages deleted.", ephemeral=True)
@@ -90,21 +92,23 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
Args:
interaction (nextcord.Interaction): Interaction that invoked the command.
"""
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == interaction.guild_id)
# awaiting response because posting scores takes a while
await interaction.response.defer(ephemeral=True)
with Session(engine) as session:
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == interaction.guild_id)
for group in session.scalars(stmt):
channel = interaction.guild.get_channel(group.ChannelId)
# awaiting response because posting scores takes a while
await interaction.response.defer(ephemeral=True)
# Error checking for registered highscore channel
error_response = "Registered Highscores channel does not exist or was never registered. "
error_response += 'Use "/register" to register channels.'
assert channel is not None, await interaction.response(error_response, ephemeral=True)
for group in session.scalars(stmt):
channel = interaction.guild.get_channel(group.ChannelId)
message: nextcord.Message = await send_highscore_message(channel, group)
await message.edit(view=SubmissionCreateButton(self.bot))
# Error checking for registered highscore channel
error_response = "Registered Highscores channel does not exist or was never registered. "
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(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,19 +129,20 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
group_id (str): Str representation of Group id to refresh. Is SlashOption.
"""
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.GroupId == int(group_id))
)
group = session.scalar(stmt)
with Session(engine) as session:
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.GroupId == int(group_id))
)
group = session.scalar(stmt)
channel = interaction.guild.get_channel(group.ChannelId)
channel = interaction.guild.get_channel(group.ChannelId)
message: nextcord.Message = await send_highscore_message(channel, group)
await message.edit(view=SubmissionCreateButton(self.bot))
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)
await interaction.response.send_message(f"Updated table {group.GroupName}!", ephemeral=True)
@refresh_group.on_autocomplete("group_id")
async def refresh_table_autofill(self, interaction: nextcord.Interaction, group_id: str):