-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[V3 Utils] Menu system #1566
Merged
Kowlin
merged 10 commits into
Cog-Creators:V3/develop
from
palmtree5:V3/enhancement/menu_system
May 4, 2018
Merged
[V3 Utils] Menu system #1566
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1527d77
[V3 Utils] start on a menu system
palmtree5 5fa315a
Fix conflicting names
palmtree5 ead9bfb
Merge branch 'V3/develop' into V3/enhancement/menu_system
palmtree5 355caae
[V3 Menus] change order of default controls
palmtree5 ae04545
[V3 Menus] add a message check to the react check
palmtree5 40147aa
Merge branch 'V3/enhancement/menu_system' of github.com:palmtree5/Red…
palmtree5 d2671dd
Add a note about original source and who ported
palmtree5 19d688b
Compare message ids, not the objects themselves
palmtree5 914790b
Merge branch 'V3/develop' into V3/enhancement/menu_system
palmtree5 2653794
Merge branch 'V3/develop' into V3/enhancement/menu_system
Kowlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
from typing import Union, List | ||
import asyncio | ||
import discord | ||
|
||
from redbot.core import RedContext | ||
|
||
|
||
async def menu(ctx: RedContext, pages: list, | ||
controls: dict, | ||
message: discord.Message=None, page: int=0, | ||
timeout: float=30.0): | ||
""" | ||
An emoji-based menu | ||
|
||
.. note:: All pages should be of the same type | ||
|
||
.. note:: All functions for handling what a particular emoji does | ||
should be coroutines (i.e. :code:`async def`). Additionally, | ||
they must take all of the parameters of this function, in | ||
addition to a string representing the emoji reacted with. | ||
This parameter should be the last one, and none of the | ||
parameters in the handling functions are optional | ||
|
||
Parameters | ||
---------- | ||
ctx: RedContext | ||
The command context | ||
pages: `list` of `str` or `discord.Embed` | ||
The pages of the menu. | ||
controls: dict | ||
A mapping of emoji to the function which handles the action for the | ||
emoji. | ||
message: discord.Message | ||
The message representing the menu. Usually :code:`None` when first opening | ||
the menu | ||
page: int | ||
The current page number of the menu | ||
timeout: float | ||
The time (in seconds) to wait for a reaction | ||
|
||
Raises | ||
------ | ||
RuntimeError | ||
If either of the notes above are violated | ||
""" | ||
if not all(isinstance(x, discord.Embed) for x in pages) and\ | ||
not all(isinstance(x, str) for x in pages): | ||
raise RuntimeError("All pages must be of the same type") | ||
for key, value in controls.items(): | ||
if not asyncio.iscoroutinefunction(value): | ||
raise RuntimeError("Function must be a coroutine") | ||
current_page = pages[page] | ||
|
||
if not message: | ||
if isinstance(current_page, discord.Embed): | ||
message = await ctx.send(embed=current_page) | ||
else: | ||
message = await ctx.send(current_page) | ||
for key in controls.keys(): | ||
await message.add_reaction(key) | ||
else: | ||
if isinstance(current_page, discord.Embed): | ||
await message.edit(embed=current_page) | ||
else: | ||
await message.edit(content=current_page) | ||
|
||
def react_check(r, u): | ||
return u == ctx.author and str(r.emoji) in controls.keys() | ||
|
||
try: | ||
react, user = await ctx.bot.wait_for( | ||
"reaction_add", | ||
check=react_check, | ||
timeout=timeout | ||
) | ||
except asyncio.TimeoutError: | ||
try: | ||
await message.clear_reactions() | ||
except discord.Forbidden: # cannot remove all reactions | ||
for key in controls.keys(): | ||
await message.remove_reaction(key, ctx.bot.user) | ||
return None | ||
|
||
return await controls[react.emoji](ctx, pages, controls, | ||
message, page, | ||
timeout, react.emoji) | ||
|
||
|
||
async def next_page(ctx: RedContext, pages: list, | ||
controls: dict, message: discord.Message, page: int, | ||
timeout: float, emoji: str): | ||
perms = message.channel.permissions_for(ctx.guild.me) | ||
if perms.manage_messages: # Can manage messages, so remove react | ||
try: | ||
await message.remove_reaction(emoji, ctx.author) | ||
except discord.NotFound: | ||
pass | ||
if page == len(pages) - 1: | ||
page = 0 # Loop around to the first item | ||
else: | ||
page = page + 1 | ||
return await menu(ctx, pages, controls, message=message, | ||
page=page, timeout=timeout) | ||
|
||
|
||
async def prev_page(ctx: RedContext, pages: list, | ||
controls: dict, message: discord.Message, page: int, | ||
timeout: float, emoji: str): | ||
perms = message.channel.permissions_for(ctx.guild.me) | ||
if perms.manage_messages: # Can manage messages, so remove react | ||
try: | ||
await message.remove_reaction(emoji, ctx.author) | ||
except discord.NotFound: | ||
pass | ||
if page == 0: | ||
next_page = len(pages) - 1 # Loop around to the last item | ||
else: | ||
next_page = page - 1 | ||
return await menu(ctx, pages, controls, message=message, | ||
page=next_page, timeout=timeout) | ||
|
||
|
||
async def close_menu(ctx: RedContext, pages: list, | ||
controls: dict, message: discord.Message, page: int, | ||
timeout: float, emoji: str): | ||
if message: | ||
await message.delete() | ||
return None | ||
|
||
|
||
DEFAULT_CONTROLS = { | ||
"➡": next_page, | ||
"⬅": prev_page, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Swap order of |
||
"❌": close_menu, | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add
and r.message==message