]> ruin.nu Git - NDIRC.git/blob - DiscordContext.pm
1d7cb58f472b482b6e6af84dc3a89d76311bd78d
[NDIRC.git] / DiscordContext.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
20 package NDIRC::DiscordContext;
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use Moose;
26
27 use Set::Object ();
28
29 has discord_id => (
30         is => 'ro',
31         isa => 'Str',
32         required => 1
33 );
34
35 has channel_id => (
36         is => 'ro',
37         isa => 'Str',
38         required => 1
39 );
40
41 has channel => (
42         is => 'ro',
43         isa => 'Str',
44         required => 1
45 );
46
47 has roles => (
48         is => 'ro',
49         isa => 'Object',
50         lazy_build => 1
51 );
52
53 has uid => (
54         is => 'ro',
55         isa => 'Int',
56         lazy_build => 1
57 );
58
59 has disp => (
60         is => 'ro',
61         isa => 'Object',
62         required => 1
63 );
64
65 has model => (
66         is => 'ro',
67         isa => 'Object',
68         required => 1
69 );
70
71 has discord => (
72         is => 'ro',
73         isa => 'Object',
74         required => 1
75 );
76
77 sub assert_user_roles {
78         my ($self,@roles) = @_;
79         return 1 unless @roles;
80
81         my $need = Set::Object->new(@roles);
82
83         if ($self->roles->superset($need)){
84                 return 1;
85         }
86
87         die "Access denied";
88 }
89
90 sub check_user_roles {
91         my ($self,@roles) = @_;
92
93         local $@;
94         eval { $self->assert_user_roles(@roles) };
95 }
96
97 sub reply {
98         my ($self,$msg) = @_;
99
100         $self->message($self->channel_id, $msg);
101 }
102
103 sub message {
104         my ($self, $target, $msg) = @_;
105
106         $msg =~ s`<b>(.*?)</b>`**$1**`gi;
107         $msg =~ s`<c(\d+)>(.*?)</c>`*$2*`gi;
108
109         $self->discord->send_message($target, $msg ); # Send the response.
110 }
111
112 sub command {
113         my ($self,@command) = @_;
114
115 }
116
117 sub intel_log {
118         my ($c,$planet, $message) = @_;
119         my $log = $c->model->prepare_cached(q{
120 INSERT INTO forum_posts (ftid,uid,message) VALUES(
121         (SELECT ftid FROM planets WHERE pid = $3),$1,$2)
122                 });
123         $log->execute($c->uid,$message,$planet);
124 }
125
126 sub def_log {
127         my ($c,$call, $message) = @_;
128         my $log = $c->model->prepare(q{
129 INSERT INTO forum_posts (ftid,uid,message) VALUES(
130         (SELECT ftid FROM calls WHERE call = $3),$1,$2)
131                 });
132         $log->execute($c->uid,$message,$call);
133 }
134
135 sub _build_roles {
136         my ($self) = @_;
137
138         my $query = $self->model->prepare(q{
139 SELECT role FROM group_roles
140 WHERE gid IN (SELECT gid FROM groupmembers JOIN users USING (uid)
141         WHERE discord_id = $1)
142                 });
143         $query->execute($self->discord_id);
144
145         my @roles;
146         while (my $group = $query->fetchrow_hashref){
147                 push @roles,$group->{role};
148         }
149         return Set::Object->new(@roles);
150 }
151
152 sub _build_uid {
153         my ($self) = @_;
154
155         my $query = $self->model->prepare(q{
156 SELECT uid FROM users
157 WHERE discord_id = $1
158                 });
159         $query->execute($self->discord_id);
160
161         if (my ($uid) = $query->fetchrow_array){
162                 $query->finish;
163                 return $uid;
164         }
165         return -4;
166 }
167
168 sub valuecolor {
169         shift @_;
170         my $s = $_;
171         $s = $_[1] if $#_ >= 1;
172         $s = "" unless defined $s;
173         return "~~$s~~" if $s eq 'Hostile';
174         return "***$s***" if $s eq 'Friendly';
175         return "*$s*" if $s eq 'Nap' or $s eq 'NAP';
176         return "<b>$s</b>" if $_[0] && $s;
177         return $s;
178 }
179
180 1;