+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 handleCommand {
+ my ($self, $c, $msg) = @_;
+
+ my ($p,$command,$args) = ($msg =~ /^([.])(\S+)(?: (.+))?/);
+
+ if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
+ if (!$command || $command =~ m{^https?://}){
+ ($p,$command,$args) = ('.','addscan',$msg);
+ }elsif($command ne 'addscan'){
+ $self->handleCommand ($c, ".addscan $msg")
+ }
+ }
+
+ return 0 unless $self->disp->has_command($command,$c->channel);
+
+ say localtime(time) . " - $msg";
+
+ return $self->disp->run_command($c,$command,$args);
+}
+
+sub parseCommand {
+ my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
+
+ return if $channel !~ /^#/ && $msg =~ /^~/;
+ $msg = ".$msg" if $channel !~ /^#/ && $msg =~ /^[^.!]/;
+
+ my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
+
+ 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)
+ }
+ }
+
+ return 0 unless $self->disp->has_command($command,$channel);
+
+ my $reply_string;
+ given ($p){
+ when ('!'){
+ $reply_string = "privmsg $nick";
+ }
+ when ('~'){
+ $reply_string = "privmsg $channel";
+ }
+ default {
+ $reply_string = "notice $nick";
+ }
+ }
+
+ $address =~ s/.*@(.*)/$1/;
+ my $c = NDIRC::IrcContext->new({
+ host => $address,
+ nick => $nick,
+ channel => $channel,
+ disp => $self->disp,
+ model => $model,
+ server => $server,
+ bot => $self,
+ reply_string => $reply_string,
+ });
+
+ return $self->disp->run_command($c,$command,$args);
+}
+
+sub toTarget {
+ my ($self, $target, $msg) = @_;
+
+ return unless exists $self->targets->{$target};
+
+ $self->message($msg, @{$self->targets->{$target}});
+
+}
+
+sub message {
+ my ($self, $msg, @targets) = @_;
+
+ for (@targets) {
+ when (/^D-(\d+)$/i) {
+ $self->discordMessage($1, $msg);
+ }
+ default {
+ $self->ircMessage(privmsg => $_, $msg);
+ }
+ }
+}
+
+sub ircMessage {
+ my ($self, $command, $target, $msg) = @_;
+
+ $msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
+ $msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;
+
+ $self->irc->yield($command, $target, $msg);
+}
+
+sub discordMessage {
+ my ($self, $target, $msg) = @_;
+
+ $msg =~ s`<b>(.*?)</b>`**$1**`gi;
+ $msg =~ s`<c(\d+)>(.*?)</c>`*$2*`gi;
+
+ $self->discord->send_message($target, $msg );
+}
+