NAME
Discord::Client::Interaction - Interaction responses and payload access
SYNOPSIS
# Acknowledge before slow work and return the entire operation.
return $interaction->defer(ephemeral => 1)->then(sub {
return $bot->loop->delay_future(after => 1);
})->then(sub {
return $interaction->reply('Your report is ready.');
});
DESCRIPTION
Command, component, autocomplete and modal handlers receive an Interaction. Reply within three seconds or defer before starting slow work. Return the handler's Future. The SDK does not acknowledge handlers automatically. Use Futures or optional Future::AsyncAwait; blocking sleep or get stops the shared loop.
Only one initial response is allowed. Failed or cancelled initial operations are not replayed because delivery may be uncertain. The three-second deadline includes event queue and REST waiting time. Expiry fails with deadline and cancels I/O. Edits and followups require confirmed acknowledgement and expire fifteen minutes after receipt.
Operations use the interaction token without Bot authorization. Token rejection invalidates the context. Failures include a sanitized Discord::Client::Error with separate HTTP status and Discord code. The client reports handler failures through its error callback; standalone dispatch uses Discord::Client::Interactions.
RESPONSE METHODS
All response methods return Futures. Message arguments accept text or Discord JSON hashrefs, including embeds, components and allowed_mentions. ephemeral => 1 makes a reply private.
reply
reply(text_or_hash, ephemeral => 1) sends an initial reply. After a successful defer, it edits the original response. Visibility chosen by defer cannot be changed by the completing reply.
defer
defer(ephemeral => 1) acknowledges before slow work. defer(update => 1) defers an update to a component's message.
update
update(text_or_hash) updates the message for a component interaction. Update and deferred-update responses also support message-associated modal submissions.
autocomplete
autocomplete(choices_array) answers autocomplete. Choices contain name and value. Normal message replies cannot answer autocomplete.
modal
modal(modal_hash) opens a modal from a command or component. Use "modal" in Discord::Client::UI or a native Discord payload.
get_original
Fetches the original response; the Future yields its message hashref.
edit_original
edit_original(text_or_hash) edits the original response; the Future yields its message hashref.
delete_original
Deletes the original response; the Future resolves to undef.
followup
followup(text_or_hash, ephemeral => 1) sends another message; the Future yields its message hashref.
edit_followup
edit_followup(message_id, text_or_hash) edits a followup; the Future yields its message hashref.
delete_followup
delete_followup(message_id) deletes a followup; the Future resolves to undef.
CONTEXT METHODS
Accessors are synchronous. IDs remain strings.
new
new(payload => $payload, loop => $loop, rest => $rest) wraps an incoming interaction. Dispatchers normally construct this for handlers. payload is required. loop and rest are optional; a loop is created when absent and token-only REST is created when first needed. Invalid payloads throw. Construction starts the initial acknowledgement timer. received_at supplies receipt time in monotonic seconds. Tests can inject clock and delay.
initial_sender can supply an inline HTTP response callback. It receives the response hashref and returns a Future confirming acceptance; later webhook operations still use REST.
id
Returns the interaction ID.
application_id
Returns the application ID.
type
Returns the numeric interaction type.
raw
Returns the incoming payload hashref. It contains the interaction token; do not log it.
option
option(name) returns a command argument or undef when absent.
options
Returns a hashref of command arguments.
command_path
Returns an arrayref of selected group/subcommand names, excluding the root command.
focused
Returns the focused autocomplete option hashref or undef.
resolved
resolved(kind, id) returns a resolved resource. Omit id for the resource map of that kind; omit both arguments for the full map.
user
Returns the caller's user hashref, including from guild member data.
member
Returns the guild member hashref or undef.
guild_id
Returns the guild ID or undef.
channel_id
Returns the channel ID or undef.
custom_id
Returns a component's custom ID or undef.
values
Returns the select values arrayref, or an empty arrayref when absent.
field
field(custom_id) reads a submitted modal Text Input. Empty input returns ''; a missing field or non-modal interaction returns undef. It reads Label and Action Row children. Other fields remain available through raw. Invalid field IDs throw.
state
Returns the response state string.
responded
Returns true when the initial response has been reserved, including while sending or after failure. It does not confirm successful acknowledgement.
acknowledgement
Returns the Future for the initial response, tracked separately from the handler's work.
close
Cancels operations and acknowledgement timers. Returns a Future for cleanup of owned resources; an injected REST client remains caller-owned.
LICENSE
Apache License 2.0.