| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | 1 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use FindBin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use lib "$FindBin::Bin/../lib"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Discord::Client; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # Configure a bot with a small asynchronous workflow. | ||||||||||
| 10 | my $bot = Discord::Client->new( | ||||||||||
| 11 | token => $ENV{DISCORD_BOT_TOKEN}, | ||||||||||
| 12 | 1 | intents => ['GUILDS'], | |||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | # Acknowledge first, yield to the loop, then complete the private reply. | ||||||||||
| 16 | 2 | ▶ | sub prepare_report { | ||||||||
|
Subroutine
prepare_report: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 17 | 2 | my ( $interaction, $bot ) = @_; | |||||||||
| 18 | |||||||||||
| 19 | # Chain each stage so cancellation propagates from the reply back to the pending delay. | ||||||||||
| 20 | return $interaction->defer( ephemeral => 1 )->then( | ||||||||||
| 21 | 2 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | # Yield to the shared loop while waiting; Gateway heartbeats continue to run. | ||||||||||
| 24 | 2 | return $bot->loop->delay_future( after => 1 ); | |||||||||
| 25 | } | ||||||||||
| 26 | )->then( | ||||||||||
| 27 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | # Complete the acknowledged private response after the simulated work finishes. | ||||||||||
| 30 | 1 | return $interaction->reply('Your report is ready.'); | |||||||||
| 31 | } | ||||||||||
| 32 | 2 | ); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | # Register the workflow while leaving deployment explicit. | ||||||||||
| 36 | $bot->command( | ||||||||||
| 37 | report => ( | ||||||||||
| 38 | description => 'Prepare a report asynchronously', | ||||||||||
| 39 | 2 | ▶ | handler => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 2 | my ($interaction) = @_; | |||||||||
| 41 | 2 | return prepare_report( $interaction, $bot ); | |||||||||
| 42 | }, | ||||||||||
| 43 | 1 | ), | |||||||||
| 44 | ); | ||||||||||
| 45 | |||||||||||
| 46 | # Run the bot, or expose its declarations to the deployment script. | ||||||||||
| 47 | 1 | ▶ | $bot->run unless caller; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 1 | $bot; | |||||||||