Add thread creation to the match

This commit is contained in:
Marc Di Luzio 2024-08-12 19:14:54 +01:00
parent 142de9b3ca
commit d3e1d01c9b
3 changed files with 42 additions and 20 deletions

View file

@ -197,13 +197,3 @@ def members_to_groups(matchees: list[Member],
# Simply assert false, this should never happen # Simply assert false, this should never happen
# And should be caught by tests # And should be caught by tests
assert False assert False
def group_to_message(group: list[Member]) -> str:
"""Get the message to send for each group"""
mentions = [m.mention for m in group]
if len(group) > 1:
mentions = f"{', '.join(mentions[:-1])} and {mentions[-1]}"
else:
mentions = mentions[0]
return f"Matched up {mentions}!"

View file

@ -9,6 +9,7 @@ import matching
import state import state
import config import config
import re import re
import util
STATE_FILE = "state.json" STATE_FILE = "state.json"
@ -138,7 +139,7 @@ async def match(interaction: discord.Interaction, members_min: int = None):
return return
# Post about all the groups with a button to send to the channel # Post about all the groups with a button to send to the channel
groups_list = '\n'.join(matching.group_to_message(g) for g in groups) groups_list = '\n'.join(", ".join([m.mention for m in g]) for g in groups)
msg = f"Roger! I've generated example groups for ya:\n\n{groups_list}" msg = f"Roger! I've generated example groups for ya:\n\n{groups_list}"
view = discord.utils.MISSING view = discord.utils.MISSING
@ -176,28 +177,37 @@ class DynamicGroupButton(discord.ui.DynamicItem[discord.ui.Button],
# This is called when the button is clicked and the custom_id matches the template. # This is called when the button is clicked and the custom_id matches the template.
@classmethod @classmethod
async def from_custom_id(cls, interaction: discord.Interaction, item: discord.ui.Button, match: re.Match[str], /): async def from_custom_id(cls, intrctn: discord.Interaction, item: discord.ui.Button, match: re.Match[str], /):
min = int(match['min']) min = int(match['min'])
return cls(min) return cls(min)
async def callback(self, interaction: discord.Interaction) -> None: async def callback(self, intrctn: discord.Interaction) -> None:
"""Match up people when the button is pressed""" """Match up people when the button is pressed"""
logger.info("Handling button press min=%s", self.min) logger.info("Handling button press min=%s", self.min)
logger.info("User %s from %s in #%s", interaction.user, logger.info("User %s from %s in #%s", intrctn.user,
interaction.guild.name, interaction.channel.name) intrctn.guild.name, intrctn.channel.name)
# Let the user know we've recieved the message # Let the user know we've recieved the message
await interaction.response.send_message(content="Matchy is matching matchees...", ephemeral=True) await intrctn.response.send_message(content="Matchy is matching matchees...", ephemeral=True)
groups = active_members_to_groups(interaction.channel, self.min) groups = active_members_to_groups(intrctn.channel, self.min)
# Send the groups # Send the groups
for msg in (matching.group_to_message(g) for g in groups): for idx, group in enumerate(groups):
await interaction.channel.send(msg)
message = await intrctn.channel.send(
f"Matched up {util.format_list([m.mention for m in group])}!")
# Set up a thread for this match if the bot has permissions to do so
if intrctn.channel.permissions_for(intrctn.guild.me).create_public_threads:
await intrctn.channel.create_thread(
name=f"{util.format_today()} Group {chr(65 + idx)}",
message=message,
reason="Creating a matching thread")
# Close off with a message # Close off with a message
await interaction.channel.send("That's all folks, happy matching and remember - DFTBA!") await intrctn.channel.send("That's all folks, happy matching and remember - DFTBA!")
# Save the groups to the history # Save the groups to the history
State.log_groups(groups) State.log_groups(groups)

22
py/util.py Normal file
View file

@ -0,0 +1,22 @@
from datetime import datetime
def get_day_with_suffix(day):
"""Get the suffix for a day of the month"""
if 11 <= day <= 13:
return str(day) + 'th'
else:
return str(day) + {1: 'st', 2: 'nd', 3: 'rd'}.get(day % 10, 'th')
def format_today():
"""Format the current datetime"""
now = datetime.now()
return f"{get_day_with_suffix(now.day)} {now.strftime("%B")}"
def format_list(list) -> str:
"""Format a list into a human readable format of foo, bar and bob"""
if len(list) > 1:
return f"{', '.join(list[:-1])} and {list[-1]}"
return list[0] if list else ''