Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cf05f44d0 | ||
|
|
f6ae34c9e6 | ||
|
|
528af14e00 | ||
|
|
dbe652dcce | ||
|
|
1658638938 |
@@ -1,6 +1,4 @@
|
||||
"""
|
||||
Initializing code for all loops.
|
||||
"""
|
||||
"""Initializing code for all loops."""
|
||||
|
||||
from nextcord.ext import commands
|
||||
|
||||
@@ -10,7 +8,7 @@ from .guild_data import GuildData
|
||||
|
||||
# This function will be called when this extension is loaded.
|
||||
# It is necessary to add these functions to the bot.
|
||||
def setup(bot: commands.Bot):
|
||||
"""Adds function to bot"""
|
||||
def setup(bot: commands.Bot) -> None:
|
||||
"""Adds function to bot."""
|
||||
bot.add_cog(BotPresence(bot))
|
||||
bot.add_cog(GuildData(bot))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -11,7 +9,7 @@ from nextcord.ext import commands, tasks
|
||||
class BotPresence(commands.Cog, name="Bot Presence"):
|
||||
"""Class for bot presence functions."""
|
||||
|
||||
def __init__(self, bot: commands.Bot):
|
||||
def __init__(self, bot: commands.Bot) -> None:
|
||||
self.bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
|
||||
+10
-15
@@ -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 sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
# pylint: disable=import-error
|
||||
from database.db import Guild, engine
|
||||
from database.db import engine
|
||||
from database.models import Guild
|
||||
|
||||
|
||||
class GuildData(commands.Cog, name="Guild Data"):
|
||||
@@ -17,30 +15,27 @@ class GuildData(commands.Cog, name="Guild Data"):
|
||||
self.bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
async def on_ready(self) -> None:
|
||||
"""Starts the loop when bot is 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() # pylint: disable=no-member
|
||||
await self.periodic_update_guild_data.start()
|
||||
|
||||
@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."""
|
||||
self.update_guild_data()
|
||||
|
||||
def update_guild_data(self):
|
||||
"""
|
||||
Updates all known guild's data in the database.
|
||||
"""
|
||||
def update_guild_data(self) -> None:
|
||||
"""Updates all known guild's data in the database."""
|
||||
with Session(engine) as session:
|
||||
for guild in self.bot.guilds:
|
||||
# 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)
|
||||
|
||||
# just in case checking if the guild doesn't exist.
|
||||
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)
|
||||
else:
|
||||
guild_data.GuildName = guild.name
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
"""
|
||||
Cog for creating submission process.
|
||||
"""
|
||||
"""Cog for creating submission process."""
|
||||
|
||||
from nextcord.ext import commands
|
||||
|
||||
@@ -15,14 +13,14 @@ class SubmissionCog(commands.Cog, name="Submission Cog"):
|
||||
self.bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
"""Called when cog is loaded"""
|
||||
async def on_ready(self) -> None:
|
||||
"""Called when cog is loaded."""
|
||||
self.bot.add_view(SubmissionCreateButton(self.bot))
|
||||
self.bot.add_view(VerificationView())
|
||||
|
||||
|
||||
# This function will be called when this extension is loaded.
|
||||
# It is necessary to add these functions to the bot.
|
||||
def setup(bot: commands.Bot):
|
||||
"""Adds function to bot"""
|
||||
def setup(bot: commands.Bot) -> None:
|
||||
"""Adds function to bot."""
|
||||
bot.add_cog(SubmissionCog(bot))
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
import nextcord
|
||||
from sqlalchemy import delete, select
|
||||
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
|
||||
from utilities.utils import submit_score
|
||||
@@ -22,8 +24,8 @@ class VerificationView(nextcord.ui.View):
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
delete(Submission)
|
||||
.where(Submission.GuildId == interaction.guild_id)
|
||||
.where(Submission.MessageId == interaction.message.id)
|
||||
.where(col(Submission.guild_id) == interaction.guild_id)
|
||||
.where(col(Submission.message_id) == interaction.message.id)
|
||||
)
|
||||
session.execute(stmt)
|
||||
session.commit()
|
||||
@@ -35,8 +37,8 @@ class VerificationView(nextcord.ui.View):
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(Submission)
|
||||
.where(Submission.GuildId == interaction.guild_id)
|
||||
.where(Submission.MessageId == interaction.message.id)
|
||||
.where(col(Submission.guild_id) == interaction.guild_id)
|
||||
.where(col(Submission.message_id) == interaction.message.id)
|
||||
)
|
||||
|
||||
submission = session.scalar(stmt)
|
||||
@@ -47,11 +49,10 @@ class VerificationView(nextcord.ui.View):
|
||||
return
|
||||
|
||||
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.commit()
|
||||
content = interaction.message.content
|
||||
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)
|
||||
|
||||
+12
-12
@@ -1,9 +1,9 @@
|
||||
import os
|
||||
|
||||
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()
|
||||
|
||||
@@ -12,19 +12,19 @@ SQL_HOST = os.getenv("SQL_HOST", "")
|
||||
SQL_PASS = os.getenv("SQL_PASS", "")
|
||||
SQL_USER = os.getenv("SQL_USER", "")
|
||||
|
||||
# url = "sqlite:///database.db"
|
||||
url = f"mysql+pymysql://{SQL_USER}:{SQL_PASS}@{SQL_HOST}/{SQL_DB}"
|
||||
url = "sqlite:///database.db"
|
||||
# url = f"mysql+pymysql://{SQL_USER}:{SQL_PASS}@{SQL_HOST}/{SQL_DB}"
|
||||
engine = create_engine(url)
|
||||
# SQLModel.metadata.create_all(engine)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
with Session(engine) as session:
|
||||
# guild = Guild(id=1, name="test guild", submission_channel=1, verification_channel=2)
|
||||
# session.add(guild)
|
||||
# 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)
|
||||
# session.add(group1)
|
||||
# session.add(group2)
|
||||
# session.commit()
|
||||
guild = Guild(id=1, name="test guild")
|
||||
session.add(guild)
|
||||
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)
|
||||
session.add(group1)
|
||||
session.add(group2)
|
||||
session.commit()
|
||||
|
||||
# anotherone = Guild(id=2, name="test guild 2", submission_channel=1, verification_channel=2)
|
||||
# session.add(anotherone)
|
||||
|
||||
+7
-3
@@ -22,9 +22,13 @@ class Guild(SQLModel, table=True):
|
||||
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)
|
||||
# deprecated, not currently used.
|
||||
submission_channel: int = Field(sa_column=Column("SubmissionChannel", BIGINT(unsigned=True)))
|
||||
verification_channel: int = Field(sa_column=Column("VerificationChannel", BIGINT(unsigned=True)))
|
||||
active: bool = Field(sa_column=Column("Active", BOOLEAN()), default=True)
|
||||
submission_channel: int | None = Field(
|
||||
sa_column=Column("SubmissionChannel", BIGINT(unsigned=True), nullable=True), default=None
|
||||
)
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user