Utils API Reference¶
Pagination¶
- class lightbulb.utils.pag.StringPaginator(*, max_lines: Optional[int] = None, max_chars: int = 2000, prefix: str = '', suffix: str = '', line_separator: str = '\n')¶
Bases:
lightbulb.utils.pag.Paginator
[str
]Creates pages from lines of text according to the given parameters.
Text should be added to the paginator using
add_line()
, which will then be split up into an appropriate number of pages, accessible throughpages
.- Keyword Arguments
max_lines (Optional[
int
]) – The maximum number of lines per page. Defaults toNone
, meaning pages will use themax_chars
param instead.max_chars (
int
) – The maximum number of characters per page. Defaults to2000
, the max character limit for a discord message.prefix (
str
) – The string to prefix every page with. Defaults to an empty string.suffix (
str
) – The string to suffix every page with. Defaults to an empty string.
Example
An example command using pagination to display all the guilds the bot is in.
from lightbulb.utils.pag import StringPaginator @bot.command() async def guilds(ctx): guilds = await bot.rest.fetch_my_guilds() pag = StringPaginator(max_lines=10) for n, guild in enumerate(guilds, start=1): pag.add_line(f"**{n}.** {guild.name}") for page in pag.build_pages(): await ctx.respond(page)
- class lightbulb.utils.pag.EmbedPaginator(*, max_lines: Optional[int] = None, max_chars: int = 2048, prefix: str = '', suffix: str = '', line_separator: str = '\n')¶
Bases:
lightbulb.utils.pag.Paginator
[hikari.embeds.Embed
]Creates embed pages from lines of text according to the given parameters.
Text is added to the paginator the same way as
StringPaginator
. The paginated text will be run though the definedembed_factory()
, or if no embed factory is defined then it will be inserted into the description of a default embed.- Keyword Arguments
max_lines (Optional[
int
]) – The maximum number of lines per page. Defaults toNone
, meaning pages will use themax_chars
param instead.max_chars (
int
) – The maximum number of characters per page. Defaults to2048
, the max character limit for a discord message.prefix (
str
) – The string to prefix every page with. Defaults to an empty string.suffix (
str
) – The string to suffix every page with. Defaults to an empty string.
- embed_factory()¶
A decorator to mark a function as the paginator’s embed factory. The page index and page content will be passed to the function when a new page is to be created.
Example
The following code will give each embed created a random colour.
from random import randint from lightbulb.utils.pag import EmbedPaginator from hikari import Embed pag = EmbedPaginator() @pag.embed_factory() def build_embed(page_index, page_content): return Embed(description=page_content, colour=randint(0, 0xFFFFFF))
See also
- set_embed_factory(func: Callable[[int, str], hikari.embeds.Embed]) None ¶
Method to set a callable as the paginator’s embed factory. Alternative to
embed_factory()
.- Parameters
func (Callable[ [
int
,str
],Embed
]) – The callable to set as the paginator’s embed factory.- Returns
None
See also
- class lightbulb.utils.pag.Paginator(*, max_lines: Optional[int] = None, max_chars: int = 2000, prefix: str = '', suffix: str = '', line_separator: str = '\n', page_factory: Callable[[int, str], lightbulb.utils.pag.T] = <function Paginator.<lambda>>)¶
Bases:
abc.ABC
,Generic
[lightbulb.utils.pag.T
]- build_pages(page_number_start: int = 1) Iterator[lightbulb.utils.pag.T] ¶
The current pages that have been created.
- Parameters
page_number_start (
int
) – The page number to start at. Defaults to1
.- Returns
Lazy generator of each page.
- Return type
Iterator[ T ]
- add_line(line: Any) None ¶
Add a line to the paginator.
- Parameters
line (
typing.Any
) – The line to add to the paginator. Will be converted to astr
.- Returns
None
Miscellaneous¶
- lightbulb.utils.find(sequence: Iterable[lightbulb.utils.search.T], predicate: Callable[[lightbulb.utils.search.T], bool]) Optional[lightbulb.utils.search.T] ¶
Find the first item from an iterable that passes for the predicate specified, or return
None
if no matching item was found.- Parameters
sequence (Iterable[ T ]) – Iterable to search through.
predicate (Callable[ [ T ],
bool
]) – Function to evaluate if the item is the correct one or not. It should return a boolean or boolean-like result.
Example
Searching for a member with a specific username in the bot’s cached members.
members = bot.get_members_view_for_guild(ctx.guild_id) member = lightbulb.utils.find(members.values(), lambda m: m.username == "foo")
See also
- lightbulb.utils.get(sequence: Iterable[lightbulb.utils.search.T], **attrs) Optional[lightbulb.utils.search.T] ¶
Get the first item from an iterable that matches all the parameters specified, or return
None
if no matching item was found.- Parameters
sequence (Iterable[ T ]) – Iterable to search through.
- Keyword Arguments
**attrs – Attributes to match.
Example
Searching for a member with a specific username in the bot’s cached members.
members = bot.get_members_view_for_guild(ctx.guild_id) member = lightbulb.utils.get(members.values(), username="foo")
See also
- lightbulb.utils.permissions_for(member: hikari.guilds.Member) hikari.permissions.Permissions ¶
Get the guild permissions for the given member.
- Parameters
member (
hikari.Member
) – Member to get permissions for.- Returns
Member’s guild permissions.
- Return type
hikari.Permissions
Warning
This method relies on the cache to work. If the cache is not available then
hikari.Permissions.NONE
will be returned.
- lightbulb.utils.permissions_in(channel: hikari.channels.GuildChannel, member: hikari.guilds.Member, include_guild_permissions: bool = True) hikari.permissions.Permissions ¶
Get the permissions for the given member in the given guild channel.
- Parameters
channel (
hikari.GuildChannel
) – Channel to get the permissions in.member (
hikari.Member
) – Member to get the permissions for.include_guild_permissions (
bool
) – Whether or not to include the member’s guild permissions. IfFalse
, only permissions granted by overwrites will be included. Defaults toTrue
.
- Returns
Member’s permissions in the given channel.
- Return type
hikari.Permissions