| 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 | # Enable Message Content in the Developer Portal before running this example. | ||||||||||
| 10 | my $bot = Discord::Client->new( | ||||||||||
| 11 | token => $ENV{DISCORD_BOT_TOKEN}, | ||||||||||
| 12 | 1 | intents => [ 'GUILDS', 'GUILD_MESSAGES', 'DIRECT_MESSAGES', 'MESSAGE_CONTENT' ], | |||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | # A message already knows how to reply and which channel it belongs to. | ||||||||||
| 16 | 3 | ▶ | sub respond_to_message { | ||||||||
|
Subroutine
respond_to_message: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 17 | 3 | my ( $message, $bot ) = @_; | |||||||||
| 18 | |||||||||||
| 19 | # Ignore other bots before checking the command to avoid automated reply loops. | ||||||||||
| 20 | 3 | ▶ | return if $message->is_from_bot; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 2 | ▶ | return unless $message->content eq '!ping'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | |||||||||||
| 23 | # Return the pending reply so the client can track it and cancel it during shutdown. | ||||||||||
| 24 | 1 | return $message->reply('Pong!'); | |||||||||
| 25 | } | ||||||||||
| 26 | 1 | $bot->on_message( \&respond_to_message ); | |||||||||
| 27 | |||||||||||
| 28 | # Run the bot; message events need no application-command deployment. | ||||||||||
| 29 | 1 | ▶ | $bot->run unless caller; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 1 | $bot; | |||||||||