DiscordPHP-BridgeBot Documentation

ComponentRouter
in package

FinalYes

Routes button and select-menu presses by `custom_id`.

DiscordPHP can attach a listener to a Button instance, but that listener lives in the process that built the button. A panel posted before a restart would go dead, and every panel would hold a closure for as long as the message existed. The bridge instead encodes what a button does into its custom_id and routes on that, so a panel from last week still works and the process holds one handler per action rather than one per message.

Ids look like tg:unlink:1234567890: a fixed prefix so another bot's components are ignored, an action, then arguments. Discord caps a custom_id at 100 characters, which is enforced when one is built rather than discovered when Discord rejects the message.

Tags
author

Valithor Obsidion valithor@discordphp.org

Table of Contents

Constants

ID_LIMIT  : mixed = 100
Discord's hard limit on a `custom_id`.
PREFIX  : mixed = 'bridge'
Marks a component as ours.

Properties

$handlers  : array<string, callable(Interaction, array<int, string>): mixed>

Methods

dispatch()  : mixed
Hands an interaction to its handler.
handles()  : bool
Whether anything is registered for an action.
id()  : string
Builds a `custom_id` for an action and its arguments.
on()  : self
Registers the handler for one action.
parse()  : array{action: string, args: list}|null
Splits one of our ids back up, or `null` when it is not ours.

Constants

ID_LIMIT

Discord's hard limit on a `custom_id`.

public mixed ID_LIMIT = 100

Properties

$handlers

private array<string, callable(Interaction, array<int, string>): mixed> $handlers = []

Methods

dispatch()

Hands an interaction to its handler.

public dispatch(Interaction $interaction) : mixed

Returns null — not a rejected promise — for anything that is not ours to handle, because this runs on every INTERACTION_CREATE in every guild the bot is in, including components belonging to other features.

Parameters
$interaction : Interaction

handles()

Whether anything is registered for an action.

public handles(string $action) : bool
Parameters
$action : string
Return values
bool

id()

Builds a `custom_id` for an action and its arguments.

public static id(string $action, string ...$args) : string
Parameters
$action : string
$args : string
Tags
throws
InvalidArgumentException

when an argument contains the : separator, or the result is too long for Discord to accept.

Return values
string

on()

Registers the handler for one action.

public on(string $action, callable(Interaction, array<int, string>): mixed $handler) : self
Parameters
$action : string
$handler : callable(Interaction, array<int, string>): mixed
Return values
self

parse()

Splits one of our ids back up, or `null` when it is not ours.

public static parse(string|null $customId) : array{action: string, args: list}|null
Parameters
$customId : string|null
Return values
array{action: string, args: list}|null
On this page

Search results