| 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 that lists the current server's channels. | ||||||||||
| 10 | my $bot = Discord::Client->new( | ||||||||||
| 11 | token => $ENV{DISCORD_BOT_TOKEN}, | ||||||||||
| 12 | 1 | intents => ['GUILDS'], | |||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | # Acknowledge before fetching channels, then complete the deferred reply. | ||||||||||
| 16 | 2 | ▶ | sub list_channels { | ||||||||
|
Subroutine
list_channels: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 17 | 2 | my ( $interaction, $bot ) = @_; | |||||||||
| 18 | |||||||||||
| 19 | # Reject direct-message use before acknowledging or requesting guild resources. | ||||||||||
| 20 | 2 | my $guild_id = $interaction->guild_id; | |||||||||
| 21 | 2 | ▶ | return $interaction->reply( 'Use this command in a server.', ephemeral => 1 ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | unless defined $guild_id; | ||||||||||
| 23 | |||||||||||
| 24 | # Acknowledge before REST I/O so channel lookup does not consume the reply deadline. | ||||||||||
| 25 | return $interaction->defer( ephemeral => 1 )->then( | ||||||||||
| 26 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | my $guild = $bot->model( guild => { id => $guild_id } ); | |||||||||
| 28 | 1 | return $guild->channels; | |||||||||
| 29 | } | ||||||||||
| 30 | )->then( | ||||||||||
| 31 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 32 | 1 | my ($channels) = @_; | |||||||||
| 33 | |||||||||||
| 34 | # Bound the displayed names and suppress mentions in server-controlled channel names. | ||||||||||
| 35 | 1 | my $names = join( ', ', map { $_->name } @$channels ); | |||||||||
| 36 | 1 | $names = substr( $names, 0, 1900 ); | |||||||||
| 37 | |||||||||||
| 38 | 1 | ▶ | return $interaction->reply( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | { | ||||||||||
| 40 | content => length($names) ? "Channels: $names" : 'No channels found.', | ||||||||||
| 41 | allowed_mentions => { parse => [] }, | ||||||||||
| 42 | } | ||||||||||
| 43 | ); | ||||||||||
| 44 | } | ||||||||||
| 45 | 1 | ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | # Keep the command declaration focused on its purpose. | ||||||||||
| 49 | $bot->command( | ||||||||||
| 50 | channels => ( | ||||||||||
| 51 | description => 'List this server\'s channels', | ||||||||||
| 52 | 2 | ▶ | handler => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 2 | my ($interaction) = @_; | |||||||||
| 54 | 2 | return list_channels( $interaction, $bot ); | |||||||||
| 55 | }, | ||||||||||
| 56 | 1 | ), | |||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | # Run the bot, or expose its declarations to the deployment script. | ||||||||||
| 60 | 1 | ▶ | $bot->run unless caller; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 1 | $bot; | |||||||||