From 67b1feb51e95d26c4cb1959911c4e1651f9aac66 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Thu, 8 Aug 2024 17:16:59 +0100 Subject: [PATCH] Clean up and use role management for permissions --- .gitignore | 2 ++ README.md | 7 +++++-- matchy.py | 42 +++++++++++++++++++++++++++--------------- 3 files changed, 34 insertions(+), 17 deletions(-) mode change 100644 => 100755 matchy.py diff --git a/.gitignore b/.gitignore index 4acd06b..cf9809a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ +__pycache__ config.py + diff --git a/README.md b/README.md index d54af23..7450e78 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # Matchy -Matchy matches matchies +Matchy matches matchees + +## Commands +### /match ## Dependencies * `python3` obviously * `discord.py` python module ## Token -Create a `config.py` file with a `TOKEN=` entry \ No newline at end of file +Create a `config.py` file with a `TOKEN=` entry diff --git a/matchy.py b/matchy.py old mode 100644 new mode 100755 index 6ace928..9b98ff7 --- a/matchy.py +++ b/matchy.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import discord import os import random @@ -8,35 +9,46 @@ from config import TOKEN intents = discord.Intents.default() intents.message_content = True intents.members = True +bot = commands.Bot(command_prefix='/', description="Matchy matches matchees", intents=intents) -bot = commands.Bot(command_prefix='/', description="Matchy matches matchies", intents=intents) +# Find a role by name +def find_role_by_name(roles: list[discord.Role], name: str) -> discord.Role: + role = None + for r in roles: + if r.name == name: + role = r + break + return role @bot.event async def on_ready(): - print("Bot is Up and Ready!") try: synced = await bot.tree.sync() print(f"Synced {len(synced)} command(s)") except Exception as e: print(e) + print("Bot is Up and Ready!") @bot.tree.command() @app_commands.describe(per_group = "People per group") -async def matchy(interaction: discord.Interaction, per_group: int): - # Find the role - role = None - for r in interaction.guild.roles: - if r.name == "matchy": - role = r - break - if not role: - await interaction.response.send_message("Server has no @matchy role :(", ephemeral=True) +async def match(interaction: discord.Interaction, per_group: int): + + # Grab the roles + matchee = find_role_by_name(interaction.guild.roles, "Matchee") + matcher = find_role_by_name(interaction.guild.roles, "Matcher") + if not matchee or not matcher: + await interaction.response.send_message("Server has missing matchy roles :(", ephemeral=True) + return + + # Validate that the user has the scope we need + if matcher not in interaction.user.roles: + await interaction.response.send_message(f"You'll need the {matcher.mention} role to do this, sorry!", ephemeral=True) return # Find all the members in the role matchies = [] for member in interaction.channel.members: - if not member.bot and role in member.roles: + if not member.bot and matchee in member.roles: matchies.append(member) break @@ -46,9 +58,9 @@ async def matchy(interaction: discord.Interaction, per_group: int): # Calculate the number of groups to generate total_num = len(matchies) num_groups = total_num//per_group - if not num_groups: + if not num_groups: # Account for when it rounds down to 0 num_groups = 1 - + # Split members into groups and share them groups = [matchies[i::num_groups] for i in range(num_groups)] for group in groups: @@ -57,4 +69,4 @@ async def matchy(interaction: discord.Interaction, per_group: int): await interaction.response.send_message("Done :)", ephemeral=True, silent=True) -bot.run(os.getenv(config.TOKEN)) \ No newline at end of file +bot.run(TOKEN)