2024-08-07 22:15:39 +01:00
|
|
|
import discord
|
2024-08-07 23:23:52 +01:00
|
|
|
import random
|
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 19:45:53 +01:00
|
|
|
import config
|
2024-08-07 23:23:52 +01:00
|
|
|
|
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 17:16:59 +01:00
|
|
|
# Find a role by name
|
|
|
|
def find_role_by_name(roles: list[discord.Role], name: str) -> discord.Role:
|
|
|
|
for r in roles:
|
|
|
|
if r.name == name:
|
2024-08-08 19:45:53 +01:00
|
|
|
return r
|
|
|
|
return None
|
2024-08-07 22:15:39 +01:00
|
|
|
|
2024-08-08 17:37:08 +01:00
|
|
|
# Get the ordinal for an int
|
|
|
|
def get_ordinal(num : int):
|
|
|
|
if num > 9:
|
|
|
|
secondToLastDigit = str(num)[-2]
|
|
|
|
if secondToLastDigit == '1':
|
|
|
|
return str(num)+'th'
|
|
|
|
lastDigit = num % 10
|
|
|
|
if (lastDigit == 1):
|
|
|
|
return str(num)+'st'
|
|
|
|
elif (lastDigit == 2):
|
|
|
|
return str(num)+'nd'
|
|
|
|
elif (lastDigit == 3):
|
|
|
|
return str(num)+'rd'
|
|
|
|
else:
|
|
|
|
return str(num)+'th'
|
|
|
|
|
2024-08-08 19:52:57 +01:00
|
|
|
guilds = []
|
2024-08-08 19:45:53 +01:00
|
|
|
def sync_guilds():
|
|
|
|
# Cache the guild info
|
|
|
|
for guild in bot.guilds:
|
|
|
|
if guild.id in config.SERVERS:
|
2024-08-08 19:52:57 +01:00
|
|
|
guilds.append(guild)
|
2024-08-08 19:45:53 +01:00
|
|
|
|
|
|
|
print(f"Synced {len(guilds)} guild(s)")
|
2024-08-08 17:37:08 +01:00
|
|
|
|
2024-08-08 00:09:30 +01:00
|
|
|
@bot.event
|
|
|
|
async def on_ready():
|
2024-08-08 19:45:53 +01:00
|
|
|
sync_guilds()
|
2024-08-08 17:16:59 +01:00
|
|
|
print("Bot is Up and Ready!")
|
2024-08-07 23:23:52 +01:00
|
|
|
|
2024-08-08 19:45:53 +01:00
|
|
|
@bot.command()
|
|
|
|
async def sync(ctx):
|
|
|
|
if ctx.author.id not in config.OWNERS:
|
|
|
|
print(f"User {ctx.author} unauthorised for sync")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Sync the commands
|
|
|
|
synced = await bot.tree.sync()
|
|
|
|
print(f"Synced {len(synced)} command(s)")
|
|
|
|
|
|
|
|
sync_guilds()
|
|
|
|
|
|
|
|
|
2024-08-08 19:52:57 +01:00
|
|
|
@bot.tree.command(description = "Match matchees into groups", guilds = list(g for g in guilds if g.id in config.SERVERS))
|
2024-08-08 19:45:53 +01:00
|
|
|
@app_commands.describe(per_group = "Matchees per group")
|
2024-08-08 17:16:59 +01:00
|
|
|
async def match(interaction: discord.Interaction, per_group: int):
|
|
|
|
|
|
|
|
# Grab the roles
|
2024-08-08 19:52:57 +01:00
|
|
|
matchee = find_role_by_name(interaction.guild.roles, "Matchee")
|
|
|
|
matcher = find_role_by_name(interaction.guild.roles, "Matcher")
|
2024-08-08 17:16:59 +01:00
|
|
|
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)
|
2024-08-07 23:23:52 +01:00
|
|
|
return
|
2024-08-08 17:37:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
await interaction.channel.send(f"{interaction.user.display_name} asked me to match groups of {per_group}! :partying_face:")
|
2024-08-07 23:23:52 +01:00
|
|
|
|
|
|
|
# Find all the members in the role
|
|
|
|
matchies = []
|
2024-08-08 00:09:30 +01:00
|
|
|
for member in interaction.channel.members:
|
2024-08-08 17:16:59 +01:00
|
|
|
if not member.bot and matchee in member.roles:
|
2024-08-08 00:09:30 +01:00
|
|
|
matchies.append(member)
|
|
|
|
break
|
2024-08-07 23:23:52 +01:00
|
|
|
|
|
|
|
# Shuffle the people for randomness
|
|
|
|
random.shuffle(matchies)
|
2024-08-08 00:09:30 +01:00
|
|
|
|
|
|
|
# Calculate the number of groups to generate
|
|
|
|
total_num = len(matchies)
|
|
|
|
num_groups = total_num//per_group
|
2024-08-08 17:16:59 +01:00
|
|
|
if not num_groups: # Account for when it rounds down to 0
|
2024-08-08 00:09:30 +01:00
|
|
|
num_groups = 1
|
2024-08-08 17:16:59 +01:00
|
|
|
|
2024-08-08 00:09:30 +01:00
|
|
|
# Split members into groups and share them
|
|
|
|
groups = [matchies[i::num_groups] for i in range(num_groups)]
|
2024-08-08 17:37:08 +01:00
|
|
|
for idx, group in enumerate(groups):
|
2024-08-07 23:23:52 +01:00
|
|
|
mentions = [m.mention for m in group]
|
2024-08-08 17:37:08 +01:00
|
|
|
await interaction.channel.send(f"{get_ordinal(idx+1)} group: " + ", ".join(mentions))
|
2024-08-08 00:09:30 +01:00
|
|
|
|
|
|
|
await interaction.response.send_message("Done :)", ephemeral=True, silent=True)
|
2024-08-07 23:23:52 +01:00
|
|
|
|
2024-08-08 19:45:53 +01:00
|
|
|
bot.run(config.TOKEN)
|