1
0
Fork 0
mirror of synced 2024-05-17 19:12:33 +12:00
Bonfire/utils/paginator.py

570 lines
18 KiB
Python
Raw Normal View History

2017-02-12 12:14:31 +13:00
import asyncio
import discord
2018-10-05 08:14:09 +13:00
import itertools
import inspect
import re
2017-02-12 12:14:31 +13:00
2017-03-08 11:35:30 +13:00
2017-02-12 12:14:31 +13:00
class CannotPaginate(Exception):
pass
2017-03-08 11:35:30 +13:00
2017-02-12 12:14:31 +13:00
class Pages:
"""Implements a paginator that queries the user for the
pagination interface.
Pages are 1-index based, not 0-index based.
2018-10-05 08:14:09 +13:00
If the user does not reply within 2 minutes then the pagination
2017-02-12 12:14:31 +13:00
interface exits automatically.
2018-10-05 08:14:09 +13:00
Parameters
------------
ctx: Context
The context of the command.
entries: List[str]
A list of entries to paginate.
per_page: int
How many entries show up per page.
show_entry_count: bool
Whether to show an entry count in the footer.
Attributes
-----------
embed: discord.Embed
The embed object that is being used to send pagination info.
Feel free to modify this externally. Only the description,
footer fields, and colour are internally modified.
permissions: discord.Permissions
Our permissions for the channel.
2017-02-12 12:14:31 +13:00
"""
2017-03-08 11:35:30 +13:00
2018-10-05 08:14:09 +13:00
def __init__(self, ctx, *, entries, per_page=12, show_entry_count=True):
self.bot = ctx.bot
2017-02-12 12:14:31 +13:00
self.entries = entries
2018-10-05 08:14:09 +13:00
self.message = ctx.message
self.channel = ctx.channel
self.author = ctx.author
2017-02-12 12:14:31 +13:00
self.per_page = per_page
pages, left_over = divmod(len(self.entries), self.per_page)
if left_over:
pages += 1
self.maximum_pages = pages
2018-10-05 08:14:09 +13:00
self.embed = discord.Embed(colour=discord.Colour.blurple())
2017-02-12 12:14:31 +13:00
self.paginating = len(entries) > per_page
2018-10-05 08:14:09 +13:00
self.show_entry_count = show_entry_count
2017-02-12 12:14:31 +13:00
self.reaction_emojis = [
2020-11-20 09:58:41 +13:00
(
"\N{BLACK LEFT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}",
self.first_page,
),
("\N{BLACK LEFT-POINTING TRIANGLE}", self.previous_page),
("\N{BLACK RIGHT-POINTING TRIANGLE}", self.next_page),
(
"\N{BLACK RIGHT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}",
self.last_page,
),
("\N{INPUT SYMBOL FOR NUMBERS}", self.numbered_page),
("\N{BLACK SQUARE FOR STOP}", self.stop_pages),
("\N{INFORMATION SOURCE}", self.show_help),
2017-02-12 12:14:31 +13:00
]
2018-10-05 08:14:09 +13:00
if ctx.guild is not None:
self.permissions = self.channel.permissions_for(ctx.guild.me)
2017-02-12 12:14:31 +13:00
else:
2018-10-05 08:14:09 +13:00
self.permissions = self.channel.permissions_for(ctx.bot.user)
2017-02-12 12:14:31 +13:00
if not self.permissions.embed_links:
2020-11-20 09:58:41 +13:00
raise CannotPaginate("Bot does not have embed links permission.")
2017-02-12 12:14:31 +13:00
2018-10-05 08:14:09 +13:00
if not self.permissions.send_messages:
2020-11-20 09:58:41 +13:00
raise CannotPaginate("Bot cannot send messages.")
2018-10-05 08:14:09 +13:00
if self.paginating:
# verify we can actually use the pagination session
if not self.permissions.add_reactions:
2020-11-20 09:58:41 +13:00
raise CannotPaginate("Bot does not have add reactions permission.")
2018-10-05 08:14:09 +13:00
if not self.permissions.read_message_history:
2020-11-20 09:58:41 +13:00
raise CannotPaginate(
"Bot does not have Read Message History permission."
)
2018-10-05 08:14:09 +13:00
2017-02-12 12:14:31 +13:00
def get_page(self, page):
base = (page - 1) * self.per_page
2020-11-20 09:58:41 +13:00
return self.entries[base : base + self.per_page]
2017-02-12 12:14:31 +13:00
async def show_page(self, page, *, first=False):
self.current_page = page
entries = self.get_page(page)
p = []
2018-10-05 08:14:09 +13:00
for index, entry in enumerate(entries, 1 + ((page - 1) * self.per_page)):
2020-11-20 09:58:41 +13:00
p.append(f"{index}. {entry}")
2018-10-05 08:14:09 +13:00
if self.maximum_pages > 1:
if self.show_entry_count:
2020-11-20 09:58:41 +13:00
text = f"Page {page}/{self.maximum_pages} ({len(self.entries)} entries)"
2018-10-05 08:14:09 +13:00
else:
2020-11-20 09:58:41 +13:00
text = f"Page {page}/{self.maximum_pages}"
2017-02-12 12:14:31 +13:00
2018-10-05 08:14:09 +13:00
self.embed.set_footer(text=text)
2017-02-12 12:14:31 +13:00
if not self.paginating:
2020-11-20 09:58:41 +13:00
self.embed.description = "\n".join(p)
2018-10-05 08:14:09 +13:00
return await self.channel.send(embed=self.embed)
2017-02-12 12:14:31 +13:00
if not first:
2020-11-20 09:58:41 +13:00
self.embed.description = "\n".join(p)
2018-10-05 08:14:09 +13:00
await self.message.edit(embed=self.embed)
2017-02-12 12:14:31 +13:00
return
2020-11-20 09:58:41 +13:00
p.append("")
p.append("Confused? React with \N{INFORMATION SOURCE} for more info.")
self.embed.description = "\n".join(p)
2018-10-05 08:14:09 +13:00
self.message = await self.channel.send(embed=self.embed)
2017-02-12 12:14:31 +13:00
for (reaction, _) in self.reaction_emojis:
2020-11-20 09:58:41 +13:00
if self.maximum_pages == 2 and reaction in ("\u23ed", "\u23ee"):
2017-02-12 12:14:31 +13:00
# no |<< or >>| buttons if we only have two pages
# we can't forbid it if someone ends up using it but remove
# it from the default set
continue
2018-10-05 08:14:09 +13:00
await self.message.add_reaction(reaction)
2017-02-12 12:14:31 +13:00
async def checked_show_page(self, page):
if page != 0 and page <= self.maximum_pages:
await self.show_page(page)
async def first_page(self):
"""goes to the first page"""
await self.show_page(1)
async def last_page(self):
"""goes to the last page"""
await self.show_page(self.maximum_pages)
async def next_page(self):
"""goes to the next page"""
await self.checked_show_page(self.current_page + 1)
async def previous_page(self):
"""goes to the previous page"""
await self.checked_show_page(self.current_page - 1)
async def show_current_page(self):
if self.paginating:
await self.show_page(self.current_page)
async def numbered_page(self):
"""lets you type a page number to go to"""
2018-10-05 08:14:09 +13:00
to_delete = []
2020-11-20 09:58:41 +13:00
to_delete.append(await self.channel.send("What page do you want to go to?"))
2017-03-08 12:47:00 +13:00
2018-10-05 08:14:09 +13:00
def message_check(m):
2020-11-20 09:58:41 +13:00
return (
m.author == self.author
and self.channel == m.channel
and m.content.isdigit()
)
2017-03-08 12:47:00 +13:00
try:
2020-11-20 09:58:41 +13:00
msg = await self.bot.wait_for("message", check=message_check, timeout=30.0)
2017-03-08 12:47:00 +13:00
except asyncio.TimeoutError:
2020-11-20 09:58:41 +13:00
to_delete.append(await self.channel.send("Took too long."))
2018-10-05 08:14:09 +13:00
await asyncio.sleep(5)
else:
2017-02-12 12:14:31 +13:00
page = int(msg.content)
to_delete.append(msg)
if page != 0 and page <= self.maximum_pages:
await self.show_page(page)
else:
2020-11-20 09:58:41 +13:00
to_delete.append(
await self.channel.send(
f"Invalid page given. ({page}/{self.maximum_pages})"
)
)
2017-02-12 12:14:31 +13:00
await asyncio.sleep(5)
try:
2018-10-05 08:14:09 +13:00
await self.channel.delete_messages(to_delete)
2017-02-12 12:14:31 +13:00
except Exception:
pass
async def show_help(self):
"""shows this message"""
2020-11-20 09:58:41 +13:00
messages = ["Welcome to the interactive paginator!\n"]
messages.append(
"This interactively allows you to see pages of text by navigating with "
"reactions. They are as follows:\n"
)
2017-02-12 12:14:31 +13:00
for (emoji, func) in self.reaction_emojis:
2020-11-20 09:58:41 +13:00
messages.append(f"{emoji} {func.__doc__}")
2017-02-12 12:14:31 +13:00
2020-11-20 09:58:41 +13:00
self.embed.description = "\n".join(messages)
2018-10-05 08:14:09 +13:00
self.embed.clear_fields()
2020-11-20 09:58:41 +13:00
self.embed.set_footer(
text=f"We were on page {self.current_page} before this message."
)
2018-10-05 08:14:09 +13:00
await self.message.edit(embed=self.embed)
2017-02-12 12:14:31 +13:00
async def go_back_to_current_page():
await asyncio.sleep(60.0)
await self.show_current_page()
self.bot.loop.create_task(go_back_to_current_page())
async def stop_pages(self):
"""stops the interactive pagination session"""
2017-03-08 11:35:30 +13:00
await self.message.delete()
2017-02-12 12:14:31 +13:00
self.paginating = False
def react_check(self, reaction, user):
2018-10-05 08:14:09 +13:00
if user is None or user.id != self.author.id:
return False
if reaction.message.id != self.message.id:
2017-02-12 12:14:31 +13:00
return False
for (emoji, func) in self.reaction_emojis:
if reaction.emoji == emoji:
self.match = func
return True
return False
2018-10-05 08:14:09 +13:00
async def paginate(self):
2017-02-12 12:14:31 +13:00
"""Actually paginate the entries and run the interactive loop if necessary."""
2018-10-05 08:14:09 +13:00
first_page = self.show_page(1, first=True)
if not self.paginating:
await first_page
else:
# allow us to react to reactions right away if we're paginating
self.bot.loop.create_task(first_page)
2017-02-12 12:14:31 +13:00
while self.paginating:
2017-03-08 12:47:00 +13:00
try:
2020-11-20 09:58:41 +13:00
reaction, user = await self.bot.wait_for(
"reaction_add", check=self.react_check, timeout=120.0
)
2017-03-08 12:47:00 +13:00
except asyncio.TimeoutError:
2017-02-12 12:14:31 +13:00
self.paginating = False
try:
2017-03-08 11:35:30 +13:00
await self.message.clear_reactions()
2018-10-05 08:14:09 +13:00
except:
2017-02-12 12:14:31 +13:00
pass
finally:
break
try:
2018-10-05 08:14:09 +13:00
await self.message.remove_reaction(reaction, user)
except:
2017-03-08 11:35:30 +13:00
pass # can't remove it so don't bother doing so
2017-02-12 12:14:31 +13:00
await self.match()
2017-03-11 16:05:42 +13:00
2018-09-24 07:23:27 +12:00
2018-10-05 08:14:09 +13:00
class FieldPages(Pages):
"""Similar to Pages except entries should be a list of
tuples having (key, value) to show as embed fields instead.
"""
2020-11-20 09:58:41 +13:00
2017-03-11 16:05:42 +13:00
async def show_page(self, page, *, first=False):
self.current_page = page
entries = self.get_page(page)
self.embed.clear_fields()
2018-10-05 08:14:09 +13:00
self.embed.description = discord.Embed.Empty
2017-03-11 16:05:42 +13:00
2018-10-05 08:14:09 +13:00
for key, value in entries:
self.embed.add_field(name=key, value=value, inline=False)
if self.maximum_pages > 1:
if self.show_entry_count:
2020-11-20 09:58:41 +13:00
text = f"Page {page}/{self.maximum_pages} ({len(self.entries)} entries)"
2017-03-11 16:05:42 +13:00
else:
2020-11-20 09:58:41 +13:00
text = f"Page {page}/{self.maximum_pages}"
2018-10-05 08:14:09 +13:00
self.embed.set_footer(text=text)
2017-03-11 16:05:42 +13:00
if not self.paginating:
2018-10-05 08:14:09 +13:00
return await self.channel.send(embed=self.embed)
2017-03-11 16:05:42 +13:00
if not first:
2018-10-05 08:14:09 +13:00
await self.message.edit(embed=self.embed)
2017-03-11 16:05:42 +13:00
return
2018-10-05 08:14:09 +13:00
self.message = await self.channel.send(embed=self.embed)
for (reaction, _) in self.reaction_emojis:
2020-11-20 09:58:41 +13:00
if self.maximum_pages == 2 and reaction in ("\u23ed", "\u23ee"):
2018-10-05 08:14:09 +13:00
# no |<< or >>| buttons if we only have two pages
# we can't forbid it if someone ends up using it but remove
# it from the default set
continue
await self.message.add_reaction(reaction)
# ?help
# ?help Cog
# ?help command
# -> could be a subcommand
2020-11-20 09:58:41 +13:00
_mention = re.compile(r"<@\!?([0-9]{1,19})>")
2018-10-05 08:14:09 +13:00
def cleanup_prefix(bot, prefix):
m = _mention.match(prefix)
if m:
user = bot.get_user(int(m.group(1)))
if user:
2020-11-20 09:58:41 +13:00
return f"@{user.name} "
2018-10-05 08:14:09 +13:00
return prefix
async def _can_run(cmd, ctx):
try:
return await cmd.can_run(ctx)
except:
return False
def _command_signature(cmd):
# this is modified from discord.py source
result = [cmd.qualified_name]
if cmd.usage:
result.append(cmd.usage)
2020-11-20 09:58:41 +13:00
return " ".join(result)
2017-03-11 16:05:42 +13:00
2018-10-05 08:14:09 +13:00
params = cmd.clean_params
if not params:
2020-11-20 09:58:41 +13:00
return " ".join(result)
2017-03-11 16:05:42 +13:00
2018-10-05 08:14:09 +13:00
for name, param in params.items():
if param.default is not param.empty:
# We don't want None or '' to trigger the [name=value] case and instead it should
# do [name] since [name=None] or [name=] are not exactly useful for the user.
2020-11-20 09:58:41 +13:00
should_print = (
param.default
if isinstance(param.default, str)
else param.default is not None
)
2018-10-05 08:14:09 +13:00
if should_print:
2020-11-20 09:58:41 +13:00
result.append(f"[{name}={param.default!r}]")
2018-10-05 08:14:09 +13:00
else:
2020-11-20 09:58:41 +13:00
result.append(f"[{name}]")
2018-10-05 08:14:09 +13:00
elif param.kind == param.VAR_POSITIONAL:
2020-11-20 09:58:41 +13:00
result.append(f"[{name}...]")
2017-03-11 16:05:42 +13:00
else:
2020-11-20 09:58:41 +13:00
result.append(f"<{name}>")
2018-10-05 08:14:09 +13:00
2020-11-20 09:58:41 +13:00
return " ".join(result)
2018-10-05 08:14:09 +13:00
class HelpPaginator(Pages):
def __init__(self, ctx, entries, *, per_page=4):
super().__init__(ctx, entries=entries, per_page=per_page)
2020-11-20 09:58:41 +13:00
self.reaction_emojis.append(
("\N{WHITE QUESTION MARK ORNAMENT}", self.show_bot_help)
)
2018-10-05 08:14:09 +13:00
self.total = len(entries)
2017-03-11 16:05:42 +13:00
2018-10-05 08:14:09 +13:00
@classmethod
async def from_cog(cls, ctx, cog):
cog_name = cog.__class__.__name__
# get the commands
entries = sorted(ctx.bot.get_cog_commands(cog_name), key=lambda c: c.name)
# remove the ones we can't run
2020-11-20 09:58:41 +13:00
entries = [
cmd for cmd in entries if (await _can_run(cmd, ctx)) and not cmd.hidden
]
2018-10-05 08:14:09 +13:00
self = cls(ctx, entries)
2020-11-20 09:58:41 +13:00
self.title = f"{cog_name} Commands"
2018-10-05 08:14:09 +13:00
self.description = inspect.getdoc(cog)
self.prefix = cleanup_prefix(ctx.bot, ctx.prefix)
return self
@classmethod
async def from_command(cls, ctx, command):
try:
entries = sorted(command.commands, key=lambda c: c.name)
except AttributeError:
entries = []
else:
2020-11-20 09:58:41 +13:00
entries = [
cmd for cmd in entries if (await _can_run(cmd, ctx)) and not cmd.hidden
]
2018-10-05 08:14:09 +13:00
self = cls(ctx, entries)
self.title = command.signature
if command.description:
2020-11-20 09:58:41 +13:00
self.description = f"{command.description}\n\n{command.help}"
2018-10-05 08:14:09 +13:00
else:
2020-11-20 09:58:41 +13:00
self.description = command.help or "No help given."
2018-10-05 08:14:09 +13:00
self.prefix = cleanup_prefix(ctx.bot, ctx.prefix)
return self
@classmethod
async def from_bot(cls, ctx):
def key(c):
2020-11-20 09:58:41 +13:00
return c.cog_name or "\u200bMisc"
2018-10-05 08:14:09 +13:00
entries = sorted(ctx.bot.commands, key=key)
nested_pages = []
per_page = 9
# 0: (cog, desc, commands) (max len == 9)
# 1: (cog, desc, commands) (max len == 9)
# ...
for cog, commands in itertools.groupby(entries, key=key):
2020-11-20 09:58:41 +13:00
plausible = [
cmd for cmd in commands if (await _can_run(cmd, ctx)) and not cmd.hidden
]
2018-10-05 08:14:09 +13:00
if len(plausible) == 0:
continue
description = ctx.bot.get_cog(cog)
if description is None:
description = discord.Embed.Empty
else:
description = inspect.getdoc(description) or discord.Embed.Empty
2020-11-20 09:58:41 +13:00
nested_pages.extend(
(cog, description, plausible[i : i + per_page])
for i in range(0, len(plausible), per_page)
)
2018-10-05 08:14:09 +13:00
self = cls(ctx, nested_pages, per_page=1) # this forces the pagination session
self.prefix = cleanup_prefix(ctx.bot, ctx.prefix)
# swap the get_page implementation with one that supports our style of pagination
self.get_page = self.get_bot_page
self._is_bot = True
# replace the actual total
self.total = sum(len(o) for _, _, o in nested_pages)
return self
def get_bot_page(self, page):
cog, description, commands = self.entries[page - 1]
2020-11-20 09:58:41 +13:00
self.title = f"{cog} Commands"
2018-10-05 08:14:09 +13:00
self.description = description
return commands
async def show_page(self, page, *, first=False):
self.current_page = page
entries = self.get_page(page)
self.embed.clear_fields()
self.embed.description = self.description
self.embed.title = self.title
2020-11-20 09:58:41 +13:00
if hasattr(self, "_is_bot"):
value = "For more help, join the official bot support server: https://discord.gg/f6uzJEj"
self.embed.add_field(name="Support", value=value, inline=False)
2018-10-05 08:14:09 +13:00
2020-11-20 09:58:41 +13:00
self.embed.set_footer(
text=f'Use "{self.prefix}help command" for more info on a command.'
)
2018-10-05 08:14:09 +13:00
signature = _command_signature
for entry in entries:
2020-11-20 09:58:41 +13:00
self.embed.add_field(
name=signature(entry),
value=entry.short_doc or "No help given",
inline=False,
)
2018-10-05 08:14:09 +13:00
if self.maximum_pages:
2020-11-20 09:58:41 +13:00
self.embed.set_author(
name=f"Page {page}/{self.maximum_pages} ({self.total} commands)"
)
2018-10-05 08:14:09 +13:00
if not self.paginating:
return await self.channel.send(embed=self.embed)
if not first:
await self.message.edit(embed=self.embed)
return
self.message = await self.channel.send(embed=self.embed)
2017-03-11 16:05:42 +13:00
for (reaction, _) in self.reaction_emojis:
2020-11-20 09:58:41 +13:00
if self.maximum_pages == 2 and reaction in ("\u23ed", "\u23ee"):
2017-03-11 16:05:42 +13:00
# no |<< or >>| buttons if we only have two pages
# we can't forbid it if someone ends up using it but remove
# it from the default set
continue
2018-10-05 08:14:09 +13:00
await self.message.add_reaction(reaction)
async def show_help(self):
"""shows this message"""
2020-11-20 09:58:41 +13:00
self.embed.title = "Paginator help"
self.embed.description = "Hello! Welcome to the help page."
2018-10-05 08:14:09 +13:00
2020-11-20 09:58:41 +13:00
messages = [f"{emoji} {func.__doc__}" for emoji, func in self.reaction_emojis]
2018-10-05 08:14:09 +13:00
self.embed.clear_fields()
2020-11-20 09:58:41 +13:00
self.embed.add_field(
name="What are these reactions for?",
value="\n".join(messages),
inline=False,
)
2018-10-05 08:14:09 +13:00
2020-11-20 09:58:41 +13:00
self.embed.set_footer(
text=f"We were on page {self.current_page} before this message."
)
2018-10-05 08:14:09 +13:00
await self.message.edit(embed=self.embed)
async def go_back_to_current_page():
await asyncio.sleep(30.0)
await self.show_current_page()
self.bot.loop.create_task(go_back_to_current_page())
async def show_bot_help(self):
"""shows how to use the bot"""
2020-11-20 09:58:41 +13:00
self.embed.title = "Using the bot"
self.embed.description = "Hello! Welcome to the help page."
2018-10-05 08:14:09 +13:00
self.embed.clear_fields()
entries = (
2020-11-20 09:58:41 +13:00
("<argument>", "This means the argument is __**required**__."),
("[argument]", "This means the argument is __**optional**__."),
("[A|B]", "This means the it can be __**either A or B**__."),
(
"[argument...]",
"This means you can have multiple arguments.\n"
"Now that you know the basics, it should be noted that...\n"
"__**You do not type in the brackets!**__",
),
2018-10-05 08:14:09 +13:00
)
2020-11-20 09:58:41 +13:00
self.embed.add_field(
name="How do I use this bot?",
value="Reading the bot signature is pretty simple.",
)
2018-10-05 08:14:09 +13:00
for name, value in entries:
self.embed.add_field(name=name, value=value, inline=False)
2020-11-20 09:58:41 +13:00
self.embed.set_footer(
text=f"We were on page {self.current_page} before this message."
)
2018-10-05 08:14:09 +13:00
await self.message.edit(embed=self.embed)
async def go_back_to_current_page():
await asyncio.sleep(30.0)
await self.show_current_page()
self.bot.loop.create_task(go_back_to_current_page())