Add loading config and history from memory
This commit is contained in:
parent
46195bd196
commit
9824e9220b
3 changed files with 57 additions and 15 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,3 @@
|
||||||
__pycache__
|
__pycache__
|
||||||
config.json
|
config.json
|
||||||
|
history.json
|
||||||
|
|
|
@ -3,8 +3,6 @@ import json
|
||||||
import random
|
import random
|
||||||
from typing import Protocol
|
from typing import Protocol
|
||||||
|
|
||||||
CONFIG = "config.json"
|
|
||||||
|
|
||||||
|
|
||||||
def load(file: str) -> dict:
|
def load(file: str) -> dict:
|
||||||
"""Load a json file directly as a dict"""
|
"""Load a json file directly as a dict"""
|
||||||
|
@ -12,9 +10,10 @@ def load(file: str) -> dict:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
|
|
||||||
def load_config() -> dict:
|
def save(file: str, content: dict):
|
||||||
"""Load the current config into a dict"""
|
"""Save out a content dictionary to a file"""
|
||||||
return load(CONFIG)
|
with open(file, "w") as f:
|
||||||
|
json.dump(content, f, indent=4)
|
||||||
|
|
||||||
|
|
||||||
def objects_to_groups(matchees: list[object],
|
def objects_to_groups(matchees: list[object],
|
||||||
|
|
61
matchy.py
61
matchy.py
|
@ -2,16 +2,44 @@
|
||||||
matchy.py - Discord bot that matches people into groups
|
matchy.py - Discord bot that matches people into groups
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import importlib
|
import os.path
|
||||||
|
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
|
||||||
|
|
||||||
# Config contains
|
|
||||||
# TOKEN : str - Discord bot token
|
CONFIG = "config.json"
|
||||||
# OWNERS : list[int] - ids of owners able to use the owner commands
|
config = matching.load(CONFIG)
|
||||||
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)
|
||||||
|
|
||||||
|
# History format:
|
||||||
|
HISTORY = "history.json"
|
||||||
|
history = matching.load(HISTORY) if os.path.isfile(HISTORY) else {
|
||||||
|
"groups": []
|
||||||
|
}
|
||||||
|
Schema(
|
||||||
|
{
|
||||||
|
Optional("groups"): [
|
||||||
|
{
|
||||||
|
"ts": And(Use(str)),
|
||||||
|
"members": [
|
||||||
|
And(Use(int))
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
).validate(history)
|
||||||
|
|
||||||
logger = logging.getLogger("matchy")
|
logger = logging.getLogger("matchy")
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
|
@ -31,13 +59,19 @@ async def on_ready():
|
||||||
await bot.change_presence(status=discord.Status.online, activity=activity)
|
await bot.change_presence(status=discord.Status.online, activity=activity)
|
||||||
|
|
||||||
|
|
||||||
|
def owner_only(ctx: commands.Context) -> bool:
|
||||||
|
"""Checks the author is an owner"""
|
||||||
|
return ctx.message.author.id in config["owners"]
|
||||||
|
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
@commands.dm_only()
|
@commands.dm_only()
|
||||||
@commands.check(lambda ctx: ctx.message.author.id in config["OWNERS"])
|
@commands.check(owner_only)
|
||||||
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)
|
||||||
importlib.reload(config)
|
global 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...")
|
||||||
|
@ -49,7 +83,7 @@ async def sync(ctx: commands.Context):
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
@commands.dm_only()
|
@commands.dm_only()
|
||||||
@commands.check(lambda ctx: ctx.message.author.id in config["OWNERS"])
|
@commands.check(owner_only)
|
||||||
async def close(ctx: commands.Context):
|
async def close(ctx: commands.Context):
|
||||||
"""Handle restart command"""
|
"""Handle restart command"""
|
||||||
await ctx.reply("Closing bot...", ephemeral=True)
|
await ctx.reply("Closing bot...", ephemeral=True)
|
||||||
|
@ -115,7 +149,16 @@ class GroupMessageButton(discord.ui.View):
|
||||||
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)
|
||||||
|
|
||||||
|
ts = time.time()
|
||||||
|
for group in self.groups:
|
||||||
|
history["groups"].append({
|
||||||
|
"ts": ts,
|
||||||
|
"members": list(m.id for m in group)
|
||||||
|
})
|
||||||
|
matching.save(HISTORY, history)
|
||||||
|
|
||||||
|
|
||||||
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
Add a link
Reference in a new issue