Handle potential permissions error

If we don't have `Manage messages` then trying to delete a user message by reaction will fail.
When `delete_by_reaction()` is called, we give up if we get Forbidden, the user can delete their own message if they want.
This commit is contained in:
xBelladonna 2019-06-15 16:56:25 +09:30
parent 39cb185cf3
commit 21af55af70
3 changed files with 38 additions and 9 deletions

View file

@ -52,7 +52,12 @@ async def member_info(ctx: CommandContext, member: Member):
msg = 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): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
# This will fail if we don't have Manage messages, just silently fail tbh
# The user can delete their own command if they want
pass
async def new_member(ctx: CommandContext): async def new_member(ctx: CommandContext):

View file

@ -18,4 +18,7 @@ async def message_info(ctx: CommandContext):
msg = 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): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
pass

View file

@ -76,7 +76,10 @@ async def system_info(ctx: CommandContext, system: System):
if await ctx.delete_by_react(ctx.message.author, msg): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
pass
async def system_new(ctx: CommandContext): async def system_new(ctx: CommandContext):
new_name = ctx.remaining() or None new_name = ctx.remaining() or None
@ -265,7 +268,10 @@ async def system_fronthistory(ctx: CommandContext, system: System):
if await ctx.delete_by_react(ctx.message.author, msg): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
pass
async def system_delete(ctx: CommandContext): async def system_delete(ctx: CommandContext):
@ -366,7 +372,10 @@ async def system_frontpercent(ctx: CommandContext, system: System):
if await ctx.delete_by_react(ctx.message.author, msg): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
pass
async def system_list(ctx: CommandContext, system: System): async def system_list(ctx: CommandContext, system: System):
# TODO: refactor this # TODO: refactor this
@ -379,7 +388,10 @@ async def system_list(ctx: CommandContext, system: System):
msg = 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): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
pass
else: else:
current_page = 0 current_page = 0
msg: discord.Message = None msg: discord.Message = None
@ -394,7 +406,10 @@ async def system_list(ctx: CommandContext, system: System):
await msg.add_reaction("\u27A1") await msg.add_reaction("\u27A1")
if await ctx.delete_by_react(ctx.message.author, msg): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
pass
else: else:
await msg.edit(embed=embed) await msg.edit(embed=embed)
@ -426,7 +441,10 @@ async def system_list(ctx: CommandContext, system: System):
msg = 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): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
pass
else: else:
current_page = 0 current_page = 0
msg: discord.Message = None msg: discord.Message = None
@ -440,7 +458,10 @@ async def system_list(ctx: CommandContext, system: System):
await msg.add_reaction("\u27A1") await msg.add_reaction("\u27A1")
if await ctx.delete_by_react(ctx.message.author, msg): if await ctx.delete_by_react(ctx.message.author, msg):
await msg.delete() await msg.delete()
try:
await ctx.message.delete() await ctx.message.delete()
except discord.Forbidden:
pass
else: else:
await msg.edit(embed=embed) await msg.edit(embed=embed)