Pull out the config and history classes
This commit is contained in:
parent
d0865bd780
commit
c44f16dd8f
3 changed files with 96 additions and 55 deletions
33
config.py
33
config.py
|
@ -1,9 +1,34 @@
|
||||||
"""Very simple config loading library"""
|
"""Very simple config loading library"""
|
||||||
import json
|
from schema import Schema, And, Use
|
||||||
|
import matching
|
||||||
|
|
||||||
CONFIG = "config.json"
|
CONFIG = "config.json"
|
||||||
|
|
||||||
|
|
||||||
def load() -> dict:
|
class Config():
|
||||||
with open(CONFIG) as f:
|
def __init__(self, data: dict):
|
||||||
return json.load(f)
|
self.__dict__ = data
|
||||||
|
|
||||||
|
@property
|
||||||
|
def token(self) -> str:
|
||||||
|
return self.__dict__["token"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def owners(self) -> list[int]:
|
||||||
|
return self.__dict__["owners"]
|
||||||
|
|
||||||
|
|
||||||
|
def load() -> Config:
|
||||||
|
"""Load the config and validate it"""
|
||||||
|
config = matching.load(CONFIG)
|
||||||
|
Schema(
|
||||||
|
{
|
||||||
|
# Discord bot token
|
||||||
|
"token": And(Use(str)),
|
||||||
|
|
||||||
|
# ids of owners authorised to use owner-only commands
|
||||||
|
"owners": And(Use(list[int])),
|
||||||
|
}
|
||||||
|
).validate(config)
|
||||||
|
|
||||||
|
return Config(config)
|
||||||
|
|
56
history.py
Normal file
56
history.py
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
"""Store matching history"""
|
||||||
|
import os
|
||||||
|
from schema import Schema, And, Use, Optional
|
||||||
|
import matching
|
||||||
|
|
||||||
|
FILE = "history.json"
|
||||||
|
|
||||||
|
|
||||||
|
class History():
|
||||||
|
def __init__(self, data: dict):
|
||||||
|
self.__dict__ = data
|
||||||
|
|
||||||
|
@property
|
||||||
|
def groups(self) -> list[dict]:
|
||||||
|
return self.__dict__["groups"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def matchees(self) -> dict:
|
||||||
|
return self.__dict__["matchees"]
|
||||||
|
|
||||||
|
def save(self) -> None:
|
||||||
|
"""Save out the history"""
|
||||||
|
matching.save(FILE, self.__dict__)
|
||||||
|
|
||||||
|
|
||||||
|
def load() -> History:
|
||||||
|
"""Load the history and validate it"""
|
||||||
|
history = matching.load(FILE) if os.path.isfile(FILE) else {
|
||||||
|
"groups": [],
|
||||||
|
"matchees": {}
|
||||||
|
}
|
||||||
|
Schema(
|
||||||
|
{
|
||||||
|
Optional("groups"): [
|
||||||
|
{
|
||||||
|
"ts": And(Use(str)),
|
||||||
|
"matchees": [
|
||||||
|
And(Use(int))
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Optional("matchees"): {
|
||||||
|
Optional(str): {
|
||||||
|
"matches": [
|
||||||
|
{
|
||||||
|
"ts": And(Use(str)),
|
||||||
|
"id": And(Use(int)),
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
).validate(history)
|
||||||
|
|
||||||
|
return History(history)
|
62
matchy.py
62
matchy.py
|
@ -2,56 +2,17 @@
|
||||||
matchy.py - Discord bot that matches people into groups
|
matchy.py - Discord bot that matches people into groups
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
|
||||||
import time
|
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
|
||||||
from schema import Schema, And, Use, Optional
|
|
||||||
import matching
|
import matching
|
||||||
|
import history
|
||||||
|
import config
|
||||||
|
|
||||||
|
|
||||||
CONFIG = "config.json"
|
Config = config.load()
|
||||||
config = matching.load(CONFIG)
|
History = history.load()
|
||||||
Schema(
|
|
||||||
{
|
|
||||||
# Discord bot token
|
|
||||||
"token": And(Use(str)),
|
|
||||||
|
|
||||||
# ids of owners authorised to use owner-only commands
|
|
||||||
"owners": And(Use(list[int])),
|
|
||||||
}
|
|
||||||
).validate(config)
|
|
||||||
|
|
||||||
# History format:
|
|
||||||
HISTORY = "history.json"
|
|
||||||
history = matching.load(HISTORY) if os.path.isfile(HISTORY) else {
|
|
||||||
"groups": [],
|
|
||||||
"matchees": {}
|
|
||||||
}
|
|
||||||
Schema(
|
|
||||||
{
|
|
||||||
Optional("groups"): [
|
|
||||||
{
|
|
||||||
"ts": And(Use(str)),
|
|
||||||
"matchees": [
|
|
||||||
And(Use(int))
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
Optional("matchees"): {
|
|
||||||
Optional(str): {
|
|
||||||
"matches": [
|
|
||||||
{
|
|
||||||
"ts": And(Use(str)),
|
|
||||||
"id": And(Use(int)),
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
).validate(history)
|
|
||||||
|
|
||||||
logger = logging.getLogger("matchy")
|
logger = logging.getLogger("matchy")
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
|
@ -73,7 +34,7 @@ async def on_ready():
|
||||||
|
|
||||||
def owner_only(ctx: commands.Context) -> bool:
|
def owner_only(ctx: commands.Context) -> bool:
|
||||||
"""Checks the author is an owner"""
|
"""Checks the author is an owner"""
|
||||||
return ctx.message.author.id in config["owners"]
|
return ctx.message.author.id in Config.owners
|
||||||
|
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
|
@ -83,7 +44,7 @@ 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
|
global config
|
||||||
config = matching.load(CONFIG)
|
config = matching.load(Config)
|
||||||
logger.info("Reloaded config")
|
logger.info("Reloaded config")
|
||||||
|
|
||||||
await msg.edit(content="Syncing commands...")
|
await msg.edit(content="Syncing commands...")
|
||||||
|
@ -167,21 +128,20 @@ def save_groups_to_history(groups: list[list[discord.Member]]) -> None:
|
||||||
ts = time.time()
|
ts = time.time()
|
||||||
for group in groups:
|
for group in groups:
|
||||||
# Add the group
|
# Add the group
|
||||||
history["groups"].append({
|
History.groups.append({
|
||||||
"ts": ts,
|
"ts": ts,
|
||||||
"matchees": list(m.id for m in group)
|
"matchees": list(m.id for m in group)
|
||||||
})
|
})
|
||||||
# Add the matches to the matchee's daya
|
# Add the matches to the matchee's daya
|
||||||
for m in group:
|
for m in group:
|
||||||
matchee = history["matchees"].get(str(m.id), {"matches": []})
|
matchee = History.matchees.get(str(m.id), {"matches": []})
|
||||||
for o in (o for o in group if o.id != m.id):
|
for o in (o for o in group if o.id != m.id):
|
||||||
matchee["matches"].append({"ts": ts, "id": o.id})
|
matchee["matches"].append({"ts": ts, "id": o.id})
|
||||||
history["matchees"][m.id] = matchee
|
History.matchees[m.id] = matchee
|
||||||
|
|
||||||
matching.save(HISTORY, history)
|
History.save()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
handler = logging.StreamHandler()
|
handler = logging.StreamHandler()
|
||||||
bot.run(config["token"], log_handler=handler, root_logger=True)
|
bot.run(Config.token, log_handler=handler, root_logger=True)
|
||||||
matching.save(HISTORY, history)
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue