Utils API Reference#
Data Store#
- class lightbulb.utils.data_store.DataStore[source]#
Data storage class allowing setting, retrieval and unsetting of custom attributes. This class subclasses
dict
so the data can be accessed the same as you would a dictionary as well as using dot notation.Example
>>> d = DataStore() >>> d.foo = "bar" >>> d.foo 'bar' >>> d["foo"] 'bar' >>> d DataStore(foo='bar') >>> d.pop("foo") 'bar' >>> d DataStore()
- get_as(item: str, type: Type[T]) T [source]#
Helper method to allow type-complete getting of items from this
DataStore
.- Parameters:
item (
str
) – The name of the key that the item is stored at.type (Type[T]) – The type to cast the item as.
- Returns:
The item stored at key
item
, cast to the given type.- Return type:
T
- Raises:
ValueError – If a key of name
item
has not been set.
Note
This does not verify types, it just performs a
typing.cast()
to fool the type system into thinking that the return value is of the correct type.New in version 2.2.4.
Pag#
- class lightbulb.utils.pag.EmbedPaginator(*, max_lines: int | None = None, max_chars: int = 2048, prefix: str = '', suffix: str = '', line_separator: str = '\n')[source]#
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() Callable[[Callable[[int, str], Embed]], Callable[[int, str], Embed]] [source]#
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
- class lightbulb.utils.pag.Paginator(*, max_lines: int | None = None, max_chars: int = 2000, prefix: str = '', suffix: str = '', line_separator: str = '\n', page_factory: ~typing.Callable[[int, str], ~lightbulb.utils.pag.T] = <function Paginator.<lambda>>)[source]#
- add_line(line: Any) None [source]#
Add a line to the paginator.
- Parameters:
line (Any) – The line to add to the paginator. Will be converted to a
str
.- Returns:
None
- class lightbulb.utils.pag.StringPaginator(*, max_lines: int | None = None, max_chars: int = 2000, prefix: str = '', suffix: str = '', line_separator: str = '\n')[source]#
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)
Permissions#
- lightbulb.utils.permissions.permissions_for(member: Member) Permissions [source]#
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.permissions_in(channel: PermissibleGuildChannel, member: Member, include_guild_permissions: bool = True) Permissions [source]#
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
Search#
- lightbulb.utils.search.find(sequence: Iterable[T], predicate: Callable[[T], bool]) T | None [source]#
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.search.get(sequence: Iterable[T], **attrs: Any) T | None [source]#
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