Refactor action confirmations
This commit is contained in:
		@@ -41,8 +41,8 @@ class CommandSuccess(CommandResponse):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CommandError(Exception, CommandResponse):
 | 
			
		||||
    def __init__(self, embed: str, help: Tuple[str, str] = None):
 | 
			
		||||
        self.text = embed
 | 
			
		||||
    def __init__(self, text: str, help: Tuple[str, str] = None):
 | 
			
		||||
        self.text = text
 | 
			
		||||
        self.help = help
 | 
			
		||||
 | 
			
		||||
    def to_embed(self):
 | 
			
		||||
@@ -108,6 +108,25 @@ class CommandContext:
 | 
			
		||||
    async def reply(self, content=None, embed=None):
 | 
			
		||||
        return await self.client.send_message(self.message.channel, content=content, embed=embed)
 | 
			
		||||
 | 
			
		||||
    async def confirm_react(self, user: discord.Member, message: str):
 | 
			
		||||
        message = await self.reply(message)
 | 
			
		||||
 | 
			
		||||
        await self.client.add_reaction(message, "✅")
 | 
			
		||||
        await self.client.add_reaction(message, "❌")
 | 
			
		||||
 | 
			
		||||
        reaction = await self.client.wait_for_reaction(emoji=["✅", "❌"], user=user, timeout=60.0*5)
 | 
			
		||||
        if not reaction:
 | 
			
		||||
            raise CommandError("Timed out - try again.")
 | 
			
		||||
        return reaction.reaction.emoji == "✅"
 | 
			
		||||
 | 
			
		||||
    async def confirm_text(self, user: discord.Member, channel: discord.Channel, confirm_text: str, message: str):
 | 
			
		||||
        await self.reply(message)
 | 
			
		||||
 | 
			
		||||
        message = await self.client.wait_for_message(channel=channel, author=user, timeout=60.0*5)
 | 
			
		||||
        if not message:
 | 
			
		||||
            raise CommandError("Timed out - try again.")
 | 
			
		||||
        return message.content == confirm_text
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import pluralkit.bot.commands.import_commands
 | 
			
		||||
import pluralkit.bot.commands.member_commands
 | 
			
		||||
 
 | 
			
		||||
@@ -158,13 +158,9 @@ async def member_delete(ctx: CommandContext):
 | 
			
		||||
    await ctx.ensure_system()
 | 
			
		||||
    member = await ctx.pop_member(CommandError("You must pass a member name.", help=help.edit_member))
 | 
			
		||||
 | 
			
		||||
    await ctx.reply(
 | 
			
		||||
        "Are you sure you want to delete {}? If so, reply to this message with the member's ID (`{}`).".format(
 | 
			
		||||
            member.name, member.hid))
 | 
			
		||||
 | 
			
		||||
    msg = await ctx.client.wait_for_message(author=ctx.message.author, channel=ctx.message.channel, timeout=60.0 * 5)
 | 
			
		||||
    if msg and msg.content.lower() == member.hid.lower():
 | 
			
		||||
        await db.delete_member(ctx.conn, member_id=member.id)
 | 
			
		||||
        return CommandSuccess("Member deleted.")
 | 
			
		||||
    else:
 | 
			
		||||
    delete_confirm_msg = "Are you sure you want to delete {}? If so, reply to this message with the member's ID (`{}`).".format(member.name, member.hid)
 | 
			
		||||
    if not await ctx.confirm_text(ctx.message.author, ctx.message.channel, member.hid, delete_confirm_msg):
 | 
			
		||||
        return CommandError("Member deletion cancelled.")
 | 
			
		||||
 | 
			
		||||
    await db.delete_member(ctx.conn, member_id=member.id)
 | 
			
		||||
    return CommandSuccess("Member deleted.")
 | 
			
		||||
 
 | 
			
		||||
