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-10 10:45:44 +01:00
|
|
|
import history
|
|
|
|
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-10 10:45:44 +01:00
|
|
|
Config = config.load()
|
|
|
|
History = history.load()
|
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-10 10:45:44 +01:00
|
|
|
return ctx.message.author.id in Config.owners
|
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-09 16:34:11 +01:00
|
|
|
msg = await ctx.reply("Reloading config...", ephemeral=True)
|
2024-08-10 10:55:09 +01:00
|
|
|
Config.reload()
|
2024-08-09 22:28:02 +01:00
|
|
|
logger.info("Reloaded config")
|
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-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 11:37:20 +01:00
|
|
|
@app_commands.describe(members_min="Minimum matchees per match (defaults to 3)",
|
2024-08-09 22:28:02 +01:00
|
|
|
matchee_role="Role for matchees (defaults to @Matchee)")
|
2024-08-11 11:37:20 +01:00
|
|
|
async def match(interaction: discord.Interaction, members_min: int = None, matchee_role: str = None):
|
2024-08-09 00:06:23 +01:00
|
|
|
"""Match groups of channel members"""
|
2024-08-09 22:12:58 +01:00
|
|
|
|
2024-08-09 22:28:02 +01:00
|
|
|
logger.info("Handling request '/match group_min=%s matchee_role=%s'",
|
2024-08-11 11:37:20 +01:00
|
|
|
members_min, matchee_role)
|
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 22:08:05 +01:00
|
|
|
if not matchee_role:
|
|
|
|
matchee_role = "Matchee"
|
|
|
|
|
|
|
|
# Grab the roles and verify the given role
|
2024-08-10 09:44:22 +01:00
|
|
|
matcher = matching.get_role_from_guild(interaction.guild, "Matcher")
|
2024-08-09 22:53:28 +01:00
|
|
|
matcher = matcher and matcher in interaction.user.roles
|
2024-08-10 09:44:22 +01:00
|
|
|
matchee = matching.get_role_from_guild(interaction.guild, matchee_role)
|
2024-08-09 22:50:22 +01:00
|
|
|
if not matchee:
|
2024-08-09 22:08:05 +01:00
|
|
|
await interaction.response.send_message(f"Server is missing '{matchee_role}' role :(", ephemeral=True)
|
2024-08-07 23:23:52 +01:00
|
|
|
return
|
2024-08-09 14:12:28 +01:00
|
|
|
|
2024-08-11 11:37:20 +01:00
|
|
|
# Create some example groups to show the user
|
2024-08-09 22:12:58 +01:00
|
|
|
matchees = list(
|
2024-08-09 22:50:22 +01:00
|
|
|
m for m in interaction.channel.members if matchee in m.roles)
|
2024-08-11 11:37:20 +01:00
|
|
|
groups = matching.members_to_groups(
|
|
|
|
matchees, History, members_min, allow_fallback=True)
|
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:37:20 +01:00
|
|
|
msg = "Example groups:\n" + \
|
|
|
|
'\n'.join(matching.group_to_message(g) for g in groups)
|
|
|
|
view = discord.utils.MISSING
|
2024-08-09 22:12:58 +01:00
|
|
|
if not matcher: # Let a non-matcher know why they don't have the button
|
2024-08-09 22:53:28 +01:00
|
|
|
msg += f"\nYou'll need the {matcher.mention if matcher else 'Matcher'}"
|
2024-08-09 22:28:02 +01:00
|
|
|
msg += " role to send this to the channel, sorry!"
|
2024-08-11 11:37:20 +01:00
|
|
|
else:
|
|
|
|
view = discord.ui.View(timeout=None)
|
|
|
|
view.add_item(DynamicGroupButton(members_min, matchee_role))
|
|
|
|
await interaction.response.send_message(msg, ephemeral=True, silent=True, view=view)
|
|
|
|
|
|
|
|
logger.info("Done.")
|
|
|
|
|
|
|
|
|
|
|
|
class DynamicGroupButton(discord.ui.DynamicItem[discord.ui.Button],
|
|
|
|
template=r'match:min:(?P<min>[0-9]+):role:(?P<role>[@\w\s]+)'):
|
|
|
|
def __init__(self, min: int, role: str) -> None:
|
|
|
|
super().__init__(
|
|
|
|
discord.ui.Button(
|
|
|
|
label='Match Groups!',
|
|
|
|
style=discord.ButtonStyle.blurple,
|
|
|
|
custom_id=f'match:min:{min}:role:{role}',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.min: int = min
|
|
|
|
self.role: int = role
|
|
|
|
|
|
|
|
# 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'])
|
|
|
|
role = str(match['role'])
|
|
|
|
return cls(min, role)
|
|
|
|
|
|
|
|
async def callback(self, interaction: discord.Interaction) -> None:
|
|
|
|
"""Match up people when the button is pressed"""
|
|
|
|
|
|
|
|
logger.info("Handling button press min=%s role=%s'", self.min, self.role)
|
|
|
|
logger.info("User %s from %s in #%s", interaction.user,
|
|
|
|
interaction.guild.name, interaction.channel.name)
|
|
|
|
|
|
|
|
# Grab the role
|
|
|
|
matchee = matching.get_role_from_guild(interaction.guild, self.role)
|
|
|
|
if not matchee:
|
|
|
|
await interaction.response.send_message(f"Server is missing '{self.role}' role :(", ephemeral=True)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Create our groups!
|
|
|
|
matchees = list(
|
|
|
|
m for m in interaction.channel.members if matchee in m.roles)
|
|
|
|
groups = matching.members_to_groups(
|
|
|
|
matchees, History, self.min, allow_fallback=True)
|
|
|
|
|
|
|
|
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)
|
|
|
|
await interaction.channel.send("That's all folks, happy matching and remember - DFTBA!")
|
2024-08-09 22:28:02 +01:00
|
|
|
await interaction.response.edit_message(content="Groups sent to channel!", view=None)
|
2024-08-11 11:37:20 +01:00
|
|
|
History.save_groups_to_history(groups)
|
|
|
|
|
|
|
|
logger.info("Done. Matched %s matchees into %s groups.",
|
|
|
|
len(matchees), len(groups))
|
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)
|