Compare commits

..
5 Commits
Author SHA1 Message Date
imcompiling 9cf05f44d0 Fix error in model.
The two channel ids need to be NULL by default, don't know them on object creation.
2025-03-03 23:32:03 -05:00
imcompiling f6ae34c9e6 Started fixing smells and updating model calls. 2025-03-03 23:29:46 -05:00
imcompiling 528af14e00 Update model usage in verification view. 2025-03-03 23:14:31 -05:00
imcompiling dbe652dcce Fix smells in submission init file. 2025-03-03 23:08:53 -05:00
imcompiling 1658638938 Remove unneeded parenthesis. 2025-03-03 23:08:41 -05:00
7 changed files with 47 additions and 53 deletions
+3 -5
View File
@@ -1,6 +1,4 @@
""" """Initializing code for all loops."""
Initializing code for all loops.
"""
from nextcord.ext import commands from nextcord.ext import commands
@@ -10,7 +8,7 @@ from .guild_data import GuildData
# This function will be called when this extension is loaded. # This function will be called when this extension is loaded.
# It is necessary to add these functions to the bot. # It is necessary to add these functions to the bot.
def setup(bot: commands.Bot): def setup(bot: commands.Bot) -> None:
"""Adds function to bot""" """Adds function to bot."""
bot.add_cog(BotPresence(bot)) bot.add_cog(BotPresence(bot))
bot.add_cog(GuildData(bot)) bot.add_cog(GuildData(bot))
+2 -4
View File
@@ -1,6 +1,4 @@
""" """Functions for controlling the bot's presence. Also contains the loop."""
Functions for controlling the bot's presence. Also contains the loop.
"""
import random import random
@@ -11,7 +9,7 @@ from nextcord.ext import commands, tasks
class BotPresence(commands.Cog, name="Bot Presence"): class BotPresence(commands.Cog, name="Bot Presence"):
"""Class for bot presence functions.""" """Class for bot presence functions."""
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot) -> None:
self.bot = bot self.bot = bot
@commands.Cog.listener() @commands.Cog.listener()
+10 -15
View File
@@ -1,13 +1,11 @@
""" """Contains a loop that updates guild data in the database periodically."""
Contains a loop that updates guild data in the database periodically.
"""
from nextcord.ext import commands, tasks from nextcord.ext import commands, tasks
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
# pylint: disable=import-error from database.db import engine
from database.db import Guild, engine from database.models import Guild
class GuildData(commands.Cog, name="Guild Data"): class GuildData(commands.Cog, name="Guild Data"):
@@ -17,30 +15,27 @@ class GuildData(commands.Cog, name="Guild Data"):
self.bot = bot self.bot = bot
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self) -> None:
"""Starts the loop when bot is ready.""" """Starts the loop when bot is ready."""
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
# pylint doesn't recognize that periodic_update_guild_data is subclassed from tasks.loop. await self.periodic_update_guild_data.start()
await self.periodic_update_guild_data.start() # pylint: disable=no-member
@tasks.loop(hours=24) @tasks.loop(hours=24)
async def periodic_update_guild_data(self): async def periodic_update_guild_data(self) -> None:
"""Loops every day, calls update_guild_data.""" """Loops every day, calls update_guild_data."""
self.update_guild_data() self.update_guild_data()
def update_guild_data(self): def update_guild_data(self) -> None:
""" """Updates all known guild's data in the database."""
Updates all known guild's data in the database.
"""
with Session(engine) as session: with Session(engine) as session:
for guild in self.bot.guilds: for guild in self.bot.guilds:
# update given guild name for guild id # update given guild name for guild id
stmt = select(Guild).where(Guild.GuildId == guild.id) stmt = select(Guild).where(Guild.id == guild.id)
guild_data = session.scalar(stmt) guild_data = session.scalar(stmt)
# just in case checking if the guild doesn't exist. # just in case checking if the guild doesn't exist.
if guild_data is None: if guild_data is None:
new_guild = Guild(GuildId=guild.id, GuildName=guild.name) new_guild = Guild(id=guild.id, name=guild.name)
session.add(new_guild) session.add(new_guild)
else: else:
guild_data.GuildName = guild.name guild_data.GuildName = guild.name
+5 -7
View File
@@ -1,6 +1,4 @@
""" """Cog for creating submission process."""
Cog for creating submission process.
"""
from nextcord.ext import commands from nextcord.ext import commands
@@ -15,14 +13,14 @@ class SubmissionCog(commands.Cog, name="Submission Cog"):
self.bot = bot self.bot = bot
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self) -> None:
"""Called when cog is loaded""" """Called when cog is loaded."""
self.bot.add_view(SubmissionCreateButton(self.bot)) self.bot.add_view(SubmissionCreateButton(self.bot))
self.bot.add_view(VerificationView()) self.bot.add_view(VerificationView())
# This function will be called when this extension is loaded. # This function will be called when this extension is loaded.
# It is necessary to add these functions to the bot. # It is necessary to add these functions to the bot.
def setup(bot: commands.Bot): def setup(bot: commands.Bot) -> None:
"""Adds function to bot""" """Adds function to bot."""
bot.add_cog(SubmissionCog(bot)) bot.add_cog(SubmissionCog(bot))
+8 -7
View File
@@ -3,8 +3,10 @@
import nextcord import nextcord
from sqlalchemy import delete, select from sqlalchemy import delete, select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlmodel import col
from database.db import Submission, engine from database.db import engine
from database.models import Submission
# pylint: disable=import-error # pylint: disable=import-error
from utilities.utils import submit_score from utilities.utils import submit_score
@@ -22,8 +24,8 @@ class VerificationView(nextcord.ui.View):
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
delete(Submission) delete(Submission)
.where(Submission.GuildId == interaction.guild_id) .where(col(Submission.guild_id) == interaction.guild_id)
.where(Submission.MessageId == interaction.message.id) .where(col(Submission.message_id) == interaction.message.id)
) )
session.execute(stmt) session.execute(stmt)
session.commit() session.commit()
@@ -35,8 +37,8 @@ class VerificationView(nextcord.ui.View):
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(Submission) select(Submission)
.where(Submission.GuildId == interaction.guild_id) .where(col(Submission.guild_id) == interaction.guild_id)
.where(Submission.MessageId == interaction.message.id) .where(col(Submission.message_id) == interaction.message.id)
) )
submission = session.scalar(stmt) submission = session.scalar(stmt)
@@ -47,11 +49,10 @@ class VerificationView(nextcord.ui.View):
return return
await submit_score( await submit_score(
interaction, submission.GroupId, submission.CategoryId, submission.UserName, submission.Score interaction, submission.group_id, submission.category_id, submission.username, submission.score
) )
session.delete(submission) session.delete(submission)
session.commit() session.commit()
content = interaction.message.content content = interaction.message.content
content += f"\nApproved by <@{interaction.user.id}>." content += f"\nApproved by <@{interaction.user.id}>."
# remove the bot information embed. it should always be the first one.
await interaction.message.edit(content=content, view=None) await interaction.message.edit(content=content, view=None)
+12 -12
View File
@@ -1,9 +1,9 @@
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
from sqlmodel import Session, create_engine, select from sqlmodel import Session, SQLModel, create_engine, select
from models import Guild from models import CategoryGroup, Guild
load_dotenv() load_dotenv()
@@ -12,19 +12,19 @@ SQL_HOST = os.getenv("SQL_HOST", "")
SQL_PASS = os.getenv("SQL_PASS", "") SQL_PASS = os.getenv("SQL_PASS", "")
SQL_USER = os.getenv("SQL_USER", "") SQL_USER = os.getenv("SQL_USER", "")
# url = "sqlite:///database.db" url = "sqlite:///database.db"
url = f"mysql+pymysql://{SQL_USER}:{SQL_PASS}@{SQL_HOST}/{SQL_DB}" # url = f"mysql+pymysql://{SQL_USER}:{SQL_PASS}@{SQL_HOST}/{SQL_DB}"
engine = create_engine(url) engine = create_engine(url)
# SQLModel.metadata.create_all(engine) SQLModel.metadata.create_all(engine)
with Session(engine) as session: with Session(engine) as session:
# guild = Guild(id=1, name="test guild", submission_channel=1, verification_channel=2) guild = Guild(id=1, name="test guild")
# session.add(guild) session.add(guild)
# group1 = CategoryGroup(name="group1", channel_id=1, guild_id=1, message_id=1) group1 = CategoryGroup(name="group1", channel_id=1, guild_id=1, message_id=1)
# group2 = CategoryGroup(name="group2", channel_id=2, guild_id=1, message_id=2) group2 = CategoryGroup(name="group2", channel_id=2, guild_id=1, message_id=2)
# session.add(group1) session.add(group1)
# session.add(group2) session.add(group2)
# session.commit() session.commit()
# anotherone = Guild(id=2, name="test guild 2", submission_channel=1, verification_channel=2) # anotherone = Guild(id=2, name="test guild 2", submission_channel=1, verification_channel=2)
# session.add(anotherone) # session.add(anotherone)
+7 -3
View File
@@ -22,9 +22,13 @@ class Guild(SQLModel, table=True):
id: int = Field(sa_column=Column("GuildId", BIGINT(unsigned=True), primary_key=True, nullable=False)) id: int = Field(sa_column=Column("GuildId", BIGINT(unsigned=True), primary_key=True, nullable=False))
name: str = Field(sa_column=Column("GuildName", VARCHAR), max_length=95) name: str = Field(sa_column=Column("GuildName", VARCHAR), max_length=95)
# deprecated, not currently used. # deprecated, not currently used.
submission_channel: int = Field(sa_column=Column("SubmissionChannel", BIGINT(unsigned=True))) submission_channel: int | None = Field(
verification_channel: int = Field(sa_column=Column("VerificationChannel", BIGINT(unsigned=True))) sa_column=Column("SubmissionChannel", BIGINT(unsigned=True), nullable=True), default=None
active: bool = Field(sa_column=Column("Active", BOOLEAN()), default=True) )
verification_channel: int | None = Field(
sa_column=Column("VerificationChannel", BIGINT(unsigned=True), nullable=True), default=None
)
active: bool = Field(sa_column=Column("Active", BOOLEAN), default=True)
category_groups: list["CategoryGroup"] = Relationship(back_populates="guild", cascade_delete=True) category_groups: list["CategoryGroup"] = Relationship(back_populates="guild", cascade_delete=True)