Some refactoring of the new external files

This commit is contained in:
Marc Di Luzio 2024-08-10 10:55:09 +01:00
parent c44f16dd8f
commit 96fb77f71f
3 changed files with 35 additions and 25 deletions

View file

@ -2,7 +2,7 @@
from schema import Schema, And, Use from schema import Schema, And, Use
import matching import matching
CONFIG = "config.json" FILE = "config.json"
class Config(): class Config():
@ -17,10 +17,14 @@ class Config():
def owners(self) -> list[int]: def owners(self) -> list[int]:
return self.__dict__["owners"] return self.__dict__["owners"]
def reload(self) -> None:
"""Reload the config back into the dict"""
self.__dict__ = load().__dict__
def load() -> Config: def load() -> Config:
"""Load the config and validate it""" """Load the config and validate it"""
config = matching.load(CONFIG) config = matching.load(FILE)
Schema( Schema(
{ {
# Discord bot token # Discord bot token

View file

@ -1,11 +1,19 @@
"""Store matching history""" """Store matching history"""
import os import os
import time
from schema import Schema, And, Use, Optional from schema import Schema, And, Use, Optional
from typing import Protocol
import matching import matching
FILE = "history.json" FILE = "history.json"
class Member(Protocol):
@property
def id(self) -> int:
pass
class History(): class History():
def __init__(self, data: dict): def __init__(self, data: dict):
self.__dict__ = data self.__dict__ = data
@ -15,13 +23,31 @@ class History():
return self.__dict__["groups"] return self.__dict__["groups"]
@property @property
def matchees(self) -> dict: def matchees(self) -> dict[str, dict]:
return self.__dict__["matchees"] return self.__dict__["matchees"]
def save(self) -> None: def save(self) -> None:
"""Save out the history""" """Save out the history"""
matching.save(FILE, self.__dict__) 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: def load() -> History:
"""Load the history and validate it""" """Load the history and validate it"""

View file

@ -2,7 +2,6 @@
matchy.py - Discord bot that matches people into groups matchy.py - Discord bot that matches people into groups
""" """
import logging import logging
import time
import discord import discord
from discord import app_commands from discord import app_commands
from discord.ext import commands from discord.ext import commands
@ -43,8 +42,7 @@ def owner_only(ctx: commands.Context) -> bool:
async def sync(ctx: commands.Context): async def sync(ctx: commands.Context):
"""Handle sync command""" """Handle sync command"""
msg = await ctx.reply("Reloading config...", ephemeral=True) msg = await ctx.reply("Reloading config...", ephemeral=True)
global config Config.reload()
config = matching.load(Config)
logger.info("Reloaded config") logger.info("Reloaded config")
await msg.edit(content="Syncing commands...") await msg.edit(content="Syncing commands...")
@ -121,25 +119,7 @@ class GroupMessageButton(discord.ui.View):
await interaction.channel.send(msg) await interaction.channel.send(msg)
await interaction.channel.send("That's all folks, happy matching and remember - DFTBA!") 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) await interaction.response.edit_message(content="Groups sent to channel!", view=None)
save_groups_to_history(self.groups) History.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()
if __name__ == "__main__": if __name__ == "__main__":