Fix lint smells. Update model usage.
This commit is contained in:
+34
-25
@@ -4,8 +4,10 @@ import nextcord
|
||||
from nextcord.ext import commands
|
||||
from sqlalchemy import distinct, select
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlmodel import col
|
||||
|
||||
from database.db import Category, CategoryGroup, Score, engine
|
||||
from database.db import engine
|
||||
from database.models import Category, CategoryGroup, Score
|
||||
from utilities.utils import (
|
||||
autofill_categories,
|
||||
autofill_groups,
|
||||
@@ -35,10 +37,9 @@ class DeleteCog(commands.Cog, name="Delete Cog"):
|
||||
async def channel_messages(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
channel: nextcord.abc.GuildChannel | None = nextcord.SlashOption(
|
||||
channel: nextcord.TextChannel | None = nextcord.SlashOption(
|
||||
description="Channel to clear of messages.",
|
||||
required=False,
|
||||
channel_types=[nextcord.ChannelType.text],
|
||||
default=None,
|
||||
),
|
||||
) -> None:
|
||||
@@ -53,13 +54,18 @@ class DeleteCog(commands.Cog, name="Delete Cog"):
|
||||
Is a SlashOption. Limited to text channels. Defaults to None.
|
||||
"""
|
||||
if not channel:
|
||||
channel = interaction.channel
|
||||
if isinstance(interaction.channel, nextcord.TextChannel):
|
||||
channel = interaction.channel
|
||||
else:
|
||||
# This should never happen.
|
||||
await interaction.response.send_message("Channel is not a text channel somehow.", ephemeral=True)
|
||||
return
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(CategoryGroup)
|
||||
.where(CategoryGroup.GuildId == interaction.guild_id)
|
||||
.where(CategoryGroup.ChannelId == channel.id)
|
||||
.where(col(CategoryGroup.guild_id) == interaction.guild_id)
|
||||
.where(col(CategoryGroup.channel_id) == channel.id)
|
||||
)
|
||||
# TODO test when nothing in channel
|
||||
groups = session.scalars(stmt)
|
||||
@@ -67,12 +73,13 @@ class DeleteCog(commands.Cog, name="Delete Cog"):
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
|
||||
for group in groups:
|
||||
try:
|
||||
message: nextcord.Message = await channel.fetch_message(group.MessageId)
|
||||
await message.delete()
|
||||
except nextcord.NotFound:
|
||||
# silently ignore that messages are not found. They got deleted already.
|
||||
pass
|
||||
if group.message_id is not None:
|
||||
try:
|
||||
message: nextcord.Message = await channel.fetch_message(group.message_id)
|
||||
await message.delete()
|
||||
except nextcord.NotFound:
|
||||
# silently ignore that messages are not found. They got deleted already.
|
||||
pass
|
||||
|
||||
await interaction.send("Highscore messages deleted.", ephemeral=True)
|
||||
|
||||
@@ -96,24 +103,24 @@ class DeleteCog(commands.Cog, name="Delete Cog"):
|
||||
user_name (str): User to remove from highscores
|
||||
"""
|
||||
with Session(engine) as session:
|
||||
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == user_name)
|
||||
stmt = select(distinct(col(Score.category_id))).where(col(Score.username) == user_name)
|
||||
category_ids: list[int] = list(session.scalars(stmt))
|
||||
|
||||
stmt = select(Score).where(Score.UserName == user_name)
|
||||
stmt = select(Score).where(col(Score.username) == user_name)
|
||||
for score in session.scalars(stmt):
|
||||
session.delete(score)
|
||||
session.commit()
|
||||
|
||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids))
|
||||
group_ids = select(col(Category.group_id)).where(col(Category.id).in_(category_ids))
|
||||
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
||||
stmt = select(CategoryGroup).where(col(CategoryGroup.id).in_(group_ids))
|
||||
|
||||
for group in session.scalars(stmt):
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
|
||||
await interaction.send(f'User "{user_name}" was removed from all Category Groups.', ephemeral=True)
|
||||
|
||||
@delete.subcommand(description="Delete a category from a group. Deletion is perminant.")
|
||||
@delete.subcommand(description="Delete a category from a group. Deletion is permanent.")
|
||||
async def category(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
@@ -132,7 +139,7 @@ class DeleteCog(commands.Cog, name="Delete Cog"):
|
||||
DeleteConfirmView(custom_id=create_delete_id(int(group_id), int(category_id)))
|
||||
)
|
||||
|
||||
@delete.subcommand(description="Delete a group. Deletion is perminant.")
|
||||
@delete.subcommand(description="Delete a group. Deletion is permanent.")
|
||||
async def group(
|
||||
self,
|
||||
interaction: nextcord.Interaction,
|
||||
@@ -171,13 +178,15 @@ class DeleteCog(commands.Cog, name="Delete Cog"):
|
||||
user_name (str): The user to remove. Is SlashOption.
|
||||
"""
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
|
||||
stmt = select(Category.CategoryName).where(Category.CategoryId == int(category_id))
|
||||
stmt = select(col(Category.name)).where(col(Category.id) == int(category_id))
|
||||
category_name = session.scalar(stmt)
|
||||
|
||||
stmt = select(Score).where(Score.CategoryId == int(category_id)).where(Score.UserName == user_name)
|
||||
stmt = (
|
||||
select(Score).where(col(Score.category_id) == int(category_id)).where(col(Score.username) == user_name)
|
||||
)
|
||||
|
||||
# there should only be one because category id and username are a composite primary key in score
|
||||
session.delete(session.scalar(stmt))
|
||||
@@ -204,10 +213,10 @@ class DeleteCog(commands.Cog, name="Delete Cog"):
|
||||
user_name (str): The user to remove. Is SlashOption.
|
||||
"""
|
||||
with Session(engine) as session:
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
|
||||
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == int(group_id))
|
||||
group = session.scalar(stmt)
|
||||
category_ids = select(Category.CategoryId).where(Category.GroupId == int(group_id))
|
||||
stmt = select(Score).where(Score.CategoryId.in_(category_ids)).where(Score.UserName == user_name)
|
||||
category_ids = select(col(Category.id)).where(col(Category.group_id) == int(group_id))
|
||||
stmt = select(Score).where(col(Score.category_id).in_(category_ids)).where(col(Score.username) == user_name)
|
||||
|
||||
for score in session.scalars(stmt):
|
||||
session.delete(score)
|
||||
@@ -215,7 +224,7 @@ class DeleteCog(commands.Cog, name="Delete Cog"):
|
||||
|
||||
await send_highscore_message(session, interaction.guild, group)
|
||||
await interaction.response.send_message(
|
||||
f'User "{user_name}" removed from all categories in {group.GroupName}.', ephemeral=True
|
||||
f'User "{user_name}" removed from all categories in {group.name}.', ephemeral=True
|
||||
)
|
||||
|
||||
@from_category.on_autocomplete("group_id")
|
||||
|
||||
Reference in New Issue
Block a user