-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ferrel.p6
90 lines (79 loc) · 3.11 KB
/
Ferrel.p6
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
use Discord;
use FimFictionStuff;
use Config::Simple;
use NativeCall;
use DateTime::Format;
sub fork is native(Str) returns ulong {*};
sub wait(int32 is rw) is native(Str) returns ulong {*};
sub waitpid(ulong, int32 is rw, int32) is native(Str) returns ulong {*};
say "Started";
my $basedir = $*PROGRAM.dirname ~ "/../";
my $conf = Config::Simple.read("{$basedir}fimstuff.ini", :f('ini'));
my $fim = FimFictionStuff.new(:uadebug($*ERR));
my $disc = Discord::Client.new;
my $qnb = 'Quill & Blade';
die "Can't login to fimfiction" unless $fim.login($conf<FimFiction><login>, $conf<FimFiction><password>);
$disc.login($conf<Discord><login>, $conf<Discord><password>);
my $qnb-channel = $disc.me{$qnb}<qnb-public-sfw>;
#$disc.send-message($qnb-channel, "Hello");
my $last-msg-id = $qnb-channel.last-message-id;
my @imsg;
@imsg = $disc.get-messages($qnb-channel, :after($last-msg-id));
while @imsg.elems > 0 {
@imsg = $disc.get-messages($qnb-channel,:after($last-msg-id));
$last-msg-id = @imsg.tail[0].id;
}
react {
whenever Supply.interval(2) {
#We only want new message
my @msg = $disc.get-messages($qnb-channel, :after($last-msg-id));
for @msg -> $msg {
if $msg.content ~~ /^"!last"$/ {
my %chapter = $fim.chapter-history.tail(1)[0];
my $time = strftime "%A, %B %d at %H:%M", DateTime.new(%chapter<time>.Int);
my $smsg = "The lastest update ($time) is : %chapter<title> (%chapter<wordcount> words) of %chapter<story> by %chapter<author> - http://www.fimfiction.net%chapter<link>";
$disc.send-message($qnb-channel, $smsg);
}
if $msg.content ~~ /^"!last-blog"$/ {
my %blog = $fim.blog-history.tail(1)[0];
my $time = strftime "%A, %B %d at %H:%M", DateTime.new(%blog<time>.Int);
my $smsg = "The last blog post ($time) is : %blog<title> by %blog<author> - http://www.fimfiction.net%blog<link>";
$disc.send-message($qnb-channel, $smsg);
}
#if $msg.content ~~ /^"!last"\s+()/ {
# my $smsg = "The lastest update for %chapter<story> ($time) is : %chapter<title> (%chapter<wordcount> words) - http://www.fimfiction.net%chapter<link>";
#}
LAST {
$last-msg-id = $msg.id;
}
}
}
whenever Supply.interval(120) {
# my ulong $pid = fork;
# if $pid == 0 {
# say "child $pid";
try {
if $fim.look-for-new-chapters {
my %chapter = $fim.chapter-history.tail(1)[0];
my $message = "%chapter<author> posted a new chapter of %chapter<story> : %chapter<title> (%chapter<wordcount> words) - https://www.fimfiction.net%chapter<link>";
say $message;
$disc.send-message($qnb-channel, $message);
}
if $fim.look-for-new-blogs {
my %blog = $fim.blog-history.tail(1)[0];
my $message = "%blog<author> posted a new blog %blog<title> - https://www.fimfiction.net%blog<link>";
say $message;
$disc.send-message($qnb-channel, $message);
}
}
CATCH {
}
# } else {
# my int32 $status;
# my int32 $p = 0;
# say "parent $pid";
# waitpid($pid, $status, $p);
# say "Stop waiting";
# }
}
}