]> ruin.nu Git - NDIRC.git/blob - DiscordContext.pm
Discord id in +user
[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         $msg =~ s`<b>(.*?)</b>`**$1**`gi;
58         $msg =~ s`<c(\d+)>(.*?)</c>`*$2*`gi;
59
60         $self->discord->send_message($target, $msg ); # Send the response.
61 }
62
63
64 sub _build_uid {
65         my ($self) = @_;
66
67         my $query = $self->model->prepare(q{
68 SELECT uid FROM users
69 WHERE discord_id = $1
70                 });
71         $query->execute($self->discord_id);
72
73         if (my ($uid) = $query->fetchrow_array){
74                 $query->finish;
75                 return $uid;
76         }
77         return -4;
78 }
79
80 sub valuecolor {
81         shift @_;
82         my $s = $_;
83         $s = $_[1] if $#_ >= 1;
84         $s = "" unless defined $s;
85         return "~~$s~~" if $s eq 'Hostile';
86         return "***$s***" if $s eq 'Friendly';
87         return "*$s*" if $s eq 'Nap' or $s eq 'NAP';
88         return "<b>$s</b>" if $_[0] && $s;
89         return $s;
90 }
91
92 __PACKAGE__->meta->make_immutable;
93
94 1;