diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py index c428826..25e5197 100644 --- a/cogs/guild_listener/__init__.py +++ b/cogs/guild_listener/__init__.py @@ -8,7 +8,6 @@ from sqlalchemy.orm import Session # pylint: disable=import-error from utilities.sql_data_classes import Guild, engine -from utilities.utils import add_preset_configuration class GuildListener(commands.Cog, name="Guild Listener"): @@ -21,7 +20,7 @@ class GuildListener(commands.Cog, name="Guild Listener"): async def on_guild_join(self, guild: nextcord.Guild): """Listener for Guild Join. Adds guild to database.""" - new_guild = Guild(GuildId=guild.id) + new_guild = Guild(GuildId=guild.id, GuildName=guild.name) with Session(engine) as session: session.add(new_guild) diff --git a/cogs/loops/__init__.py b/cogs/loops/__init__.py new file mode 100644 index 0000000..9ee014f --- /dev/null +++ b/cogs/loops/__init__.py @@ -0,0 +1,16 @@ +""" +Initializing code for all loops. +""" + +from nextcord.ext import commands + +from .bot_presence import BotPresence +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""" + bot.add_cog(BotPresence(bot)) + bot.add_cog(GuildData(bot)) diff --git a/cogs/presence/__init__.py b/cogs/loops/bot_presence.py similarity index 85% rename from cogs/presence/__init__.py rename to cogs/loops/bot_presence.py index 6646e08..d752396 100644 --- a/cogs/presence/__init__.py +++ b/cogs/loops/bot_presence.py @@ -1,5 +1,5 @@ """ -Functions for controlling the bot's presence. +Functions for controlling the bot's presence. Also contains the loop. """ import random @@ -46,10 +46,3 @@ class BotPresence(commands.Cog, name="Bot Presence"): status += f" | {len(self.bot.guilds)} servers" await self.bot.change_presence(activity=nextcord.Game(status)) - - -# 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""" - bot.add_cog(BotPresence(bot)) diff --git a/cogs/loops/guild_data.py b/cogs/loops/guild_data.py new file mode 100644 index 0000000..98fd77b --- /dev/null +++ b/cogs/loops/guild_data.py @@ -0,0 +1,48 @@ +""" +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 utilities.sql_data_classes import Guild, engine + + +class GuildData(commands.Cog, name="Guild Data"): + """Class to contain loop.""" + + def __init__(self, bot: commands.bot) -> None: + self.bot = bot + + @commands.Cog.listener() + async def on_ready(self): + """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 + + @tasks.loop(hours=24) + async def periodic_update_guild_data(self): + """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. + """ + 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) + 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) + session.add(new_guild) + else: + guild_data.GuildName = guild.name + + session.commit() diff --git a/highscores_database.sql b/highscores_database.sql index ae0a0f6..992f43f 100644 --- a/highscores_database.sql +++ b/highscores_database.sql @@ -3,6 +3,7 @@ use highscores; CREATE TABLE Guilds( GuildId bigint unsigned not null unique, + GuildName varchar(95) not null, SubmissionChannel bigint unsigned, VerificationChannel bigint unsigned, PRIMARY KEY (GuildId)