Corrected skeleton provider. Add cog for guild listener.

This commit is contained in:
alwayscompiling
2022-07-22 22:05:21 -04:00
parent df80eb2952
commit 3cc67a4e9e
2 changed files with 41 additions and 7 deletions
+37
View File
@@ -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))
+4 -7
View File
@@ -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.