Compare commits
24
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00265f2f58 | ||
|
|
93d558edbc | ||
|
|
0f2b1aa2e6 | ||
|
|
3f6bac99ab | ||
|
|
46e2955642 | ||
|
|
ac9bf6b35f | ||
|
|
869b2ccb82 | ||
|
|
3ddfdfe9b0 | ||
|
|
3860e74579 | ||
|
|
3b49a6ec2a | ||
|
|
2f89588358 | ||
|
|
0513df944f | ||
|
|
ddf319b9d5 | ||
|
|
13f5bc0aab | ||
|
|
e6f3fcb604 | ||
|
|
eaa342d479 | ||
|
|
ac11f3e462 | ||
|
|
106b394c44 | ||
|
|
6c1505685b | ||
|
|
2e681be690 | ||
|
|
5448f2a426 | ||
|
|
7647c355af | ||
|
|
a95fca40f9 | ||
|
|
148ba253cf |
@@ -8,6 +8,7 @@ from sqlalchemy.orm import Session
|
|||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from utilities.sql_data_classes import Guild, engine
|
from utilities.sql_data_classes import Guild, engine
|
||||||
|
from utilities.utils import add_preset_configuration
|
||||||
|
|
||||||
|
|
||||||
class GuildListener(commands.Cog, name="Guild Listener"):
|
class GuildListener(commands.Cog, name="Guild Listener"):
|
||||||
@@ -20,7 +21,7 @@ class GuildListener(commands.Cog, name="Guild Listener"):
|
|||||||
async def on_guild_join(self, guild: nextcord.Guild):
|
async def on_guild_join(self, guild: nextcord.Guild):
|
||||||
"""Listener for Guild Join. Adds guild to database."""
|
"""Listener for Guild Join. Adds guild to database."""
|
||||||
|
|
||||||
new_guild = Guild(GuildId=guild.id, GuildName=guild.name)
|
new_guild = Guild(GuildId=guild.id)
|
||||||
|
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.add(new_guild)
|
session.add(new_guild)
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
"""
|
|
||||||
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))
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
"""
|
|
||||||
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()
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Functions for controlling the bot's presence. Also contains the loop.
|
Functions for controlling the bot's presence.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
@@ -46,3 +46,10 @@ class BotPresence(commands.Cog, name="Bot Presence"):
|
|||||||
status += f" | {len(self.bot.guilds)} servers"
|
status += f" | {len(self.bot.guilds)} servers"
|
||||||
|
|
||||||
await self.bot.change_presence(activity=nextcord.Game(status))
|
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))
|
||||||
@@ -3,7 +3,6 @@ use highscores;
|
|||||||
|
|
||||||
CREATE TABLE Guilds(
|
CREATE TABLE Guilds(
|
||||||
GuildId bigint unsigned not null unique,
|
GuildId bigint unsigned not null unique,
|
||||||
GuildName varchar(95) not null,
|
|
||||||
SubmissionChannel bigint unsigned,
|
SubmissionChannel bigint unsigned,
|
||||||
VerificationChannel bigint unsigned,
|
VerificationChannel bigint unsigned,
|
||||||
PRIMARY KEY (GuildId)
|
PRIMARY KEY (GuildId)
|
||||||
|
|||||||
Reference in New Issue
Block a user