]> ruin.nu Git - NDIRC.git/blob - Delling.pm
Discord id in +user
[NDIRC.git] / Delling.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19 package NDIRC::Delling;
20
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use Moose;
26 extends 'NDIRC::Bot';
27
28 use POE::Session;
29 use ND::DB;
30
31 use NDIRC::DiscordContext;
32
33 my ($tick,$stattick) = (0,0);
34 my $last_announcement = 0;
35
36 after irc_public => sub {
37         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
38         my ($nick,$username,$address) = ( split /[!@]/, $who );
39         my $channel = $where->[0];
40
41         my $irc = $sender->get_heap();
42
43         #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
44         my $dbh = DB();
45         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
46         $seen->execute($address);
47
48         my $nickname = $irc->nick_name;
49         if ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
50                 local $_ = $1;
51                 my $text = $2;
52                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
53                         $msg = ".anon $_ $text";
54                         $self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh);
55                 }
56         }elsif ($self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh)){
57                 #Command parsed and run successfully
58         }
59 };
60
61 after irc_msg => sub {
62         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
63         my ($nick,$username,$address) = ( split /[!@]/, $who );
64         my $irc = $sender->get_heap();
65
66         my $dbh = DB();
67         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
68         $seen->execute($address);
69
70         if ($self->parseCommand($msg,$irc,$nick,$address,'pm',$dbh)){
71                 #Command parsed and run successfully
72         }else{
73                 $irc->yield(notice => $nick, "unknown command");
74         }
75 };
76
77 sub irc_join {
78         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
79         my ($nick,$username,$address) = ( split /[!@]/, $who );
80         my $irc = $sender->get_heap();
81         my $disp = $self->disp;
82
83         my $dbh = DB();
84         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
85         $seen->execute($address);
86
87         if($self->disp->has_command('voice',$channel)){
88                 my $flags = $dbh->prepare_cached(q{
89 SELECT DISTINCT flag
90 FROM users u
91         JOIN groupmembers g USING (uid)
92         JOIN channel_group_flags gf USING (gid)
93 WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
94                 });
95                 $flags->execute($address, $channel);
96                 my $mode = '';
97                 my @who;
98                 while (my ($flag) = $flags->fetchrow()){
99                         $mode .= $flag;
100                         push @who, $nick;
101                 }
102                 say "$mode - @who";
103                 $irc->yield(mode => $channel, $mode, @who) if $mode;
104         }
105
106         if (lc $channel ~~ lc $self->targets->{members}){
107                 if (time - $last_announcement < 1){
108                         $last_announcement = time;
109                         return;
110                 }
111                 my $user = $dbh->selectrow_hashref(q{
112 SELECT uid,pid,hostmask,password
113         ,(SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid) AS last_forum_visit
114 FROM users u WHERE hostmask = ?
115                 }, undef, $address);
116                 if ($user){
117                         unless ($user->{password}) {
118                                 $irc->yield(privmsg => $self->targets->{members}, "$nick: Get a new random password with /msg delling !getpass . If you don't know your username, then you can get it with .points");
119                         }
120                         if ($tick > 12 && not defined $user->{pid}){
121                                 $irc->yield(privmsg => $self->targets->{members}, "$nick: go to https://nd.ruin.nu/ and enter your coords.");
122                         }
123
124                         if (not defined $user->{last_forum_visit}){
125                                 $irc->yield(privmsg => $self->targets->{members}, "$nick: Go read the forum! https://nd.ruin.nu/forum");
126                         }else {
127                                 my $unread = $dbh->selectrow_hashref(q{SELECT * FROM unread_posts($1)},undef,$user->{uid});
128                                 if ($unread && $unread->{new}){
129                                         $irc->yield(notice => $nick, "$unread->{new} posts since your last forum visit ($unread->{unread} unread posts in total) https://nd.ruin.nu/forum/allUnread");
130                                 }
131                         }
132                 }
133                 if (time - $last_announcement < 2){
134                         $last_announcement = time;
135                         return;
136                 }
137                 $last_announcement = time;
138         }
139 }
140
141 sub refresh {
142         my ($self,$kernel,$heap) = @_[OBJECT,KERNEL,HEAP];
143         $kernel->delay( refresh => 60 );
144         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
145
146         my $irc = $heap->{irc};
147         my $disp = $self->disp;
148         my $dbh = DB();
149         my $scans = $dbh->prepare(q{SELECT s.scan_id
150                         ,coords(x,y,z),type
151                         ,array_agg(sr.nick) AS nick
152                         ,array_agg(sr.id) AS id
153                 FROM scan_requests sr
154                         JOIN scans s USING (pid,type)
155                         JOIN current_planet_stats USING (pid)
156                 WHERE sr.time > NOW() - '30 min'::INTERVAL
157                         AND s.tick >= sr.tick AND NOT sr.sent
158                 GROUP BY scan_id,x,y,z,type
159                 });
160         my $sentscan = $dbh->prepare(q{UPDATE scan_requests
161                 SET sent = TRUE WHERE id = ANY($1)
162                 });
163         $scans->execute;
164         while (my $scan = $scans->fetchrow_hashref){
165                 $heap->{irc}->yield(notice => $scan->{nick}, "($scan->{coords} $scan->{type})"
166                         ." http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
167                 $sentscan->execute($scan->{id});
168         }
169
170         my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats});
171         if ($tick != $row[0]){
172                 $tick = $row[0];
173                 $self->toTarget(def => "New tick: $tick");
174         }
175         if (defined $row[1] && $stattick != $row[1]){
176                 $stattick = $row[1];
177                 $self->toTarget(members => "New tick: $stattick");
178         }
179         my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent});
180         my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?});
181         $ircreqs->execute;
182         while (my $req = $ircreqs->fetchrow_hashref){
183                 if ($req->{channel} eq 'def'){
184                         $self->toTarget(def => chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}");
185                         #$self->parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$disp->targets->{def},$dbh);
186                 }else{
187                         $self->toTarget($req->{channel} => "<$req->{username} via webbie> $req->{message}");
188                 }
189                 $upircreq->execute($req->{id});
190         }
191         my $defmissions = $dbh->prepare(q{
192 SELECT username,call,tick,dm.fleet,p.value
193         ,100 * SUM(fs.amount * (metal + crystal + eonium) / 100) / p.value AS value
194 FROM users u
195         JOIN current_planet_stats p USING (pid)
196         JOIN launch_confirmations USING (uid)
197         JOIN fleets f USING (fid)
198         JOIN fleet_ships fs USING (fid)
199         JOIN defense_missions dm ON f.fid = dm.fleet
200         JOIN ship_stats ss USING (ship)
201 WHERE NOT dm.announced
202 GROUP BY username,call,tick,dm.fleet,p.value
203                 });
204         my $updefmis = $dbh->prepare(q{UPDATE defense_missions SET announced = TRUE WHERE fleet = ?});
205         $defmissions->execute();
206         while (my $mission = $defmissions->fetchrow_hashref){
207                 $self->toTarget(def => chr(3)."06 $mission->{username} sent def to call $mission->{call}, $mission->{value}% of value (tick $mission->{tick}) https://nd.ruin.nu/calls/edit/$mission->{call}");
208                 $updefmis->execute($mission->{fleet});
209         }
210
211 }
212
213 after sig_usr2 => sub {
214         my $self = shift;
215 };
216
217 after _start => sub {
218  ($tick,$stattick) = DB()->selectrow_array(q{SELECT tick(),max(tick) FROM planet_stats});
219 };
220
221 after discord_message_create => sub {
222         my $self = shift;
223         my $hash = shift;
224
225         my $author = $hash->{author};
226         my $msg = $hash->{content};
227         my $channel_id = $hash->{channel_id};
228         my $author_name = $author->{username}.'#'.$author->{discriminator};
229         my $author_id = $author->{id};
230         return if $author->{'id'} eq $self->discord_id; # Ignore my own messages
231
232         my $channel = "D-".$channel_id;
233         if (exists $self->discord_channels->{$channel_id}) {
234                 $channel = 'dm' if ($self->discord_channels->{$channel_id}->{type} == 1);
235         }
236
237         say localtime(time) . " - $channel_id $channel $author_name $author_id";
238
239         my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
240
241
242         return 0 unless $self->disp->has_command($command,$channel);
243
244         say localtime(time) . " - $msg";
245
246         my $c = NDIRC::DiscordContext->new({
247                         discord_id => $author_name,
248                         channel_id => $channel_id,
249                         channel => $channel,
250                         disp => $self->disp,
251                         model => DB(),
252                         discord => $self->discord,
253                 });
254
255         return $self->disp->run_command($c,$command,$args);
256
257 };
258
259 1;