Update guild listener to use new database. Add default config function to utils.

This commit is contained in:
alwayscompiling
2022-11-30 21:45:46 -05:00
parent 18df783d86
commit 9531dca892
2 changed files with 37 additions and 11 deletions
+8 -11
View File
@@ -2,10 +2,12 @@
Defines cog for guild listeners. Contains functions for various guild events. Defines cog for guild listeners. Contains functions for various guild events.
""" """
from nextcord.ext import commands
import nextcord import nextcord
from nextcord.ext import commands
from highscores import highscores_data # pylint: disable=import-error # pylint: disable=import-error
from highscores import Guild, session
from utilities.utils import add_preset_configuration
class GuildListener(commands.Cog, name="Guild Listener"): class GuildListener(commands.Cog, name="Guild Listener"):
@@ -17,10 +19,11 @@ class GuildListener(commands.Cog, name="Guild Listener"):
@commands.Cog.listener() @commands.Cog.listener()
async def on_guild_join(self, guild: nextcord.Guild): async def on_guild_join(self, guild: nextcord.Guild):
"""Listener for Guild Join. Creates skeleton for new guild joined and saves it.""" """Listener for Guild Join. Creates skeleton for new guild joined and saves it."""
highscores_data.add_guild(guild.id) new_guild = Guild(GuildId=guild.id)
session.add(new_guild)
session.commit()
# This add config will be removed eventually. Config adding will have to be controlled by commands. # This add config will be removed eventually. Config adding will have to be controlled by commands.
highscores_data.add_configuration(guild.id, "runescape") add_preset_configuration(guild.id, "runescape")
highscores_data.save_guild_data(highscores_data.get_guild(guild.id))
welcome_message: str = "Hello, I keep track of Highscores for you!" welcome_message: str = "Hello, I keep track of Highscores for you!"
welcome_message += " Currently only supporting RuneScape boss highscores," welcome_message += " Currently only supporting RuneScape boss highscores,"
@@ -29,12 +32,6 @@ class GuildListener(commands.Cog, name="Guild Listener"):
await guild.system_channel.send(content=welcome_message) await guild.system_channel.send(content=welcome_message)
@commands.Cog.listener()
async def on_guild_remove(self, guild: nextcord.Guild):
"""Listener for Guild Remove. Removes data from highscores_data."""
print(f"Removing data for guild {guild.name} (id {guild.id}).")
highscores_data.remove_guild(guild.id)
# 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.
+29
View File
@@ -2,6 +2,7 @@
Contains various utlity functions for highscores bot Contains various utlity functions for highscores bot
""" """
import json
import time import time
import nextcord import nextcord
@@ -168,6 +169,7 @@ def format_highscore_message(group: CategoryGroup) -> str:
category_ids.append(cat.CategoryId) category_ids.append(cat.CategoryId)
# get all scores in category group, load into data frame. # get all scores in category group, load into data frame.
# FIXME there is some slice warning here from pandas.
d_f = pd.read_sql_query(sql=session.query(Score).where(Score.CategoryId.in_(category_ids)).statement, con=engine) d_f = pd.read_sql_query(sql=session.query(Score).where(Score.CategoryId.in_(category_ids)).statement, con=engine)
# this list is what will be printed in the discord message # this list is what will be printed in the discord message
@@ -306,3 +308,30 @@ async def submit_score(
assert channel is not None, await interaction.send(error_response, ephemeral=True) assert channel is not None, await interaction.send(error_response, ephemeral=True)
await send_highscore_message(channel, group) await send_highscore_message(channel, group)
def add_preset_configuration(guild_id: int, config_str: str) -> None:
"""
Adds an example configuration to a Guild. Uses guild id to know which guild and
config name to open a file in example_config.
Args:
guild_id (int): Guild id to add config to
config_str (str): Config name to add
"""
with open(f"example_config/{config_str}.json", encoding="utf-8", mode="r") as file:
config = json.load(file)
for group_name, categorys in config["tables"].items():
group_obj = CategoryGroup(GroupName=group_name, GuildId=guild_id)
session.add(group_obj)
for category_name in categorys:
cat_obj = Category(
CategoryName=category_name,
Size=config["table_size"],
IsTimeRecord=config["categories"][category_name]["is_time_record"],
IsAscending=config["categories"][category_name]["ascending"],
)
session.add(cat_obj)
session.commit()