ComponentRouter
in package
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
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
PREFIX
Marks a component as ours.
public
mixed
PREFIX
= 'bridge'
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
boolid()
Builds a `custom_id` for an action and its arguments.
public
static id(string $action, string ...$args) : string
Parameters
- $action : string
- $args : string
Tags
Return values
stringon()
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
selfparse()
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