Hooks
ballsdex.core.hooks
HOOK_NAMES
HOOK_NAMES: frozenset[str] = frozenset((hook_name) for cls in (SpawnCheckContext, PreCatchContext, CatchRollContext, CatchMessageContext, PreTradeContext, PreGiveContext))
__all__
__all__ = ('HOOK_TIMEOUT', 'HookContext', 'CancellableContext', 'SpawnCheckContext', 'PreCatchContext', 'CatchRollContext', 'CatchMessageContext', 'PreTradeContext', 'PreGiveContext', 'HookRegistry', 'hook')
CancellableContext
Bases: HookContext
A context that can be cancelled to block the action. Once cancelled, the remaining hooks are not called.
Attributes:
CatchMessageContext
CatchMessageContext(*, view: BallSpawnView, interaction: Interaction, player: Player, ball_instance: BallInstance, is_new: bool, content: str)
Bases: HookContext
A countryball was caught, and the catch message is about to be sent.
Attributes:
-
view(BallSpawnView) –The view of the spawn that was caught.
-
interaction(Interaction) –The interaction of the catch prompt.
-
player(Player) –The player who caught.
-
ball_instance(BallInstance) –The countryball now owned by the player.
-
is_new(bool) –Whether this is the first time the player owns this countryball.
-
content(str) –The catch message that will be sent. Editable.
CatchRollContext
CatchRollContext(*, view: BallSpawnView, user: User | Member, player: Player, special: Special | None, attack_bonus: int, health_bonus: int)
Bases: HookContext
A new countryball instance is about to be created after a catch. The rolled values can be modified. Not called when catching a dropped countryball, since it already exists.
Attributes:
-
view(BallSpawnView) –The view of the spawn being caught.
-
user(User | Member) –The user catching.
-
player(Player) –The player catching.
-
special(Special | None) –The special event applied. Editable.
-
attack_bonus(int) –The attack bonus in percent. Editable.
-
health_bonus(int) –The health bonus in percent. Editable.
HookContext
HookRegistry
Holds all registered hooks. Available as bot.hooks.
Source code in ballsdex/core/hooks.py
add_cog
add_cog(cog: Cog)
Register all methods of a cog decorated with hook.
Source code in ballsdex/core/hooks.py
has
register
Register a hook function.
Parameters:
-
name(str) –The name of the hook.
-
func(HookFunc) –The async function to call with the context.
-
priority(int, default:0) –Hooks with a higher priority run first. Defaults to 0.
-
owner(object | None, default:None) –The object owning this hook, used to unregister all its hooks at once.
Source code in ballsdex/core/hooks.py
remove_cog
remove_cog(cog: Cog)
run
Run all functions registered for the hook of this context, and return the context.
Parameters:
-
ctx(C) –The context passed to each hook function.
Source code in ballsdex/core/hooks.py
unregister
Unregister a hook function. Does nothing if it wasn't registered.
unregister_owner
unregister_owner(owner: object)
Unregister all hook functions registered with this owner.
PreCatchContext
PreCatchContext(*, view: BallSpawnView, interaction: Interaction, player: Player, guess: str)
Bases: CancellableContext
A user guessed the name of a countryball correctly and is about to catch it. Cancelling prevents the catch, the countryball stays available to others.
Attributes:
-
view(BallSpawnView) –The view of the spawn being caught.
-
interaction(Interaction) –The interaction of the catch prompt, already deferred.
-
player(Player) –The player trying to catch.
-
guess(str) –The name typed by the user.
PreGiveContext
Bases: CancellableContext
A player is about to give countryballs to another, or send them a donation request. Cancelling blocks the donation, and the reason is shown to the sender.
Attributes:
PreTradeContext
Bases: CancellableContext
Both users confirmed a trade and it is about to be saved. Cancelling ends the trade without exchanging anything, and the reason is shown on the trade message.
Attributes:
SpawnCheckContext
SpawnCheckContext(*, guild: Guild, channel: TextChannel, message: Message, algo: str)
Bases: CancellableContext
A countryball is about to spawn naturally. Cancelling silently skips this spawn.
Attributes:
hook
Mark a cog method as a hook. It is registered when the cog is added to the bot.
Parameters:
-
name(str) –The name of the hook.
-
priority(int, default:0) –Hooks with a higher priority run first. Defaults to 0.