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

View file

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

40
matchy.py Normal file → Executable file
View file

@ -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,7 +58,7 @@ 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
@ -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))
bot.run(TOKEN)