From 3cc67a4e9e26722e9117a9404d1d435482977e69 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Fri, 22 Jul 2022 22:05:21 -0400 Subject: [PATCH] Corrected skeleton provider. Add cog for guild listener. --- cogs/guild_listener/__init__.py | 37 +++++++++++++++++++++++++++++++++ utilities/data_storage.py | 11 ++++------ 2 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 cogs/guild_listener/__init__.py diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py new file mode 100644 index 0000000..f9e1822 --- /dev/null +++ b/cogs/guild_listener/__init__.py @@ -0,0 +1,37 @@ +""" +Defines cog for guild listeners. Contains functions for various guild events. +""" + +from nextcord.ext import commands +import nextcord + +from highscores import highscores_data # pylint: disable=import-error +from utilities.data_storage import create_highscore_skeleton # pylint: disable=import-error +from utilities.data_storage import save_highscores_data # pylint: disable=import-error + + +class GuildListener(commands.Cog, name="Guild Listener"): + """Sets up message listener.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.Cog.listener() + async def on_guild_join(self, guild: nextcord.Guild): + """Listener for Guild Join. Creates skeleton for new guild joined and saves it.""" + # turning guild id into string because json doesn't support ints as object keys. + highscores_data[str(guild.id)] = create_highscore_skeleton() + save_highscores_data(highscores_data) + + welcome_message: str = "Hello, I keep track of Highscores for you!" + welcome_message += "Currently only supporting RuneScape boss highscores, but will be more dynamic support in future." + welcome_message += "For assistance on getting started, use \?register help\." + + guild.system_channel.send(content=welcome_message) + + +# 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(GuildListener(bot)) diff --git a/utilities/data_storage.py b/utilities/data_storage.py index 9caec20..3b07f1a 100644 --- a/utilities/data_storage.py +++ b/utilities/data_storage.py @@ -7,11 +7,10 @@ HIGHSCORES_CONFIG_FILE = "highscores_config.json" HIGHSCORES_DATA_FILE = "highscores_data.json" -def create_highscore_data_entry(guild_id: int) -> dict: +def create_highscore_skeleton() -> dict: """ - Creates a skeleton entry for highscores data for given guild + Creates a skeleton entry for new highscores data - @param guild_id: the guild the data skeleton is for @return: dict skeleton entry """ # create the dict @@ -25,9 +24,7 @@ def create_highscore_data_entry(guild_id: int) -> dict: boss_categories[category] = rank_list skeleton["tables"][boss]["categories"] = boss_categories - # return the entire skeleton as a dict under the guild id - # turning guild id into string because json doesn't support ints as object keys. - return {str(guild_id): skeleton} + return skeleton def create_message_map(data: dict): @@ -69,7 +66,7 @@ def open_highscores_data(): return data -def save_highscores_data(data): +def save_highscores_data(data: dict): """ Saves the highscores data dict into a json file. @param data: dict of highscores data.