]> ruin.nu Git - NDIRC.git/blobdiff - Bot.pm
Initial Discord support
[NDIRC.git] / Bot.pm
diff --git a/Bot.pm b/Bot.pm
index 5693dd4d0b353c392d51a173736421262ba3f74d..89ec47d7c1e75ae9d7c3cb397b07e0b92b6a2026 100644 (file)
--- a/Bot.pm
+++ b/Bot.pm
@@ -32,6 +32,9 @@ use POE::Component::IRC::Plugin::Connector;
 use POE::Component::IRC::Plugin::AutoJoin;
 use POE::Component::IRC::Plugin::NickReclaim;
 
+use Mojo::Discord;
+use Mojo::IOLoop;
+
 use NDIRC::Dispatcher;
 use NDIRC::Context;
 
@@ -43,6 +46,21 @@ has disp => (
        lazy_build => 1
 );
 
+has discord => (
+       is => 'rw',
+       isa => 'Object'
+);
+
+has discord_name => (
+       is => 'rw',
+       isa => 'Str'
+);
+
+has discord_id => (
+       is => 'rw',
+       isa => 'Str'
+);
+
 # We registered for all events, this will produce some debug info.
 sub _default {
        my ($event, $args) = @_[ARG0 .. $#_];
@@ -61,7 +79,7 @@ sub _default {
 }
 
 sub _start {
-       my ($kernel,$heap,$session) = @_[KERNEL,HEAP,SESSION];
+       my ($self,$kernel,$heap,$session) = @_[OBJECT,KERNEL,HEAP,SESSION];
 
        # retrieve our component's object from the heap where we stashed it
        my $irc = $heap->{irc};
@@ -94,6 +112,28 @@ sub _start {
        $irc->yield( connect => { server => 'irc.netgamers.org' } );
 
        $kernel->delay( refresh => 60 );
+
+       if (my $f =  new IO::File 'discord'){
+               my $user = <$f>;
+               chomp $user;
+               my $token = <$f>;
+               chomp $token;
+
+               $self->discord(Mojo::Discord->new(
+                               'token'     => $token,
+                               'name'      => $user,
+                               'url'       => 'https://nd.ruin.nu',
+                               'version'   => '1.0',
+                               'callbacks' => {
+                                       'READY'          => sub { $self->discord_ready(@_) },
+                                       'MESSAGE_CREATE' => sub { $self->discord_message_create(@_) },
+                                       'GUILD_CREATE' => sub { $self->discord_guild_create(@_) },
+                               },
+                               'reconnect' => 1,
+                               'verbose'   => 1,
+                       ));
+               $self->discord->init();
+       }
        return;
 }
 
@@ -276,6 +316,21 @@ sub  refresh {
 sub irc_join {
 }
 
+sub discord_ready {
+       my $self = shift;
+       my $hash = shift;
+       $self->discord_id($hash->{user}{id});
+       $self->discord_name($hash->{user}{username});
+
+       say localtime(time) . " - Connected to Discord. $self->{discord_id}";
+}
+
+sub discord_message_create {
+}
+
+sub discord_guild_create {
+}
+
 sub parseCommand {
        my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;