AnyEvent-Discord-Client

 view release on metacpan or  search on metacpan

lib/AnyEvent/Discord/Client.pm  view on Meta::CPAN

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$VERSION = eval $VERSION;
 
use JSON;
use URI;
 
my $debug = 0;
 
sub new {
  my ($class, %args) = @_;
 
  my $self = {
    token => delete($args{token}),
    api_root => delete($args{api_root}) // 'https://discordapp.com/api',
    prefix => delete($args{prefix}) // "!",
    commands => delete($args{commands}) // {},

lib/AnyEvent/Discord/Client.pm  view on Meta::CPAN

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
});
 
$self->{conn}->on(each_message => sub {
  my($connection, $message) = @_;
  my $msg = decode_json($message->{body});
  die "invalid message" unless ref $msg eq 'HASH' && defined $msg->{op};
 
  $self->{last_seq} = 0+$msg->{s} if defined $msg->{s};
 
  if ($msg->{op} == 0) { #dispatch
    print "\e[1;30mdispatch event $msg->{t}:".Dumper($msg->{d})."\e[0m\n" if $debug;
    $event_handler{$msg->{t}}($self, $msg->{d}) if $event_handler{$msg->{t}};
  } elsif ($msg->{op} == 10) { #hello
    $self->{heartbeat_timer} = AnyEvent->timer(
      after => $msg->{d}{heartbeat_interval}/1e3,
      interval => $msg->{d}{heartbeat_interval}/1e3,
      cb => sub {
        $self->websocket_send(1, $self->{last_seq});
      },
    );
  } elsif ($msg->{op} == 11) { #heartbeat ack
    # ignore for now; eventually, notice missing ack and reconnect
  } else {
    print "\e[1;30mnon-event message op=$msg->{op}:".Dumper($msg)."\e[0m\n" if $debug;
  }
});
 
$self->{conn}->on(parse_error => sub {
  my ($connection, $error) = @_;
  print "parse_error: $error\n";
  exit;
});
 
$self->{conn}->on(finish => sub {



( run in 0.227 second using v1.01-cache-2.11-cpan-d6f9594c0a5 )