From d3e1d01c9bb44129a147037e9b1e3e7a3e9c4da8 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Mon, 12 Aug 2024 19:14:54 +0100 Subject: [PATCH] Add thread creation to the match --- py/matching.py | 10 ---------- py/matchy.py | 30 ++++++++++++++++++++---------- py/util.py | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 py/util.py diff --git a/py/matching.py b/py/matching.py index 34bbe43..d001c2a 100644 --- a/py/matching.py +++ b/py/matching.py @@ -197,13 +197,3 @@ def members_to_groups(matchees: list[Member], # Simply assert false, this should never happen # And should be caught by tests 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}!" diff --git a/py/matchy.py b/py/matchy.py index fe5fd9c..28c73d3 100755 --- a/py/matchy.py +++ b/py/matchy.py @@ -9,6 +9,7 @@ import matching import state import config import re +import util STATE_FILE = "state.json" @@ -138,7 +139,7 @@ async def match(interaction: discord.Interaction, members_min: int = None): return # 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}" 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. @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']) 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""" logger.info("Handling button press min=%s", self.min) - logger.info("User %s from %s in #%s", interaction.user, - interaction.guild.name, interaction.channel.name) + logger.info("User %s from %s in #%s", intrctn.user, + intrctn.guild.name, intrctn.channel.name) # 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 - for msg in (matching.group_to_message(g) for g in groups): - await interaction.channel.send(msg) + for idx, group in enumerate(groups): + + 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 - 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 State.log_groups(groups) diff --git a/py/util.py b/py/util.py new file mode 100644 index 0000000..3f7715a --- /dev/null +++ b/py/util.py @@ -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 ''