Run autopep8 formatting
This commit is contained in:
parent
05ed2cee9c
commit
354e202b18
1 changed files with 34 additions and 20 deletions
42
matchy.py
42
matchy.py
|
@ -16,7 +16,9 @@ logger.setLevel(logging.INFO)
|
||||||
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 matchees", intents=intents)
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
|
@ -25,11 +27,11 @@ async def on_ready():
|
||||||
activity = discord.Game("/match")
|
activity = discord.Game("/match")
|
||||||
await bot.change_presence(status=discord.Status.online, activity=activity)
|
await bot.change_presence(status=discord.Status.online, activity=activity)
|
||||||
|
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
@commands.dm_only()
|
@commands.dm_only()
|
||||||
@commands.check(lambda ctx: ctx.message.author.id in config.OWNERS)
|
@commands.check(lambda ctx: ctx.message.author.id in config.OWNERS)
|
||||||
async def sync(ctx: commands.Context):
|
async def sync(ctx: commands.Context):
|
||||||
|
|
||||||
"""Handle sync command"""
|
"""Handle sync command"""
|
||||||
msg = await ctx.reply("Reloading config...", ephemeral=True)
|
msg = await ctx.reply("Reloading config...", ephemeral=True)
|
||||||
importlib.reload(config)
|
importlib.reload(config)
|
||||||
|
@ -41,9 +43,10 @@ async def sync(ctx: commands.Context):
|
||||||
|
|
||||||
await msg.edit(content="Done!")
|
await msg.edit(content="Done!")
|
||||||
|
|
||||||
@bot.tree.command(description = "Match up matchees")
|
|
||||||
|
@bot.tree.command(description="Match up matchees")
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
@app_commands.describe(group_min = "Minimum matchees per match (defaults to 3)", matchee_role = "Role for matchees (defaults to @Matchee)")
|
@app_commands.describe(group_min="Minimum matchees per match (defaults to 3)", matchee_role="Role for matchees (defaults to @Matchee)")
|
||||||
async def match(interaction: discord.Interaction, group_min: int = None, matchee_role: str = None):
|
async def match(interaction: discord.Interaction, group_min: int = None, matchee_role: str = None):
|
||||||
"""Match groups of channel members"""
|
"""Match groups of channel members"""
|
||||||
|
|
||||||
|
@ -57,49 +60,59 @@ async def match(interaction: discord.Interaction, group_min: int = None, matchee
|
||||||
matchee_role = "Matchee"
|
matchee_role = "Matchee"
|
||||||
|
|
||||||
# Grab the roles and verify the given role
|
# Grab the roles and verify the given role
|
||||||
matcher_role = next((r for r in interaction.guild.roles if r.name == "Matcher"), None)
|
matcher_role = next(
|
||||||
matchee_role = next((r for r in interaction.guild.roles if r.name == matchee_role), None)
|
(r for r in interaction.guild.roles if r.name == "Matcher"), None)
|
||||||
|
matchee_role = next(
|
||||||
|
(r for r in interaction.guild.roles if r.name == matchee_role), None)
|
||||||
if not matchee_role:
|
if not matchee_role:
|
||||||
await interaction.response.send_message(f"Server is missing '{matchee_role}' role :(", ephemeral=True)
|
await interaction.response.send_message(f"Server is missing '{matchee_role}' role :(", ephemeral=True)
|
||||||
return
|
return
|
||||||
matcher = matcher_role and matcher_role in interaction.user.roles
|
matcher = matcher_role and matcher_role in interaction.user.roles
|
||||||
|
|
||||||
# Create our groups!
|
# Create our groups!
|
||||||
matchees = list(m for m in interaction.channel.members if matchee_role in m.roles)
|
matchees = list(
|
||||||
|
m for m in interaction.channel.members if matchee_role in m.roles)
|
||||||
groups = matchees_to_groups(matchees, group_min)
|
groups = matchees_to_groups(matchees, group_min)
|
||||||
|
|
||||||
# Post about all the groups with a button to send to the channel
|
# Post about all the groups with a button to send to the channel
|
||||||
msg = f"{'\n'.join(group_to_message(g) for g in groups)}"
|
msg = f"{'\n'.join(group_to_message(g) for g in groups)}"
|
||||||
if not matcher: # Let a non-matcher know why they don't have the button
|
if not matcher: # Let a non-matcher know why they don't have the button
|
||||||
msg += f"\nYou'll need the {matcher_role.mention if matcher_role else 'Matcher'} role to send this to the channel, sorry!"
|
msg += f"\nYou'll need the {
|
||||||
|
matcher_role.mention if matcher_role else 'Matcher'} role to send this to the channel, sorry!"
|
||||||
await interaction.response.send_message(msg, ephemeral=True, silent=True, view=(GroupMessageButton(groups) if matcher else discord.utils.MISSING))
|
await interaction.response.send_message(msg, ephemeral=True, silent=True, view=(GroupMessageButton(groups) if matcher else discord.utils.MISSING))
|
||||||
|
|
||||||
logger.info(f"Done. Matched {len(matchees)} matchees into {len(groups)} groups.")
|
logger.info(f"Done. Matched {len(matchees)} matchees into {
|
||||||
|
len(groups)} groups.")
|
||||||
|
|
||||||
async def send_groups_to_channel(channel : discord.channel, groups : list[list[discord.Member]]):
|
|
||||||
|
async def send_groups_to_channel(channel: discord.channel, groups: list[list[discord.Member]]):
|
||||||
"""Send the group messages to a channel"""
|
"""Send the group messages to a channel"""
|
||||||
for msg in (group_to_message(g) for g in groups):
|
for msg in (group_to_message(g) for g in groups):
|
||||||
await channel.send(msg)
|
await channel.send(msg)
|
||||||
await channel.send("That's all folks, happy matching and remember - DFTBA!")
|
await channel.send("That's all folks, happy matching and remember - DFTBA!")
|
||||||
|
|
||||||
|
|
||||||
class GroupMessageButton(discord.ui.View):
|
class GroupMessageButton(discord.ui.View):
|
||||||
"""A button to press to send the groups to the channel"""
|
"""A button to press to send the groups to the channel"""
|
||||||
def __init__(self, groups : list[list[discord.Member]], timeout : int = 180):
|
|
||||||
|
def __init__(self, groups: list[list[discord.Member]], timeout: int = 180):
|
||||||
self.groups = groups
|
self.groups = groups
|
||||||
super().__init__(timeout=timeout)
|
super().__init__(timeout=timeout)
|
||||||
|
|
||||||
@discord.ui.button(label="Send groups to channel", style=discord.ButtonStyle.green, emoji="📮")
|
@discord.ui.button(label="Send groups to channel", style=discord.ButtonStyle.green, emoji="📮")
|
||||||
async def send_to_channel(self, interaction:discord.Interaction, button: discord.ui.Button):
|
async def send_to_channel(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||||
await send_groups_to_channel(interaction.channel, self.groups)
|
await send_groups_to_channel(interaction.channel, self.groups)
|
||||||
await interaction.response.edit_message(content=f"Groups sent to channel!", view=None)
|
await interaction.response.edit_message(content=f"Groups sent to channel!", view=None)
|
||||||
|
|
||||||
def matchees_to_groups(matchees : list[discord.Member], per_group : int) -> list[list[discord.Member]]:
|
|
||||||
|
def matchees_to_groups(matchees: list[discord.Member], per_group: int) -> list[list[discord.Member]]:
|
||||||
"""Generate the groups from the set of matchees"""
|
"""Generate the groups from the set of matchees"""
|
||||||
random.shuffle(matchees)
|
random.shuffle(matchees)
|
||||||
num_groups = max(len(matchees)//per_group, 1)
|
num_groups = max(len(matchees)//per_group, 1)
|
||||||
return [matchees[i::num_groups] for i in range(num_groups)]
|
return [matchees[i::num_groups] for i in range(num_groups)]
|
||||||
|
|
||||||
def group_to_message(group : list[discord.Member]) -> str:
|
|
||||||
|
def group_to_message(group: list[discord.Member]) -> str:
|
||||||
"""Get the message to send for each group"""
|
"""Get the message to send for each group"""
|
||||||
mentions = [m.mention for m in group]
|
mentions = [m.mention for m in group]
|
||||||
if len(group) > 1:
|
if len(group) > 1:
|
||||||
|
@ -108,5 +121,6 @@ def group_to_message(group : list[discord.Member]) -> str:
|
||||||
mentions = mentions[0]
|
mentions = mentions[0]
|
||||||
return f"Matched up {mentions}!"
|
return f"Matched up {mentions}!"
|
||||||
|
|
||||||
|
|
||||||
handler = logging.StreamHandler()
|
handler = logging.StreamHandler()
|
||||||
bot.run(config.TOKEN, log_handler=handler, root_logger=True)
|
bot.run(config.TOKEN, log_handler=handler, root_logger=True)
|
Loading…
Add table
Reference in a new issue