Discord-Client

 view release on metacpan or  search on metacpan

lib/Discord/Client/Interaction.pm  view on Meta::CPAN

                || abs( $choice->{value} ) > 9007199254740992
                || ( $type == 4
                    && ( $choice->{value} != int( $choice->{value} ) || abs( $choice->{value} ) >= 9007199254740992 ) );
        }
        push @wire, { %$choice, value => $type == 3 ? "$choice->{value}" : 0 + $choice->{value} };
    }
    return $self->_initial( 8, { choices => \@wire }, 'replied' );
}

sub modal {
    my ( $self, $data ) = @_;

    # A modal opens only from a command or message component, never a modal submission.
    return $self->_failed( 'Modal response is invalid for this interaction', 'state' )
        unless $self->{type} == 2 || $self->{type} == 3;
    return $self->_failed( 'Invalid modal response', 'request' )
        unless ref($data) eq 'HASH'
        && defined( $data->{custom_id} )
        && !ref( $data->{custom_id} )
        && length( $data->{custom_id} ) <= 100
        && length( $data->{custom_id} ) > 0
        && defined( $data->{title} )
        && !ref( $data->{title} )
        && length( $data->{title} ) > 0
        && length( $data->{title} ) <= 45
        && ref( $data->{components} ) eq 'ARRAY';
    return $self->_initial( 9, $data, 'replied' );
}

sub _initial {
    my ( $self, $type, $data, $next_state ) = @_;

    # Reject closed, expired or already-answered contexts before attempting delivery.
    return $self->_failed( 'Interaction is closed', 'shutdown' ) if $self->{closed};
    return $self->_failed( 'Interaction initial response deadline exceeded', 'deadline' )
        if $self->{clock}->() >= $self->{deadline} || $self->{state} eq 'expired';
    return $self->_failed( 'Interaction already has an initial response', 'state' ) unless $self->{state} eq 'new';

    # Validate serialization before consuming the one initial-response opportunity.
    my $payload = { type => $type };
    $payload->{data} = $data if defined $data;
    return $self->_failed( 'Interaction response is not JSON encodable', 'request' )
        unless eval {
            JSON::PP->new->encode($payload);
            1;
        };

    # Reserve the response before invoking either REST or an inline HTTP sender.
    $self->{state} = 'responding';
    my $source = eval { $self->_send_initial_response($payload) };
    my $future = $self->_observe($source);
    $self->{initial_operation} = $future;
    $self->{initial_source} = $source if blessed($source) && $source->isa('Future');
    $future->on_ready( sub { $self->_complete_initial_response( $future, $next_state ) } );
    return $future;
}

sub _send_initial_response {
    my ( $self, $payload ) = @_;

    # HTTP delivery can answer inline; Gateway delivery uses the unauthenticated callback endpoint.
    return $self->{initial_sender}->($payload) if $self->{initial_sender};
    my $path = '/interactions/' . $self->{id} . '/' . uri_escape_utf8( $self->{token} ) . '/callback';
    return $self->_rest->request( 'POST', $path, auth => 0, json => $payload );
}

sub _complete_initial_response {
    my ( $self, $future, $next_state ) = @_;
    return if $self->{state} eq 'expired' || $self->{closed};

    # Release delivery state and deadline before notifying acknowledgement observers.
    delete @{$self}{qw(initial_operation initial_source)};
    if ( my $timer = delete $self->{initial_timer} ) {
        $timer->cancel unless $timer->is_ready;
    }

    # Only confirmed delivery enables webhook operations; failure remains non-retryable.
    if ( $future->is_done ) {
        $self->{state} = $next_state;
        $self->{acknowledgement}->done unless $self->{acknowledgement}->is_ready;
        return;
    }

    # Cancellation cannot distinguish a rejected response from one already delivered.
    $self->{state} = 'failed';
    my $failure = $future;
    unless ( $future->is_failed ) {
        $failure = $self->_failed( 'Interaction response cancelled; delivery is uncertain', 'cancelled' );
    }
    $self->{acknowledgement}->fail( $failure->failure ) unless $self->{acknowledgement}->is_ready;
}

sub _rest {
    my ($self) = @_;

    # Standalone contexts lazily create token-only REST and own its cleanup.
    unless ( $self->{rest} ) {
        $self->{rest} = Discord::Client::REST->new( auth => 0, loop => $self->{loop} );
        $self->{own_rest} = 1;
    }
    return $self->{rest};
}

sub _observe {
    my ( $self, $source ) = @_;
    return $self->_failed( 'Interaction transport failed', 'transport' )
        unless blessed($source) && $source->isa('Future');

    # Track operations so context shutdown can cancel both the observer and transport.
    my $future = $self->{loop}->new_future;
    my $key = refaddr($future);
    $self->{operations}{$key} = $future;
    $future->on_cancel( sub { $source->cancel unless $source->is_ready } );

    # Detach completed work before forwarding its outcome to application callbacks.
    $source->on_ready(
        sub {
            delete $self->{operations}{$key};
            return if $future->is_ready;
            if ( $source->is_done ) {
                $future->done( $source->result );



( run in 0.637 second using v1.01-cache-2.11-cpan-eb134014566 )