From d5e36d7f816cee5a1455799cd4caa5e307e179aa Mon Sep 17 00:00:00 2001 From: xBelladonna Date: Sat, 15 Jun 2019 16:32:38 +0930 Subject: [PATCH] Delete PluralKit posts by reaction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a 🗑 reaction to posts like system cards and member lists that deletes the message when the command author reacts --- src/pluralkit/bot/commands/__init__.py | 10 ++++++ src/pluralkit/bot/commands/member_commands.py | 5 ++- .../bot/commands/message_commands.py | 5 ++- src/pluralkit/bot/commands/system_commands.py | 33 ++++++++++++++++--- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/pluralkit/bot/commands/__init__.py b/src/pluralkit/bot/commands/__init__.py index 53e6d9e8..031d7b83 100644 --- a/src/pluralkit/bot/commands/__init__.py +++ b/src/pluralkit/bot/commands/__init__.py @@ -164,6 +164,16 @@ class CommandContext: except asyncio.TimeoutError: raise CommandError("Timed out - try again.") + async def delete_by_react(self, user: Union[discord.Member, discord.User], message: discord.Message): + try: + await message.add_reaction("🗑") # Wastebasket + reaction, _ = await self.client.wait_for("reaction_add", check=lambda r, u: u.id == user.id and r.emoji in ["🗑"], + timeout=60 * 10) # 10 minute timeout + return reaction.emoji == "🗑" + except asyncio.TimeoutError: + # Delete the reaction if the user doesn't react within the timeout period + await message.remove_reaction("🗑", self.client.user) + import pluralkit.bot.commands.api_commands import pluralkit.bot.commands.import_commands diff --git a/src/pluralkit/bot/commands/member_commands.py b/src/pluralkit/bot/commands/member_commands.py index 5d335e4a..3b0d8ac7 100644 --- a/src/pluralkit/bot/commands/member_commands.py +++ b/src/pluralkit/bot/commands/member_commands.py @@ -51,7 +51,10 @@ async def specific_member_root(ctx: CommandContext): async def member_info(ctx: CommandContext, member: Member): - await ctx.reply(embed=await pluralkit.bot.embeds.member_card(ctx.conn, member)) + msg = await ctx.reply(embed=await pluralkit.bot.embeds.member_card(ctx.conn, member)) + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() async def new_member(ctx: CommandContext): diff --git a/src/pluralkit/bot/commands/message_commands.py b/src/pluralkit/bot/commands/message_commands.py index 2d1e4614..69287c18 100644 --- a/src/pluralkit/bot/commands/message_commands.py +++ b/src/pluralkit/bot/commands/message_commands.py @@ -15,4 +15,7 @@ async def message_info(ctx: CommandContext): raise CommandError( "Message with ID '{}' not found. Are you sure it's a message proxied by PluralKit?".format(mid)) - await ctx.reply(embed=await embeds.message_card(ctx.client, message)) + msg = await ctx.reply(embed=await embeds.message_card(ctx.client, message)) + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() \ No newline at end of file diff --git a/src/pluralkit/bot/commands/system_commands.py b/src/pluralkit/bot/commands/system_commands.py index 80403994..b718c362 100644 --- a/src/pluralkit/bot/commands/system_commands.py +++ b/src/pluralkit/bot/commands/system_commands.py @@ -74,8 +74,11 @@ async def specified_system_root(ctx: CommandContext): async def system_info(ctx: CommandContext, system: System): this_system = await ctx.get_system() - await ctx.reply(embed=await pluralkit.bot.embeds.system_card(ctx.conn, ctx.client, system, this_system and this_system.id == system.id)) + msg = await ctx.reply(embed=await pluralkit.bot.embeds.system_card(ctx.conn, ctx.client, system, this_system and this_system.id == system.id)) + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() async def system_new(ctx: CommandContext): new_name = ctx.remaining() or None @@ -265,7 +268,11 @@ async def system_fronthistory(ctx: CommandContext, system: System): embed = embeds.status("\n".join(lines) or "(none)") embed.title = "Past switches" - await ctx.reply(embed=embed) + msg = await ctx.reply(embed=embed) + + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() async def system_delete(ctx: CommandContext): @@ -362,7 +369,11 @@ async def system_frontpercent(ctx: CommandContext, system: System): embed.set_footer(text="Since {} ({} ago)".format(ctx.format_time(span_start), display_relative(span_start))) - await ctx.reply(embed=embed) + msg = await ctx.reply(embed=embed) + + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() async def system_list(ctx: CommandContext, system: System): # TODO: refactor this @@ -372,7 +383,10 @@ async def system_list(ctx: CommandContext, system: System): page_size = 8 if len(all_members) <= page_size: # If we have less than 8 members, don't bother paginating - await ctx.reply(embed=embeds.member_list_full(system, all_members, 0, page_size)) + msg = await ctx.reply(embed=embeds.member_list_full(system, all_members, 0, page_size)) + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() else: current_page = 0 msg: discord.Message = None @@ -385,6 +399,9 @@ async def system_list(ctx: CommandContext, system: System): msg = await ctx.reply(embed=embed) await msg.add_reaction("\u2B05") await msg.add_reaction("\u27A1") + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() else: await msg.edit(embed=embed) @@ -413,7 +430,10 @@ async def system_list(ctx: CommandContext, system: System): page_size = 25 if len(all_members) <= page_size: # If we have less than 25 members, don't bother paginating - await ctx.reply(embed=embeds.member_list_short(system, all_members, 0, page_size)) + msg = await ctx.reply(embed=embeds.member_list_short(system, all_members, 0, page_size)) + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() else: current_page = 0 msg: discord.Message = None @@ -425,6 +445,9 @@ async def system_list(ctx: CommandContext, system: System): msg = await ctx.reply(embed=embed) await msg.add_reaction("\u2B05") await msg.add_reaction("\u27A1") + if await ctx.delete_by_react(ctx.message.author, msg): + await msg.delete() + await ctx.message.delete() else: await msg.edit(embed=embed)