-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordbot
executable file
·132 lines (106 loc) · 4.21 KB
/
discordbot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
package Bot;
use Moo;
use strictures 2;
use Mojo::Discord;
use namespace::clean;
use JSON;
has token => ( is => 'ro' );
has name => ( is => 'ro', default => 'PPNL Brug' );
has url => ( is => 'ro', default => 'https://piratenpartij.nl' );
has version => ( is => 'ro', default => '1.0' );
has reconnect => ( is => 'rw', default => 1 );
has loglevel => ( is => 'ro', default => 'error' );
has logdir => ( is => 'ro', default => '/tmp' );
has discord => ( is => 'lazy', builder => sub {
my $self = shift;
Mojo::Discord->new(
token => $self->token,
name => $self->name,
url => $self->url,
version => $self->version,
reconnect => $self->reconnect,
loglevel => $self->loglevel,
logdir => $self->logdir,
)
});
sub start
{
my ($self,$cfg) = @_;
my $tel = $cfg->{telegram} if defined $cfg->{telegram};
my $irc = $cfg->{irc} if defined $cfg->{irc};
my $mat = $cfg->{matrix} if defined $cfg->{matrix};
my $sig = $cfg->{signal} if defined $cfg->{signal};
my $mm = $cfg->{mattermost} if defined $cfg->{mattermost};
my $dis = $cfg->{discord} if defined $cfg->{discord};
my $userurl = 'https://discordapp.com/api/v6/users/';
$self->discord->gw->on('READY' => sub {
my ($gw, $hash) = @_;
print localtime(time) . " Connected to Discord.\n";
});
$self->discord->gw->on('MESSAGE_CREATE' => sub {
my ($gw, $msg) = @_;
my $text = $msg->{'content'};
my $channel_id = $msg->{'channel_id'};
my $user = $msg->{'author'}{'username'};
my $pre = "[dis] $user: ";
if ($msg->{'channel_id'} eq $dis->{'channel_id'} and $msg->{author}->{id} ne $dis->{user_id}) {
my $reply = (defined $msg->{referenced_message}{content}) ?
"(reply to: $msg->{referenced_message}{content}) " : "";
# building users id to name hash
$dis->{users}{$msg->{author}{id}} = $user;
my @mentions = $text =~ m/<[\@\&\!]+([0-9]+)>/;
for my $m (@mentions) {
unless (defined $dis->{users}{$m}) {
eval {
my $str = qx( curl -s -XGET -H "Content-Type: application/json" -H "Authorization: Bot $dis->{token}" "$userurl$m" );
my $j = JSON->new->allow_nonref;
my $json = $j->decode($str);
$dis->{users}{$m} = $json->{username} if defined $json->{username};
};
}
$text =~ s/<[\@\&\!]+$m>/\@$dis->{users}{$m}/g if defined $dis->{users}{$m};
}
if ($text) {
Hermod::relay2tel($tel,"$pre$reply$text\n");
Hermod::relay2irc("$reply$text\n",$irc,$pre);
Hermod::relay2mm("$pre$reply$text\n",$mm);
Hermod::relay2mtx("$pre$reply$text\n",$mat);
Hermod::relay2sig("$pre$reply$text\n",$sig);
}
if (defined $msg->{attachments} and ref $msg->{attachments} eq "ARRAY") {
$pre = "[dis] **$user sends file ";
for my $att (@{$msg->{attachments}}) {
$text = "$att->{filename} $att->{url}\n";
Hermod::relay2tel($tel,"$pre$text\n");
Hermod::relay2irc("$text\n",$irc,$pre);
Hermod::relay2mm("$pre$text\n",$mm);
Hermod::relay2mtx("$pre$text\n",$mat);
Hermod::relay2sig("$pre$text\n",$sig);
}
}
}
$self->discord->send_ack_dm($msg->{channel_id}, $msg->{id}, $user, 'success!') if ( lc $msg eq 'test' );
});
$self->discord->init();
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
}
1;
use Bot;
use Hermod;
use TOML;
use JSON;
open my $fh, '<', "/etc/hermod.toml" or die "error opening configuration $!";
my ($cfg, $e) = from_toml do { local $/; <$fh> };
unless ($cfg) {
die "Error parsing toml: $e";
}
my $dis = $cfg->{discord} if defined $cfg->{discord};
my $bot = Bot->new(
token => $dis->{token},
logdir => $dis->{logdir},
);
$bot->start($cfg);