from datetime import datetime import logging from typing import List import dateparser import humanize import pluralkit.utils from pluralkit import Member from pluralkit.bot import utils, embeds, help from pluralkit.bot.commands import * logger = logging.getLogger("pluralkit.commands") @command(cmd="switch", usage=" [name|id]...", description="Registers a switch and changes the current fronter.", category="Switching commands") async def switch_member(ctx: MemberCommandContext, args: List[str]): if len(args) == 0: return embeds.error("You must pass at least one member name or ID to register a switch to.", help=help.switch_register) members: List[Member] = [] for member_name in args: # Find the member member = await utils.get_member_fuzzy(ctx.conn, ctx.system.id, member_name) if not member: return embeds.error("Couldn't find member \"{}\".".format(member_name)) members.append(member) # Compare requested switch IDs and existing fronter IDs to check for existing switches # Lists, because order matters, it makes sense to just swap fronters member_ids = [member.id for member in members] fronter_ids = (await pluralkit.utils.get_fronter_ids(ctx.conn, ctx.system.id))[0] if member_ids == fronter_ids: if len(members) == 1: return embeds.error("{} is already fronting.".format(members[0].name)) return embeds.error("Members {} are already fronting.".format(", ".join([m.name for m in members]))) # Also make sure there aren't any duplicates if len(set(member_ids)) != len(member_ids): return embeds.error("Duplicate members in member list.") # Log the switch async with ctx.conn.transaction(): switch_id = await db.add_switch(ctx.conn, system_id=ctx.system.id) for member in members: await db.add_switch_member(ctx.conn, switch_id=switch_id, member_id=member.id) if len(members) == 1: return embeds.success("Switch registered. Current fronter is now {}.".format(members[0].name)) else: return embeds.success("Switch registered. Current fronters are now {}.".format(", ".join([m.name for m in members]))) @command(cmd="switch out", description="Registers a switch with no one in front.", category="Switching commands") async def switch_out(ctx: MemberCommandContext, args: List[str]): # Get current fronters fronters, _ = await pluralkit.utils.get_fronter_ids(ctx.conn, system_id=ctx.system.id) if not fronters: return embeds.error("There's already no one in front.") # Log it, and don't log any members await db.add_switch(ctx.conn, system_id=ctx.system.id) return embeds.success("Switch-out registered.") @command(cmd="switch move", usage="