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