Welcome to blurple.py!¶
Blurple.py is a framework built on top of discord.py, giving you the tools you need to build discord bots with convenience.
Theme provided by discord.py
The API Reference is outlined below.
blurple.ui¶
All the styled components you’ll ever need for building user interfaces in discord.
Style¶
-
class
blurple.ui.
Style
¶ A contextual class to style components.
There are 9 main styles.
PRIMARY
SECONDARY
SUCCESS
DANGER
WARNING
INFO
LIGHT
DARK
GHOST
You can also create a custom style:
- Example Usage
grape_style = (0x9266CC, "\U0001f347", "Grape")
PRIMARY
andSECONDARY
styles use custom emoji, so are unable to be used out of the box. To work around this, I’ve provided the source .svgs in the repository for the custom emojis used throughout the project. you can add these to a server that your bot is in, then create a custom style.Alternatively, if you want, you can support me on ko-fi, and I’ll invite your bot to my server with the original custom emojis.
Alert¶
-
class
blurple.ui.
Alert
(style: blurple.ui.style.Style, title: str, description: str = None, **options)¶ A subclass of
discord.Embed
for stylish alert messages.- Parameters
style (Style) – The style of the alert.
title (str) – The title of the alert, will be wrapped in emoji and alert name unless specified in options.
description (str) – An optional description of the alert, use your imagination for it’s use.
**options –
Alert options to customize it’s look.
- emoji
Defaults to
True
. Can be set to false to remove the emoji from the alert title. This will automatically be removed if a custom style specifies it as an empty string.- name
Defaults to
True
. Can be set to false to remove the name of the alert from the title. This will automatically be removed if a custom style specifies it as an empty string.
Toast¶
-
class
blurple.ui.
Toast
(style: blurple.ui.style.Style, text: str, **options)¶ A subclass of
discord.Embed
for stylish toasts.The difference between this class and
Alert
is these are intended to hold more deemphasized information. Some functionality is implemented with :module:`blurple.io`, such as the “dismiss” button, and automatic dismissal after the specified duration.- Parameters
style (Style) – The style of the toasts.
text (str) – The text of the toasts, will be wrapped in an emoji unless specified in options.
**options –
Toast options to customize it’s look.
- emoji
Defaults to
True
. Can be set to false to remove the emoji from the toast.
-
async
send
(client: discord.abc.Messageable, duration: int = 5)¶ Send the toast out.
- Parameters
client – The client used, usually a
discord.abc.Messageable
. Must have implementedsend()
duration (int) – The duration the toast lasts before disappearing in seconds
blurple.io¶
Robust functions that enable you to build stable, multi-step commands with ease.
Reply¶
-
class
blurple.io.
Reply
(ctx: discord.ext.commands.context.Context, *, validate: Union[str, Callable, List, None] = None, on_error: Union[str, discord.embeds.Embed] = <blurple.ui.alert.Alert object>, timeout=180, **kwargs)¶ An abstract class for getting replies, to be extended.
If you are trying to get a reply from the user directly, you may be looking for
MessageReply
orReactionAddReply
.- Extending this class:
In order to extend this class, there are 5 methods you can specialize.
on_reply_init()
Use this method to initialize variables at the start.on_pre_reply()
Use this method to prepare anything before reply attempts.reply_check()
This is required. Evaluate whether an event call is considered a user reply attempt.on_reply_attempt()
Use this method to handle resetting the state after a reply attempt.on_reply_complete()
Use this method to handle final cleanup.
- Parameters
ctx – The
Context
variablevalidate –
An optional parameter to validate the reply.
If left blank, no validation will be performed.
If you pass a
list
/set
, validation will succeed when the reply content is found inside the list/set.If you pass a
str
, validation will succeed when the reply content matches the string as a regex.If you pass a
function
orcoroutine
, the function will be called, and the coroutine awaited, validation will succeed when the function returns a Truthy value. The reply object will be passed as a parameter.
on_error – An optional parameter specifying the message to send when the user fails validation, defaults to a simple “Invalid Reply”
Alert
.timeout – An optional parameter to change the default timeout length, defaults to 180 seconds.
**kwargs –
Any additional parameters that will be passed to
on_reply_init()
. This can be useful if you are extending this class and need to take extra parameters when initializing.
-
async
result
()¶ Await the result of the reply.
-
async classmethod
result_between
(replies: Container[blurple.io.reply.Reply]) → Tuple[blurple.io.reply.Reply, Any]¶ Return the first completed result between multiple reply objects.
- Parameters
replies – A collection of Reply objects.
- Returns
A tuple containing the Reply object and the result it returned.
- How to use this
This can be an especially powerful function if used correctly. Here’s an example of an rsvp list interaction with reactions using this function.
This is completely contrived for example and not a practical use.
rsvp_react = "📝" # Replace this with whatever you want rsvp_list = [] # Start the reply wait message = await ctx.send("React to RSVP!") await message.add_reaction(rsvp_react) add = io.ReactionAddBasic(message, validate=[rsvp_react]) remove = io.ReactionRemoveBasic(message, validate=[rsvp_react]) while True: obj, result = await io.Reply.result_between({add, remove}) if obj is add: rsvp_list.append(result.user_id) elif obj is remove: rsvp_list.remove(result.user_id) else: # obj is None (The reply timed out) break # Reply wait complete await message.clear_reactions() await message.edit(f"Here's the list of RSVPrs:\n{'\n'.join([f'> <@{user_id}>' for user_id in rsvp_list])}")
-
async
on_reply_init
()¶ An abstract method, to be extended in custom Reply listeners.
This method runs when the Reply class is created.
-
async
on_pre_reply
()¶ An abstract method, to be extended in custom Reply listeners.
This method runs before each reply attempt, can run multiple times with validation.
-
reply_check
(reply)¶ An abstract method, to be extended in custom Reply listeners.
This method runs as a check to determine whether to recognize the reply event, can run multiple times with validation.
-
async
on_reply_attempt
(reply)¶ An abstract method, to be extended in custom Reply listeners.
This method runs after each reply attempt, can run multiple times with validation.
- Returns
You can optionally return a parsed version of the reply to be used instead of the raw reply object.
-
async
on_reply_complete
()¶ An abstract method, to be extended in custom Reply listeners.
This method runs after a valid reply is returned.
MessageBasic¶
-
class
blurple.io.
MessageBasic
(ctx: discord.ext.commands.context.Context, *, validate: Union[str, Callable, List, None] = None, on_error: Union[str, discord.embeds.Embed] = <blurple.ui.alert.Alert object>, timeout=180, **kwargs)¶ An unopinionated, lower level class to wait for a user to send a message.
-
reply_check
(reply: discord.message.Message)¶ Specialized to check if the message is in the same channel, and is by the same author.
-
MessageReply¶
-
class
blurple.io.
MessageReply
(ctx: discord.ext.commands.context.Context, *, validate: Union[str, Callable, List, None] = None, on_error: Union[str, discord.embeds.Embed] = <blurple.ui.alert.Alert object>, timeout=180, **kwargs)¶ Wait for a user to send a reply.
- Example Usage
reply = await io.MessageReply(ctx, validate=["yes", "no"]).result()
-
async
on_reply_attempt
(reply: discord.message.Message)¶ Specialized to delete the reply on attempt.
ReactionAddBasic¶
-
class
blurple.io.
ReactionAddBasic
(ctx: discord.ext.commands.context.Context, *, validate: Union[str, Callable, List, None] = None, on_error: Union[str, discord.embeds.Embed] = <blurple.ui.alert.Alert object>, timeout=180, **kwargs)¶ An unopinionated, lower level class to wait for a user to add a reaction.
-
async
on_reply_init
(message: discord.message.Message)¶ Sepcialized to pass message object.
-
reply_check
(payload: discord.raw_models.RawReactionActionEvent)¶ Specialized to check if the reaction and payload message is valid.
-
async
ReactionRemoveBasic¶
-
class
blurple.io.
ReactionRemoveBasic
(ctx: discord.ext.commands.context.Context, *, validate: Union[str, Callable, List, None] = None, on_error: Union[str, discord.embeds.Embed] = <blurple.ui.alert.Alert object>, timeout=180, **kwargs)¶ An unopinionated, lower level class to wait for a user to remove a reaction.
ReactionAddReply¶
-
class
blurple.io.
ReactionAddReply
(ctx: discord.ext.commands.context.Context, *, validate: Union[str, Callable, List, None] = None, on_error: Union[str, discord.embeds.Embed] = <blurple.ui.alert.Alert object>, timeout=180, **kwargs)¶ Ask for the user’s reaction reply.
- Example Usage
reply = await io.ReactionAddReply(ctx, validate=["✅", "❎"]).result()
-
async
on_reply_init
(message: discord.message.Message)¶ Specialized to add vaild reaction emojis to message, if validation is on.
-
reply_check
(payload: discord.raw_models.RawReactionActionEvent)¶ Specialized to check if payload user and message are valid.
-
async
on_reply_attempt
(payload: discord.raw_models.RawReactionActionEvent)¶ Specialized to remove the user’s reaction.
-
async
on_reply_complete
()¶ Specialized to clear all reactions off the message.
blurple.ext¶
Utilities and sane defaults for discord.ext commands.
Router¶
-
class
blurple.ext.
Router
(bot: discord.ext.commands.bot.Bot)¶ Create a router, connected to a bot instance to allow route-based command registry.
- Example Usage
bot = commands.Bot() router = Router(bot) @router.route(["cmd", "subcmd"]) async def subcmd(ctx): pass
-
route
(command: list, **kwargs)¶ A shortcut decorator that registers a command route.
- Parameters
command (list[str]) – A list of strings defining the route to the command.
**kwargs –
Any command attributes to pass on, such as aliases.
HelpCommand¶
-
class
blurple.ext.
HelpCommand
(embed: discord.embeds.Embed = <class 'discord.embeds.Embed'>, embed_args: dict = {}, **options)¶ A drop-in replacement for the default HelpCommand class.
- Parameters
[embed] (discord.Embed) – Specify a custom
discord.Embed
subclass to use for the help embed, defaults todiscord.Embed
.[embed_args] (dict) – Specify custom arguments to pass to the
discord.Embed
class used for the help embed.
- Example Usage
bot = commands.Bot() bot.help_command = ext.HelpCommand()
DurationConverter¶
-
class
blurple.ext.
DurationConverter
(*args, **kwargs)¶ Converter to process text-based durations of time to datetime.
years: y/Y, yr(s), year(s)
months: m, mon(s), month(s)
weeks: w/W, week(s)
days: d/D, day(s)
hours: h/H, hr(s), hour(s)
minutes: M, min(s), minute(s)
seconds: s/S, sec(s), second(s)
Units must be provided in descending order of magnitude.
- Example Usage
from blurple import ext async def mycommand(ctx, *, duration: ext.DurationConverter):
-
async
convert
(ctx, argument: str)¶ -
The method to override to do conversion logic.
If an error is found while converting, it is recommended to raise a
CommandError
derived exception as it will properly propagate to the error handlers.- ctx:
Context
The invocation context that the argument is being used in.
- argument:
str
The argument that is being converted.
- CommandError
A generic exception occurred when converting the argument.
- BadArgument
The converter failed to convert the argument.
- ctx: