2024-08-07 22:15:39 +01:00
|
|
|
import discord
|
2024-08-07 23:23:52 +01:00
|
|
|
import random
|
2024-08-08 23:32:52 +01:00
|
|
|
import logging
|
2024-08-09 14:03:53 +01:00
|
|
|
import importlib
|
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-08 23:32:52 +01:00
|
|
|
# Config contains
|
|
|
|
# TOKEN : str - Discord bot token
|
|
|
|
# SERVERS : list[int] - ids of the servers to have commands active
|
|
|
|
# OWNERS : list[int] - ids of owners able to use the owner commands
|
2024-08-08 19:45:53 +01:00
|
|
|
import config
|
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-08 19:45:53 +01:00
|
|
|
bot = commands.Bot(command_prefix='$', description="Matchy matches matchees", intents=intents)
|
2024-08-07 22:15:39 +01:00
|
|
|
|
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-08 19:45:53 +01:00
|
|
|
@bot.command()
|
2024-08-09 00:32:59 +01:00
|
|
|
@commands.dm_only()
|
2024-08-09 22:08:05 +01:00
|
|
|
@commands.check(lambda ctx: ctx.message.author.id in config.OWNERS)
|
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-09 14:03:53 +01:00
|
|
|
importlib.reload(config)
|
|
|
|
logger.info(f"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-08 23:32:52 +01:00
|
|
|
logger.info(f"Synced {len(synced)} command(s)")
|
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:08:05 +01:00
|
|
|
@bot.tree.command(description = "Match up matchees")
|
2024-08-09 00:32:59 +01:00
|
|
|
@commands.guild_only()
|
2024-08-09 22:08:05 +01:00
|
|
|
@app_commands.describe(group_min = "Minimum matchees per match (defaults to 3)", matchee_role = "Role for matchees (defaults to @Matchee)")
|
|
|
|
async def match(interaction: discord.Interaction, group_min: int = None, matchee_role: str = None):
|
2024-08-09 00:06:23 +01:00
|
|
|
"""Match groups of channel members"""
|
2024-08-09 22:08:05 +01:00
|
|
|
|
|
|
|
logger.info(f"User {interaction.user} from {interaction.guild.name} in #{interaction.channel.name} requested "
|
|
|
|
+ f"'/match group_min={group_min} matchee_role={matchee_role}'")
|
|
|
|
|
|
|
|
# Sort out the defaults, if not specified they'll come in as None
|
|
|
|
if not group_min:
|
|
|
|
group_min = 3
|
|
|
|
if not matchee_role:
|
|
|
|
matchee_role = "Matchee"
|
|
|
|
|
|
|
|
# Grab the roles and verify the given role
|
|
|
|
matcher_role = next((r for r in interaction.guild.roles if r.name == "Matcher"), None)
|
|
|
|
matchee_role = next((r for r in interaction.guild.roles if r.name == matchee_role), None)
|
|
|
|
if not matchee_role:
|
|
|
|
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 22:08:05 +01:00
|
|
|
matcher = matcher_role and matcher_role in interaction.user.roles
|
2024-08-09 14:12:28 +01:00
|
|
|
|
2024-08-09 22:08:05 +01:00
|
|
|
# Create our groups!
|
|
|
|
matchees = list(m for m in interaction.channel.members if matchee_role in m.roles)
|
|
|
|
groups = matchees_to_groups(matchees, group_min)
|
|
|
|
|
|
|
|
# Post about all the groups with a button to send to the channel
|
|
|
|
msg = f"{'\n'.join(group_to_message(g) for g in groups)}"
|
|
|
|
if not matcher: # Let a non-matcher know why they don't have the button
|
|
|
|
msg += f"\nYou'll need the {matcher_role.mention if matcher_role else 'Matcher'} role to send this to the channel, sorry!"
|
|
|
|
await interaction.response.send_message(msg, ephemeral=True, silent=True, view=(GroupMessageButton(groups) if matcher else discord.utils.MISSING))
|
|
|
|
|
|
|
|
logger.info(f"Done. Matched {len(matchees)} matchees into {len(groups)} groups.")
|
|
|
|
|
|
|
|
async def send_groups_to_channel(channel : discord.channel, groups : list[list[discord.Member]]):
|
|
|
|
"""Send the group messages to a channel"""
|
|
|
|
for msg in (group_to_message(g) for g in groups):
|
|
|
|
await channel.send(msg)
|
|
|
|
await channel.send("That's all folks, happy matching and remember - DFTBA!")
|
|
|
|
|
|
|
|
class GroupMessageButton(discord.ui.View):
|
|
|
|
"""A button to press to send the groups to the channel"""
|
|
|
|
def __init__(self, groups : list[list[discord.Member]], timeout : int = 180):
|
|
|
|
self.groups = groups
|
|
|
|
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):
|
|
|
|
await send_groups_to_channel(interaction.channel, self.groups)
|
|
|
|
await interaction.response.edit_message(content=f"Groups sent to channel!", view=None)
|
|
|
|
|
|
|
|
def matchees_to_groups(matchees : list[discord.Member], per_group : int) -> list[list[discord.Member]]:
|
|
|
|
"""Generate the groups from the set of matchees"""
|
2024-08-08 23:32:52 +01:00
|
|
|
random.shuffle(matchees)
|
2024-08-09 22:08:05 +01:00
|
|
|
num_groups = max(len(matchees)//per_group, 1)
|
|
|
|
return [matchees[i::num_groups] for i in range(num_groups)]
|
|
|
|
|
|
|
|
def group_to_message(group : list[discord.Member]) -> str:
|
|
|
|
"""Get the message to send for each group"""
|
|
|
|
mentions = [m.mention for m in group]
|
|
|
|
if len(group) > 1:
|
|
|
|
mentions = "{} and {}".format(', '.join(mentions[:-1]), mentions[-1])
|
2024-08-09 00:23:43 +01:00
|
|
|
else:
|
2024-08-09 22:08:05 +01:00
|
|
|
mentions = mentions[0]
|
|
|
|
return f"Matched up {mentions}!"
|
2024-08-07 23:23:52 +01:00
|
|
|
|
2024-08-09 00:06:23 +01:00
|
|
|
handler = logging.StreamHandler()
|
2024-08-09 22:08:05 +01:00
|
|
|
bot.run(config.TOKEN, log_handler=handler, root_logger=True)
|