The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

AnyEvent::Discord::Client - A Discord client library for the AnyEvent framework.

SYNOPSIS

    use AnyEvent::Discord::Client;

    my $token = 'NjI5NTQ4Mjg3NTMxMjg2......';
    
    my $bot = new AnyEvent::Discord::Client(
      token => $token,
      commands => {
        'commands' => sub {
          my ($bot, $args, $msg, $channel, $guild) = @_;
          $bot->say($channel->{id}, join("   ", map {"`$_`"} sort grep {!$commands_hidden{$_}} keys %{$bot->commands}));
        },
      },
    );
    
    $bot->add_commands(
      'hello' => sub {
        my ($bot, $args, $msg, $channel, $guild) = @_;
    
        $bot->say($channel->{id}, "hi, $msg->{author}{username}!");
      },
    );
    
    $bot->connect();
    AnyEvent->condvar->recv;

After adding this bot to a channel in a Discord Guild, type '!hello' in chat to run the example command.

DESCRIPTION

This module provides the functionality required to create a simple Discord client or bot using the REST and WebSocket interfaces to Discord.

CONSTRUCTION

AnyEvent::Discord::Client->new(%opts)
    AnyEvent::Discord::Client->new(token => $token)

Creates a new AnyEvent::Discord::Client with the given configuration. Takes the following parameters:

token

A Discord Bot token as given by the "Bot" section of a Discord Developer Portal application. Required.

api_root

The Discord API root to use. Default https://discordapp.com/api.

prefix

The command prefix to use when looking for registered commands in chat. Default !.

commands

A hashref of commands to begin with as if the hash were passed to register_commands().

METHODS

commands()

Returns a hashref of the currently registered commands.

user()

Returns a hashref representing a Discord User object for the currently logged-in user.

guilds()

Returns a hashref of guild IDs to hashrefs representing Discord Guild objects for any Guilds the client has seen.

channels()

Returns a hashref of channel IDs to hashrefs representing Discord Channel objects for any Channels the client has seen.

roles()

Returns a hashref of role IDs to hashrefs representing Discord Role objects for any Roles the client has seen.

connect()

Causes the client to connect to Discord. Will automatically attempt to reconnect if disconnected. Returns nothing and immediately; to wait forever and prevent the program from exiting, follow this call with:

    AnyEvent->condvar->recv;
say($channel_id, $message)

Sends the given $message text to the given $channel_id.

typing($channel)

Displays a typing indicator in the given channel. Discord automatically removes the indicator after a few seconds; to keep it longer, this method returns an AnyEvent watcher that you can keep in scope until you're done with your operation, then set it to undef after. For example:

    my $typing_watcher = $bot->typing($channel);
    
    # Now, do a potentially very slow operation, like calling an API.
    
    # Once the API responds, even asynchronously, disable the watcher:
    undef $typing_watcher;
add_commands(%commands)

Installs new commands - chat messages that begin with the prefix given during construction and any key from the given hash. When seen as a chat message, the corresponding subref of the registered command will be invoked. The subref is passed a reference to the AnyEvent::Discord::Client object, the text after the command, and hashrefs representing the relevant Discord Message, Channel, and Guild objects. For example:

    $bot->add_commands(
      # register "!hello" command
      'hello' => sub {
        my ($bot, $args, $msg, $channel, $guild) = @_;
    
        $bot->say($channel->{id}, "hi, $msg->{author}{username}!");
      },
    );
api($method, $path, $data, $cb)

Invokes the Discord API asynchronously and returns immediately. $method is the HTTP method to use; $path is the endpoint to call. If $data is a reference, it is sent as JSON; otherwise, if it is defined, it is sent as a x-www-form-urlencoded body. Calls $cb with undef on failure. On success, calls $cb with the decoded JSON result if the response type is application/json or 1 otherwise.

api_sync($method, $path, $data)

Invokes the Discord API synchronously and returns the result of the call. $method is the HTTP method to use; $path is the endpoint to call. If $data is a reference, it is sent as JSON; otherwise, if it is defined, it is sent as a x-www-form-urlencoded body. Returns undef on failure. On success, returns the decoded JSON result if the response type is application/json or 1 otherwise.

websocket_send($op, $d)

Sends a raw WebSocket payload as per the Discord Gateway documentation.

AUTHOR

Eric Wastl, <topaz at cpan.org>

BUGS

Please report any bugs or feature requests to bug-anyevent-discord-client at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-Discord-Client. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc AnyEvent::Discord::Client

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2019 Eric Wastl.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.