diff --git a/config.py b/config.py index 99487df..2689e9e 100644 --- a/config.py +++ b/config.py @@ -2,7 +2,7 @@ from schema import Schema, And, Use import matching -CONFIG = "config.json" +FILE = "config.json" class Config(): @@ -16,11 +16,15 @@ class Config(): @property def owners(self) -> list[int]: return self.__dict__["owners"] + + def reload(self) -> None: + """Reload the config back into the dict""" + self.__dict__ = load().__dict__ def load() -> Config: """Load the config and validate it""" - config = matching.load(CONFIG) + config = matching.load(FILE) Schema( { # Discord bot token diff --git a/history.py b/history.py index 3144ce3..b697aa6 100644 --- a/history.py +++ b/history.py @@ -1,11 +1,19 @@ """Store matching history""" import os +import time from schema import Schema, And, Use, Optional +from typing import Protocol import matching FILE = "history.json" +class Member(Protocol): + @property + def id(self) -> int: + pass + + class History(): def __init__(self, data: dict): self.__dict__ = data @@ -15,13 +23,31 @@ class History(): return self.__dict__["groups"] @property - def matchees(self) -> dict: + def matchees(self) -> dict[str, dict]: return self.__dict__["matchees"] def save(self) -> None: """Save out the history""" matching.save(FILE, self.__dict__) + def save_groups_to_history(self, groups: list[list[Member]]) -> None: + """Save out the groups to the history file""" + ts = time.time() + for group in groups: + # Add the group + self.groups.append({ + "ts": ts, + "matchees": list(m.id for m in group) + }) + # Add the matches to the matchee data + for m in group: + matchee = self.matchees.get(str(m.id), {"matches": []}) + for o in (o for o in group if o.id != m.id): + matchee["matches"].append({"ts": ts, "id": o.id}) + self.matchees[str(m.id)] = matchee + + self.save() + def load() -> History: """Load the history and validate it""" diff --git a/matchy.py b/matchy.py index 5412b78..1ef7715 100755 --- a/matchy.py +++ b/matchy.py @@ -2,7 +2,6 @@ matchy.py - Discord bot that matches people into groups """ import logging -import time import discord from discord import app_commands from discord.ext import commands @@ -43,8 +42,7 @@ def owner_only(ctx: commands.Context) -> bool: async def sync(ctx: commands.Context): """Handle sync command""" msg = await ctx.reply("Reloading config...", ephemeral=True) - global config - config = matching.load(Config) + Config.reload() logger.info("Reloaded config") await msg.edit(content="Syncing commands...") @@ -121,25 +119,7 @@ class GroupMessageButton(discord.ui.View): await interaction.channel.send(msg) await interaction.channel.send("That's all folks, happy matching and remember - DFTBA!") await interaction.response.edit_message(content="Groups sent to channel!", view=None) - save_groups_to_history(self.groups) - - -def save_groups_to_history(groups: list[list[discord.Member]]) -> None: - ts = time.time() - for group in groups: - # Add the group - History.groups.append({ - "ts": ts, - "matchees": list(m.id for m in group) - }) - # Add the matches to the matchee's daya - for m in group: - matchee = History.matchees.get(str(m.id), {"matches": []}) - for o in (o for o in group if o.id != m.id): - matchee["matches"].append({"ts": ts, "id": o.id}) - History.matchees[m.id] = matchee - - History.save() + History.save_groups_to_history(self.groups) if __name__ == "__main__":