[Utils] Exit menu silently when message is deleted (#2344)

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>
This commit is contained in:
Toby Harradine 2019-01-10 11:33:38 +11:00 committed by GitHub
parent dde5582669
commit aac1460240
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -73,10 +73,13 @@ async def menu(
# noinspection PyAsyncCall # noinspection PyAsyncCall
start_adding_reactions(message, controls.keys(), ctx.bot.loop) start_adding_reactions(message, controls.keys(), ctx.bot.loop)
else: else:
try:
if isinstance(current_page, discord.Embed): if isinstance(current_page, discord.Embed):
await message.edit(embed=current_page) await message.edit(embed=current_page)
else: else:
await message.edit(content=current_page) await message.edit(content=current_page)
except discord.NotFound:
return
try: try:
react, user = await ctx.bot.wait_for( react, user = await ctx.bot.wait_for(
@ -90,9 +93,12 @@ async def menu(
except discord.Forbidden: # cannot remove all reactions except discord.Forbidden: # cannot remove all reactions
for key in controls.keys(): for key in controls.keys():
await message.remove_reaction(key, ctx.bot.user) await message.remove_reaction(key, ctx.bot.user)
return None except discord.NotFound:
return
return await controls[react.emoji](ctx, pages, controls, message, page, timeout, react.emoji) else:
return await controls[react.emoji](
ctx, pages, controls, message, page, timeout, react.emoji
)
async def next_page( async def next_page(
@ -106,10 +112,8 @@ async def next_page(
): ):
perms = message.channel.permissions_for(ctx.me) perms = message.channel.permissions_for(ctx.me)
if perms.manage_messages: # Can manage messages, so remove react if perms.manage_messages: # Can manage messages, so remove react
try: with contextlib.suppress(discord.NotFound):
await message.remove_reaction(emoji, ctx.author) await message.remove_reaction(emoji, ctx.author)
except discord.NotFound:
pass
if page == len(pages) - 1: if page == len(pages) - 1:
page = 0 # Loop around to the first item page = 0 # Loop around to the first item
else: else:
@ -128,10 +132,8 @@ async def prev_page(
): ):
perms = message.channel.permissions_for(ctx.me) perms = message.channel.permissions_for(ctx.me)
if perms.manage_messages: # Can manage messages, so remove react if perms.manage_messages: # Can manage messages, so remove react
try: with contextlib.suppress(discord.NotFound):
await message.remove_reaction(emoji, ctx.author) await message.remove_reaction(emoji, ctx.author)
except discord.NotFound:
pass
if page == 0: if page == 0:
page = len(pages) - 1 # Loop around to the last item page = len(pages) - 1 # Loop around to the last item
else: else:
@ -148,9 +150,8 @@ async def close_menu(
timeout: float, timeout: float,
emoji: str, emoji: str,
): ):
if message: with contextlib.suppress(discord.NotFound):
await message.delete() await message.delete()
return None
def start_adding_reactions( def start_adding_reactions(
@ -161,7 +162,7 @@ def start_adding_reactions(
"""Start adding reactions to a message. """Start adding reactions to a message.
This is a non-blocking operation - calling this will schedule the This is a non-blocking operation - calling this will schedule the
reactions being added, but will the calling code will continue to reactions being added, but the calling code will continue to
execute asynchronously. There is no need to await this function. execute asynchronously. There is no need to await this function.
This is particularly useful if you wish to start waiting for a This is particularly useful if you wish to start waiting for a
@ -169,7 +170,7 @@ def start_adding_reactions(
this is exactly what `menu` uses to do that. this is exactly what `menu` uses to do that.
This spawns a `asyncio.Task` object and schedules it on ``loop``. This spawns a `asyncio.Task` object and schedules it on ``loop``.
If ``loop`` omitted, the loop will be retreived with If ``loop`` omitted, the loop will be retrieved with
`asyncio.get_event_loop`. `asyncio.get_event_loop`.
Parameters Parameters