Add match data to the history
This commit is contained in:
parent
9824e9220b
commit
d0865bd780
1 changed files with 34 additions and 11 deletions
33
matchy.py
33
matchy.py
|
@ -26,19 +26,31 @@ Schema(
|
|||
# History format:
|
||||
HISTORY = "history.json"
|
||||
history = matching.load(HISTORY) if os.path.isfile(HISTORY) else {
|
||||
"groups": []
|
||||
"groups": [],
|
||||
"matchees": {}
|
||||
}
|
||||
Schema(
|
||||
{
|
||||
Optional("groups"): [
|
||||
{
|
||||
"ts": And(Use(str)),
|
||||
"members": [
|
||||
"matchees": [
|
||||
And(Use(int))
|
||||
]
|
||||
}
|
||||
],
|
||||
Optional("matchees"): {
|
||||
Optional(str): {
|
||||
"matches": [
|
||||
{
|
||||
"ts": And(Use(str)),
|
||||
"id": And(Use(int)),
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
).validate(history)
|
||||
|
||||
logger = logging.getLogger("matchy")
|
||||
|
@ -142,19 +154,30 @@ class GroupMessageButton(discord.ui.View):
|
|||
super().__init__(timeout=timeout)
|
||||
|
||||
@discord.ui.button(label="Send groups to channel", style=discord.ButtonStyle.green, emoji="📮")
|
||||
async def send_to_channel(self, interaction: discord.Interaction, _button: discord.ui.Button):
|
||||
async def send_to_channel(self, interaction: discord.Interaction, _button: discord.ui.Button) -> None:
|
||||
"""Send the groups to the channel with the button is pressed"""
|
||||
for msg in (matching.group_to_message(g) for g in self.groups):
|
||||
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 self.groups:
|
||||
for group in groups:
|
||||
# Add the group
|
||||
history["groups"].append({
|
||||
"ts": ts,
|
||||
"members": list(m.id for m in group)
|
||||
"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
|
||||
|
||||
matching.save(HISTORY, history)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue