]> ruin.nu Git - NDIRC.git/blob - DiscordContext.pm
Universal scan parsing
[NDIRC.git] / DiscordContext.pm
1 #**************************************************************************
2 #   Copyright (C) 2019 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 use namespace::autoclean;
27 extends "NDIRC::Context";
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 discord => (
42         is => 'ro',
43         isa => 'Object',
44         required => 1
45 );
46
47
48 sub reply {
49         my ($self,$msg) = @_;
50
51         $self->message($self->channel_id, $msg);
52 }
53
54 sub message {
55         my ($self, $target, $msg) = @_;
56
57         $self->bot->discordMessage($target, $msg ); # Send the response.
58 }
59
60 sub replyId {
61         my $self = shift;
62         return "D-".$self->channel_id;
63 }
64
65 sub _build_uid {
66         my ($self) = @_;
67
68         my $query = $self->model->prepare(q{
69 SELECT uid FROM users
70 WHERE discord_id = $1
71                 });
72         $query->execute($self->discord_id);
73
74         if (my ($uid) = $query->fetchrow_array){
75                 $query->finish;
76                 return $uid;
77         }
78         return -4;
79 }
80
81 sub valuecolor {
82         shift @_;
83         my $s = $_;
84         $s = $_[1] if $#_ >= 1;
85         $s = "" unless defined $s;
86         return "~~$s~~" if $s eq 'Hostile';
87         return "***$s***" if $s eq 'Friendly';
88         return "*$s*" if $s eq 'Nap' or $s eq 'NAP';
89         return "<b>$s</b>" if $_[0] && $s;
90         return $s;
91 }
92
93 __PACKAGE__->meta->make_immutable;
94
95 1;