examples/channels.pl

filter: Statementstmt 100% 34 / 34 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 94.1% 48 / 51 CC 3 SCAR 2.9
CC
3
Cov
95%
__ANON__
2.0
list_channels
2.0
BEGIN
1.0
CRAP 1.3
« examples/messages.pl ↑ Summary blib/lib/Discord/Client/Model/Channel.pm »
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
BranchTrueFalseTotal
unless defined $guild_id 1 1 2
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
BranchTrueFalseTotal
length $names ? : 1 0 1
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
BranchTrueFalseTotal
unless caller 0 1 1
61 1 $bot;
« examples/messages.pl ↑ Summary blib/lib/Discord/Client/Model/Channel.pm »