X-Git-Url: https://ruin.nu/git/index.pl?a=blobdiff_plain;f=Bot.pm;h=0d517867f3dd39869ec9a4e542ca0afee753b0c6;hb=445fc8eeeb37b4728b1d7608862e772856eebb19;hp=89ec47d7c1e75ae9d7c3cb397b07e0b92b6a2026;hpb=0822847cd53af14ada68762a2f9d152a274a4d9b;p=NDIRC.git diff --git a/Bot.pm b/Bot.pm index 89ec47d..0d51786 100644 --- a/Bot.pm +++ b/Bot.pm @@ -26,6 +26,7 @@ use Moose; use POE::Component::IRC::Common qw/irc_to_utf8/; use POE::Session; +use POE::Kernel; use POE::Component::IRC::Plugin::Logger; use POE::Component::IRC::Plugin::BotTraffic; use POE::Component::IRC::Plugin::Connector; @@ -36,7 +37,7 @@ use Mojo::Discord; use Mojo::IOLoop; use NDIRC::Dispatcher; -use NDIRC::Context; +use NDIRC::IrcContext; use IO::File; @@ -61,6 +62,18 @@ has discord_id => ( isa => 'Str' ); +has discord_channels => ( + is => 'rw', + isa => 'HashRef', + default => sub { {} } +); + +has targets => ( + is => 'ro', + isa => 'HashRef[ArrayRef[Str]]', + default => sub{ {} }, +); + # We registered for all events, this will produce some debug info. sub _default { my ($event, $args) = @_[ARG0 .. $#_]; @@ -128,6 +141,7 @@ sub _start { '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, @@ -228,13 +242,16 @@ sub _build_disp { $disp->load(@commands); } + %{$self->targets} = (); my $channels = new IO::File 'channels' or die $!;; while (<$channels>){ my ($chan, @types) = split /\s+/; say "$chan - @types"; if ($chan =~ /^(.*):(.*)$/){ $chan = $1; - $disp->set_target($2,$chan); + $self->targets->{$2} = [] unless exists $self->targets->{$2}; + push @{$self->targets->{$2}},$chan; + say "$2 - @{$self->targets->{$2}}"; } $disp->add_channel($chan,\@types); } @@ -329,6 +346,23 @@ 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 { @@ -363,7 +397,7 @@ sub parseCommand { } $address =~ s/.*@(.*)/$1/; - my $c = NDIRC::Context->new({ + my $c = NDIRC::IrcContext->new({ host => $address, nick => $nick, channel => $channel, @@ -376,4 +410,24 @@ sub parseCommand { return $self->disp->run_command($c,$command,$args); } +sub toTarget { + my ($self, $target, $msg) = @_; + + return unless exists $self->targets->{$target}; + + my $session = $poe_kernel->get_active_session(); + my $heap = $session->get_heap(); + my $irc = $heap->{irc}; + + + for (@{$self->targets->{$target}}) { + when (/^#/) { + $irc->yield(privmsg => $_, $msg); + } + when (/^d-(\d+)/i) { + $self->discord->send_message($1, $msg ); + } + } +} + 1;