AnyEvent-Discord
view release on metacpan or search on metacpan
lib/AnyEvent/Discord.pm view on Meta::CPAN
=head1 DESCRIPTION
This module provides an AnyEvent interface for the Discord API over the REST
and WebSocket APIs. It is designed to be somewhat similar to the SlackRTM and
XMPP modules, with a subset of their far more mature functionality.
To get started, one needs to create a new application in the Discord Developer
Portal (https://discord.com/developers). Once an application is created, a token
can be captured by navigating to the "Bot" tab on the left side and selecting
'Click to Reveal Token'. That generated token is the same token required by this
module.
=head1 CONFIGURATION ACCESSORS
=over 4
=item token (String) (required)
The token generated by the Discord Application portal, under Bot.
=item base_uri (String) (optional)
The base URI for communicating with the Discord API.
=item socket_options (HashRef) (optional)
Used to override options to sent to AnyEvent::WebSocket::Client, if needed.
=item verbose (Num) (defaults to 0)
Verbose output, writes internal debug information at 1, additionally writes
network conversation at 2.
=back
=head1 DATA ACCESSORS
=over 4
=item guilds
Available/created/seen guilds, as a hashmap of id => name
=item channels
Available/created/seen channels, as a hashmap of id => name
=item users
Available/created/seen users, as a hashmap of id => name
=back
=head1 PUBLIC METHODS
=over 4
=item new(\%arguments)
Instantiate the AnyEvent::Discord client. The hashref of arguments matches the
configuration accessors listed above. A common invocation looks like:
my $client = AnyEvent::Discord->new({ token => 'ABCDEF' });
=item on($event_type, \&handler)
Attach an event handler to a defined event type. If an invalid event type is
specified, no error will occur -- this is mostly to be able to handle events
that are created after this module is published. This is an append method, so
calling on() for an event multiple times will call each callback assigned. If
the handler already exists for an event, no error will be returned, but the
handler will not be called twice.
Discord event types: https://discord.com/developers/docs/topics/gateway#list-of-intents
Opcodes: https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-opcodes
These events receive the parameters client, data object (d) and the opcode (op).
The $client variable is this instance of AnyEvent::Discord, and the contents of
$data and $op are dependent on the event type.
sub event_responder {
my ($client, $data, $opcode) = @_;
return;
}
Internal event types:
=over 4
=item disconnected
Receives no parameters, just notifies a disconnection will occur. It will
auto reconnect.
=item error
Receives an error message as a parameter, allows internal handling of errors
that are not a hard failure.
=back
=item off($event_type, \&handler?)
Detach an event handler from a defined event type. If the handler does not
exist for the event, no error will be returned. If no handler is provided, all
handlers for the event type will be removed.
=item connect()
Start connecting to the Discord API and return immediately. In a new AnyEvent
application, this would come before executing "AnyEvent->condvar->recv". This
method will retrieve the available gateway endpoint, create a connection,
identify itself, begin a heartbeat, and once complete, Discord will fire a
'ready' event to the handler.
=item send($channel_id, $content)
Send a message to the provided channel.
=item typing($channel_id)
( run in 3.546 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )