X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Bot.pm;h=0a65f8fbafc11c7f84f7774579bb36e9818f1dd2;hb=14fe324122bee271d8153f3db81491240a87895c;hp=9a289f0695a55a39866706d9f199a5bd491ecb26;hpb=17aabe51d5b62ae4bcae5ec3b02757ab5aa65b77;p=NDIRC.git diff --git a/Bot.pm b/Bot.pm index 9a289f0..0a65f8f 100644 --- a/Bot.pm +++ b/Bot.pm @@ -32,8 +32,11 @@ 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; +use NDIRC::IrcContext; use IO::File; @@ -43,6 +46,27 @@ 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' +); + +has discord_channels => ( + is => 'rw', + isa => 'HashRef', + default => sub { {} } +); + # We registered for all events, this will produce some debug info. sub _default { my ($event, $args) = @_[ARG0 .. $#_]; @@ -61,7 +85,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 +118,29 @@ 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(@_) }, + 'CHANNEL_CREATE' => sub { $self->discord_channel_create(@_) }, + }, + 'reconnect' => 1, + 'verbose' => 1, + )); + $self->discord->init(); + } return; } @@ -276,6 +323,38 @@ 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 { + my $self = shift; + my $hash = shift; + + for my $chan (@{$hash->{channels}}) { + say localtime(time) . " - $chan->{id} - $chan->{name}"; + $self->discord_channels->{$chan->{id}} = $chan; + } +} + +sub discord_channel_create { + my $self = shift; + my $chan = shift; + + for my $key (keys %{$chan}) { + say localtime(time) . " - $key - $chan->{$key}"; + } + $self->discord_channels->{$chan->{id}} = $chan; +} + sub parseCommand { my ($self, $msg, $server, $nick, $address, $channel, $model) = @_; @@ -284,8 +363,8 @@ sub parseCommand { my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/); - if ($msg =~ m{http://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){ - if (!$command || $command =~ m{^http://}){ + if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){ + if (!$command || $command =~ m{^https?://}){ ($p,$command,$args) = ('.','addscan',$msg); }elsif($command ne 'addscan'){ $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model) @@ -308,7 +387,7 @@ sub parseCommand { } $address =~ s/.*@(.*)/$1/; - my $c = NDIRC::Context->new({ + my $c = NDIRC::IrcContext->new({ host => $address, nick => $nick, channel => $channel,