Use commands rather than text values
* Fix up the group matching to spread more evenly * Also swap to using a config.py rather than .envrc or similar
This commit is contained in:
parent
2e9be1cace
commit
3da8065fa6
3 changed files with 39 additions and 39 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1 @@
|
||||||
.envrc
|
config.py
|
||||||
|
|
|
@ -4,4 +4,6 @@ Matchy matches matchies
|
||||||
## Dependencies
|
## Dependencies
|
||||||
* `python3` obviously
|
* `python3` obviously
|
||||||
* `discord.py` python module
|
* `discord.py` python module
|
||||||
* `BOT_TOKEN` envar set to a discord bot token
|
|
||||||
|
## Token
|
||||||
|
Create a `config.py` file with a `TOKEN=<token>` entry
|
72
matchy.py
72
matchy.py
|
@ -1,62 +1,60 @@
|
||||||
import discord
|
import discord
|
||||||
import os
|
import os
|
||||||
import argparse
|
|
||||||
import random
|
import random
|
||||||
from itertools import islice
|
from discord import app_commands
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
from config import TOKEN
|
||||||
def chunk(it, size):
|
|
||||||
it = iter(it)
|
|
||||||
return iter(lambda: tuple(islice(it, size)), ())
|
|
||||||
|
|
||||||
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 matchies", intents=intents)
|
bot = commands.Bot(command_prefix='/', description="Matchy matches matchies", intents=intents)
|
||||||
|
|
||||||
@bot.command()
|
@bot.event
|
||||||
async def hello(ctx, *args):
|
async def on_ready():
|
||||||
"""Just say hello back"""
|
print("Bot is Up and Ready!")
|
||||||
await ctx.send(f"Hi {ctx.author.mention}")
|
try:
|
||||||
|
synced = await bot.tree.sync()
|
||||||
|
print(f"Synced {len(synced)} command(s)")
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
@bot.command()
|
@bot.tree.command()
|
||||||
async def matchy(ctx, *args):
|
@app_commands.describe(per_group = "People per group")
|
||||||
"""Create some random groups"""
|
async def matchy(interaction: discord.Interaction, per_group: int):
|
||||||
argparser = argparse.ArgumentParser(exit_on_error=False, add_help=False, usage='$matchy [options]')
|
|
||||||
argparser.add_argument("--num", default=3, type=int, help="Number of people to match up")
|
|
||||||
argparser.add_argument("--help", "-h", action='store_true', help=argparse.SUPPRESS)
|
|
||||||
args = argparser.parse_args(args)
|
|
||||||
|
|
||||||
# Print the help if requested
|
|
||||||
if args.help:
|
|
||||||
await ctx.send(argparser.format_help())
|
|
||||||
return
|
|
||||||
|
|
||||||
# Find the role
|
# Find the role
|
||||||
role = None
|
role = None
|
||||||
for r in ctx.message.guild.roles:
|
for r in interaction.guild.roles:
|
||||||
if r.name == "matchy":
|
if r.name == "matchy":
|
||||||
role = r
|
role = r
|
||||||
break
|
break
|
||||||
if not role:
|
if not role:
|
||||||
await ctx.send("Error: server has no @matchy role!")
|
await interaction.response.send_message("Server has no @matchy role :(", ephemeral=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Find all the members in the role
|
# Find all the members in the role
|
||||||
matchies = []
|
matchies = []
|
||||||
for member in ctx.channel.members:
|
for member in interaction.channel.members:
|
||||||
for r in member.roles:
|
if not member.bot and role in member.roles:
|
||||||
if r == role:
|
matchies.append(member)
|
||||||
matchies.append(member)
|
break
|
||||||
break
|
|
||||||
|
|
||||||
# Shuffle the people for randomness
|
# Shuffle the people for randomness
|
||||||
random.shuffle(matchies)
|
random.shuffle(matchies)
|
||||||
|
|
||||||
# Chunk up the groups and share them
|
|
||||||
for group in chunk(matchies, args.num):
|
|
||||||
mentions = [m.mention for m in group]
|
|
||||||
await ctx.send("A group! " + ", ".join(mentions))
|
|
||||||
|
|
||||||
bot.run(os.getenv("BOT_TOKEN"))
|
# Calculate the number of groups to generate
|
||||||
|
total_num = len(matchies)
|
||||||
|
num_groups = total_num//per_group
|
||||||
|
if not num_groups:
|
||||||
|
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:
|
||||||
|
mentions = [m.mention for m in group]
|
||||||
|
await interaction.channel.send("Group : " + ", ".join(mentions))
|
||||||
|
|
||||||
|
await interaction.response.send_message("Done :)", ephemeral=True, silent=True)
|
||||||
|
|
||||||
|
bot.run(os.getenv(config.TOKEN))
|
Loading…
Add table
Reference in a new issue