2024-08-09 22:28:02 +01:00
|
|
|
"""
|
|
|
|
matchy.py - Discord bot that matches people into groups
|
|
|
|
"""
|
2024-08-08 23:32:52 +01:00
|
|
|
import logging
|
2024-08-09 22:28:02 +01:00
|
|
|
import discord
|
2024-08-08 00:09:30 +01:00
|
|
|
from discord import app_commands
|
2024-08-07 22:45:23 +01:00
|
|
|
from discord.ext import commands
|
2024-08-10 09:44:22 +01:00
|
|
|
import matching
|
2024-08-11 12:16:23 +01:00
|
|
|
import state
|
2024-08-10 10:45:44 +01:00
|
|
|
import config
|
2024-08-11 11:37:20 +01:00
|
|
|
import re
|
2024-08-10 09:44:22 +01:00
|
|
|
|
2024-08-10 10:06:13 +01:00
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
STATE_FILE = "state.json"
|
|
|
|
CONFIG_FILE = "config.json"
|
|
|
|
|
|
|
|
Config = config.load_from_file(CONFIG_FILE)
|
|
|
|
State = state.load_from_file(STATE_FILE)
|
2024-08-07 23:23:52 +01:00
|
|
|
|
2024-08-08 23:32:52 +01:00
|
|
|
logger = logging.getLogger("matchy")
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
2024-08-07 22:15:39 +01:00
|
|
|
intents = discord.Intents.default()
|
|
|
|
intents.message_content = True
|
2024-08-07 23:23:52 +01:00
|
|
|
intents.members = True
|
2024-08-09 22:12:58 +01:00
|
|
|
bot = commands.Bot(command_prefix='$',
|
|
|
|
description="Matchy matches matchees", intents=intents)
|
|
|
|
|
2024-08-07 22:15:39 +01:00
|
|
|
|
2024-08-11 11:37:20 +01:00
|
|
|
@bot.event
|
|
|
|
async def setup_hook():
|
|
|
|
bot.add_dynamic_items(DynamicGroupButton)
|
|
|
|
|
|
|
|
|
2024-08-08 00:09:30 +01:00
|
|
|
@bot.event
|
|
|
|
async def on_ready():
|
2024-08-09 22:08:05 +01:00
|
|
|
"""Bot is ready and connected"""
|
2024-08-08 23:32:52 +01:00
|
|
|
logger.info("Bot is up and ready!")
|
2024-08-09 22:08:05 +01:00
|
|
|
activity = discord.Game("/match")
|
|
|
|
await bot.change_presence(status=discord.Status.online, activity=activity)
|
2024-08-07 23:23:52 +01:00
|
|
|
|
2024-08-09 22:12:58 +01:00
|
|
|
|
2024-08-10 10:06:13 +01:00
|
|
|
def owner_only(ctx: commands.Context) -> bool:
|
|
|
|
"""Checks the author is an owner"""
|
2024-08-11 17:53:37 +01:00
|
|
|
return State.get_user_has_scope(ctx.message.author.id, state.AuthScope.OWNER)
|
2024-08-10 10:06:13 +01:00
|
|
|
|
|
|
|
|
2024-08-08 19:45:53 +01:00
|
|
|
@bot.command()
|
2024-08-09 00:32:59 +01:00
|
|
|
@commands.dm_only()
|
2024-08-10 10:06:13 +01:00
|
|
|
@commands.check(owner_only)
|
2024-08-09 16:34:11 +01:00
|
|
|
async def sync(ctx: commands.Context):
|
2024-08-09 22:08:05 +01:00
|
|
|
"""Handle sync command"""
|
2024-08-11 17:53:37 +01:00
|
|
|
msg = await ctx.reply("Reloading state...", ephemeral=True)
|
|
|
|
global State
|
|
|
|
State = state.load_from_file(STATE_FILE)
|
|
|
|
logger.info("Reloaded state")
|
2024-08-09 16:34:11 +01:00
|
|
|
|
2024-08-09 22:08:05 +01:00
|
|
|
await msg.edit(content="Syncing commands...")
|
2024-08-08 19:45:53 +01:00
|
|
|
synced = await bot.tree.sync()
|
2024-08-09 22:28:02 +01:00
|
|
|
logger.info("Synced %s command(s)", len(synced))
|
2024-08-09 16:34:11 +01:00
|
|
|
|
|
|
|
await msg.edit(content="Done!")
|
2024-08-08 19:45:53 +01:00
|
|
|
|
2024-08-09 22:12:58 +01:00
|
|
|
|
2024-08-10 00:01:43 +01:00
|
|
|
@bot.command()
|
|
|
|
@commands.dm_only()
|
2024-08-10 10:06:13 +01:00
|
|
|
@commands.check(owner_only)
|
2024-08-10 00:01:43 +01:00
|
|
|
async def close(ctx: commands.Context):
|
|
|
|
"""Handle restart command"""
|
|
|
|
await ctx.reply("Closing bot...", ephemeral=True)
|
|
|
|
logger.info("Closing down the bot")
|
|
|
|
await bot.close()
|
|
|
|
|
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
@bot.tree.command(description="Join the matchees for this channel")
|
|
|
|
@commands.guild_only()
|
|
|
|
async def join(interaction: discord.Interaction):
|
2024-08-11 19:02:47 +01:00
|
|
|
State.set_user_active_in_channel(
|
2024-08-11 17:53:37 +01:00
|
|
|
interaction.user.id, interaction.channel.id)
|
|
|
|
state.save_to_file(State, STATE_FILE)
|
|
|
|
await interaction.response.send_message(
|
|
|
|
f"Roger roger {interaction.user.mention}!\n"
|
|
|
|
+ f"Added you to {interaction.channel.mention}!",
|
|
|
|
ephemeral=True, silent=True)
|
|
|
|
|
|
|
|
|
|
|
|
@bot.tree.command(description="Leave the matchees for this channel")
|
|
|
|
@commands.guild_only()
|
|
|
|
async def leave(interaction: discord.Interaction):
|
2024-08-11 19:02:47 +01:00
|
|
|
State.set_user_active_in_channel(
|
2024-08-11 17:53:37 +01:00
|
|
|
interaction.user.id, interaction.channel.id, False)
|
|
|
|
state.save_to_file(State, STATE_FILE)
|
|
|
|
await interaction.response.send_message(
|
|
|
|
f"No worries {interaction.user.mention}. Come back soon :)", ephemeral=True, silent=True)
|
|
|
|
|
|
|
|
|
2024-08-11 19:02:47 +01:00
|
|
|
@bot.tree.command(description="Pause your matching in this channel for a number of days")
|
|
|
|
@commands.guild_only()
|
|
|
|
@app_commands.describe(days="Days to pause for (defaults to 7)")
|
|
|
|
async def pause(interaction: discord.Interaction, days: int = None):
|
|
|
|
if not days: # Default to a week
|
|
|
|
days = 7
|
|
|
|
State.set_user_paused_in_channel(
|
|
|
|
interaction.user.id, interaction.channel.id, days)
|
|
|
|
state.save_to_file(State, STATE_FILE)
|
|
|
|
await interaction.response.send_message(
|
|
|
|
f"Sure thing {interaction.user.mention}. Paused you for {days} days!", ephemeral=True, silent=True)
|
|
|
|
|
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
@bot.tree.command(description="List the matchees for this channel")
|
|
|
|
@commands.guild_only()
|
|
|
|
async def list(interaction: discord.Interaction):
|
|
|
|
matchees = get_matchees_in_channel(interaction.channel)
|
|
|
|
mentions = [m.mention for m in matchees]
|
|
|
|
msg = "Current matchees in this channel:\n" + \
|
|
|
|
f"{', '.join(mentions[:-1])} and {mentions[-1]}"
|
|
|
|
await interaction.response.send_message(msg, ephemeral=True, silent=True)
|
|
|
|
|
|
|
|
|
2024-08-09 22:12:58 +01:00
|
|
|
@bot.tree.command(description="Match up matchees")
|
2024-08-09 00:32:59 +01:00
|
|
|
@commands.guild_only()
|
2024-08-11 17:53:37 +01:00
|
|
|
@app_commands.describe(members_min="Minimum matchees per match (defaults to 3)")
|
|
|
|
async def match(interaction: discord.Interaction, members_min: int = None):
|
2024-08-09 00:06:23 +01:00
|
|
|
"""Match groups of channel members"""
|
2024-08-09 22:12:58 +01:00
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
logger.info("Handling request '/match group_min=%s", members_min)
|
2024-08-09 22:28:02 +01:00
|
|
|
logger.info("User %s from %s in #%s", interaction.user,
|
|
|
|
interaction.guild.name, interaction.channel.name)
|
2024-08-09 22:08:05 +01:00
|
|
|
|
|
|
|
# Sort out the defaults, if not specified they'll come in as None
|
2024-08-11 11:37:20 +01:00
|
|
|
if not members_min:
|
|
|
|
members_min = 3
|
2024-08-09 14:12:28 +01:00
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
# Grab the groups
|
|
|
|
groups = active_members_to_groups(interaction.channel, members_min)
|
|
|
|
|
|
|
|
# Let the user know when there's nobody to match
|
|
|
|
if not groups:
|
|
|
|
await interaction.response.send_message("Nobody to match up :(", ephemeral=True, silent=True)
|
|
|
|
return
|
2024-08-09 22:12:58 +01:00
|
|
|
|
2024-08-09 22:08:05 +01:00
|
|
|
# Post about all the groups with a button to send to the channel
|
2024-08-11 11:53:05 +01:00
|
|
|
groups_list = '\n'.join(matching.group_to_message(g) for g in groups)
|
2024-08-11 12:07:19 +01:00
|
|
|
msg = f"Roger! I've generated example groups for ya:\n\n{groups_list}"
|
2024-08-11 11:37:20 +01:00
|
|
|
view = discord.utils.MISSING
|
2024-08-11 11:53:05 +01:00
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
if State.get_user_has_scope(interaction.user.id, state.AuthScope.MATCHER):
|
2024-08-11 11:53:05 +01:00
|
|
|
# Let a non-matcher know why they don't have the button
|
2024-08-11 17:53:37 +01:00
|
|
|
msg += f"\n\nYou'll need the {state.AuthScope.MATCHER} scope to post this to the channel, sorry!"
|
2024-08-11 11:37:20 +01:00
|
|
|
else:
|
2024-08-11 11:53:05 +01:00
|
|
|
# Otherwise set up the button
|
|
|
|
msg += "\n\nClick the button to match up groups and send them to the channel.\n"
|
2024-08-11 11:37:20 +01:00
|
|
|
view = discord.ui.View(timeout=None)
|
2024-08-11 17:53:37 +01:00
|
|
|
view.add_item(DynamicGroupButton(members_min))
|
2024-08-11 11:53:05 +01:00
|
|
|
|
2024-08-11 11:37:20 +01:00
|
|
|
await interaction.response.send_message(msg, ephemeral=True, silent=True, view=view)
|
|
|
|
|
|
|
|
logger.info("Done.")
|
|
|
|
|
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
# Increment when adjusting the custom_id so we don't confuse old users
|
|
|
|
_BUTTON_CUSTOM_ID_VERSION = 1
|
|
|
|
|
|
|
|
|
2024-08-11 11:37:20 +01:00
|
|
|
class DynamicGroupButton(discord.ui.DynamicItem[discord.ui.Button],
|
2024-08-11 17:53:37 +01:00
|
|
|
template=f'match:v{_BUTTON_CUSTOM_ID_VERSION}:' + r'min:(?P<min>[0-9]+)'):
|
|
|
|
def __init__(self, min: int) -> None:
|
2024-08-11 11:37:20 +01:00
|
|
|
super().__init__(
|
|
|
|
discord.ui.Button(
|
|
|
|
label='Match Groups!',
|
|
|
|
style=discord.ButtonStyle.blurple,
|
2024-08-11 17:53:37 +01:00
|
|
|
custom_id=f'match:min:{min}',
|
2024-08-11 11:37:20 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self.min: int = min
|
|
|
|
|
|
|
|
# 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], /):
|
|
|
|
min = int(match['min'])
|
2024-08-11 17:53:37 +01:00
|
|
|
return cls(min)
|
2024-08-11 11:37:20 +01:00
|
|
|
|
|
|
|
async def callback(self, interaction: discord.Interaction) -> None:
|
|
|
|
"""Match up people when the button is pressed"""
|
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
logger.info("Handling button press min=%s", self.min)
|
2024-08-11 11:37:20 +01:00
|
|
|
logger.info("User %s from %s in #%s", interaction.user,
|
|
|
|
interaction.guild.name, interaction.channel.name)
|
2024-08-11 12:07:19 +01:00
|
|
|
|
2024-08-11 11:53:05 +01:00
|
|
|
# Let the user know we've recieved the message
|
|
|
|
await interaction.response.send_message(content="Matchy is matching matchees...", ephemeral=True)
|
2024-08-11 11:37:20 +01:00
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
groups = active_members_to_groups(interaction.channel, self.min)
|
2024-08-11 11:37:20 +01:00
|
|
|
|
2024-08-11 11:53:05 +01:00
|
|
|
# Send the groups
|
2024-08-11 11:37:20 +01:00
|
|
|
for msg in (matching.group_to_message(g) for g in groups):
|
2024-08-10 09:44:22 +01:00
|
|
|
await interaction.channel.send(msg)
|
2024-08-11 12:07:19 +01:00
|
|
|
|
2024-08-11 11:53:05 +01:00
|
|
|
# Close off with a message
|
2024-08-10 09:44:22 +01:00
|
|
|
await interaction.channel.send("That's all folks, happy matching and remember - DFTBA!")
|
2024-08-11 11:53:05 +01:00
|
|
|
|
|
|
|
# Save the groups to the history
|
2024-08-11 17:53:37 +01:00
|
|
|
State.log_groups(groups)
|
|
|
|
state.save_to_file(State, STATE_FILE)
|
|
|
|
|
|
|
|
logger.info("Done! Matched into %s groups.", len(groups))
|
|
|
|
|
|
|
|
|
|
|
|
def get_matchees_in_channel(channel: discord.channel):
|
|
|
|
"""Fetches the matchees in a channel"""
|
2024-08-11 19:02:47 +01:00
|
|
|
# Reactivate any unpaused users
|
|
|
|
State.reactivate_users(channel.id)
|
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
# Gather up the prospective matchees
|
|
|
|
return [m for m in channel.members if State.get_user_active_in_channel(m.id, channel.id)]
|
|
|
|
|
|
|
|
|
|
|
|
def active_members_to_groups(channel: discord.channel, min_members: int):
|
|
|
|
"""Helper to create groups from channel members"""
|
|
|
|
|
|
|
|
# Gather up the prospective matchees
|
|
|
|
matchees = get_matchees_in_channel(channel)
|
2024-08-11 11:37:20 +01:00
|
|
|
|
2024-08-11 17:53:37 +01:00
|
|
|
# Create our groups!
|
|
|
|
return matching.members_to_groups(matchees, State, min_members, allow_fallback=True)
|
2024-08-10 10:06:13 +01:00
|
|
|
|
2024-08-09 22:12:58 +01:00
|
|
|
|
2024-08-09 23:14:42 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
handler = logging.StreamHandler()
|
2024-08-10 10:45:44 +01:00
|
|
|
bot.run(Config.token, log_handler=handler, root_logger=True)
|