Clean up and use role management for permissions

This commit is contained in:
Marc Di Luzio 2024-08-08 17:16:59 +01:00
parent 3da8065fa6
commit 67b1feb51e
3 changed files with 34 additions and 17 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
__pycache__
config.py config.py

View file

@ -1,5 +1,8 @@
# Matchy # Matchy
Matchy matches matchies Matchy matches matchees
## Commands
### /match
## Dependencies ## Dependencies
* `python3` obviously * `python3` obviously

40
matchy.py Normal file → Executable file
View file

@ -1,3 +1,4 @@
#!/usr/bin/env python3
import discord import discord
import os import os
import random import random
@ -8,35 +9,46 @@ from config import TOKEN
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
intents.members = 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 @bot.event
async def on_ready(): async def on_ready():
print("Bot is Up and Ready!")
try: try:
synced = await bot.tree.sync() synced = await bot.tree.sync()
print(f"Synced {len(synced)} command(s)") print(f"Synced {len(synced)} command(s)")
except Exception as e: except Exception as e:
print(e) print(e)
print("Bot is Up and Ready!")
@bot.tree.command() @bot.tree.command()
@app_commands.describe(per_group = "People per group") @app_commands.describe(per_group = "People per group")
async def matchy(interaction: discord.Interaction, per_group: int): async def match(interaction: discord.Interaction, per_group: int):
# Find the role
role = None # Grab the roles
for r in interaction.guild.roles: matchee = find_role_by_name(interaction.guild.roles, "Matchee")
if r.name == "matchy": matcher = find_role_by_name(interaction.guild.roles, "Matcher")
role = r if not matchee or not matcher:
break await interaction.response.send_message("Server has missing matchy roles :(", ephemeral=True)
if not role: return
await interaction.response.send_message("Server has no @matchy role :(", ephemeral=True)
# 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 return
# Find all the members in the role # Find all the members in the role
matchies = [] matchies = []
for member in interaction.channel.members: 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) matchies.append(member)
break break
@ -46,7 +58,7 @@ async def matchy(interaction: discord.Interaction, per_group: int):
# Calculate the number of groups to generate # Calculate the number of groups to generate
total_num = len(matchies) total_num = len(matchies)
num_groups = total_num//per_group 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 num_groups = 1
# Split members into groups and share them # Split members into groups and share them
@ -57,4 +69,4 @@ async def matchy(interaction: discord.Interaction, per_group: int):
await interaction.response.send_message("Done :)", ephemeral=True, silent=True) await interaction.response.send_message("Done :)", ephemeral=True, silent=True)
bot.run(os.getenv(config.TOKEN)) bot.run(TOKEN)