@@ -103,23 +103,10 @@ async def switch_move(ctx: CommandContext):
 | 
			
		||||
        last_relative = humanize.naturaltime(pluralkit.utils.fix_time(last_timestamp))
 | 
			
		||||
        new_absolute = new_time.isoformat(sep=" ", timespec="seconds")
 | 
			
		||||
        new_relative = humanize.naturaltime(pluralkit.utils.fix_time(new_time))
 | 
			
		||||
        embed = embeds.status(
 | 
			
		||||
            "This will move the latest switch ({}) from {} ({}) to {} ({}). Is this OK?".format(members, last_absolute,
 | 
			
		||||
                                                                                                last_relative,
 | 
			
		||||
                                                                                                new_absolute,
 | 
			
		||||
                                                                                                new_relative))
 | 
			
		||||
 | 
			
		||||
        # Await and handle confirmation reactions
 | 
			
		||||
        confirm_msg = await ctx.reply(embed=embed)
 | 
			
		||||
        await ctx.client.add_reaction(confirm_msg, "✅")
 | 
			
		||||
        await ctx.client.add_reaction(confirm_msg, "❌")
 | 
			
		||||
 | 
			
		||||
        reaction = await ctx.client.wait_for_reaction(emoji=["✅", "❌"], message=confirm_msg, user=ctx.message.author,
 | 
			
		||||
                                                      timeout=60.0 * 5)
 | 
			
		||||
        if not reaction:
 | 
			
		||||
            return CommandError("Switch move timed out.")
 | 
			
		||||
 | 
			
		||||
        if reaction.reaction.emoji == "❌":
 | 
			
		||||
        # Confirm with user
 | 
			
		||||
        switch_confirm_message = "This will move the latest switch ({}) from {} ({}) to {} ({}). Is this OK?".format(members, last_absolute, last_relative, new_absolute, new_relative)
 | 
			
		||||
        if not await ctx.confirm_react(ctx.message.author, switch_confirm_message):
 | 
			
		||||
            return CommandError("Switch move cancelled.")
 | 
			
		||||
 | 
			
		||||
        # DB requires the actual switch ID which our utility method above doesn't return, do this manually
 | 
			
		||||
@@ -128,3 +115,6 @@ async def switch_move(ctx: CommandContext):
 | 
			
		||||
        # Change the switch in the DB
 | 
			
		||||
        await db.move_last_switch(ctx.conn, system.id, switch_id, new_time)
 | 
			
		||||
        return CommandSuccess("Switch moved.")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -208,17 +208,13 @@ async def system_fronthistory(ctx: CommandContext):
 | 
			
		||||
async def system_delete(ctx: CommandContext):
 | 
			
		||||
    system = await ctx.ensure_system()
 | 
			
		||||
 | 
			
		||||
    await ctx.reply(
 | 
			
		||||
        "Are you sure you want to delete your system? If so, reply to this message with the system's ID (`{}`).".format(
 | 
			
		||||
            system.hid))
 | 
			
		||||
 | 
			
		||||
    msg = await ctx.client.wait_for_message(author=ctx.message.author, channel=ctx.message.channel, timeout=60.0 * 5)
 | 
			
		||||
    if msg and msg.content.lower() == system.hid.lower():
 | 
			
		||||
        await db.remove_system(ctx.conn, system_id=system.id)
 | 
			
		||||
        return CommandSuccess("System deleted.")
 | 
			
		||||
    else:
 | 
			
		||||
    delete_confirm_msg = "Are you sure you want to delete your system? If so, reply to this message with the system's ID (`{}`).".format(system.hid)
 | 
			
		||||
    if not await ctx.confirm_text(ctx.message.author, ctx.message.channel, system.hid, delete_confirm_msg):
 | 
			
		||||
        return CommandError("System deletion cancelled.")
 | 
			
		||||
 | 
			
		||||
    await db.remove_system(ctx.conn, system_id=system.id)
 | 
			
		||||
    return CommandSuccess("System deleted.")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def system_frontpercent(ctx: CommandContext):
 | 
			
		||||
    system = await ctx.ensure_system()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user