-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
127 lines (114 loc) · 3.5 KB
/
index.js
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
const Plex = require("plex-api");
const Irc = require("irc");
const config = require("./config.json");
const owner = config.owner;
const commands = require('./lib/cmds/public');
const pmCommands = require('./lib/cmds/private');
const queryLimit = config.queryLimit || 5;
const queryInterval = config.queryInterval || 180;
var resetTimeout;
var limitDate;
var queryCount = 0;
var curChannel = config.irc.channel;
const resetLimit = () => queryCount = 0;
const pClient = new Plex({
hostname: config.plex.server,
username: config.plex.username || undefined,
password: config.plex.password || undefined
});
const ircClient = new Irc.Client(
config.irc.server,
config.irc.nick, {
channels: [config.irc.channel],
realName: "PlexBot",
userName: "plexbot",
port: config.irc.port || (config.irc.secure ? 6697 : 6667),
secure: config.irc.secure || false,
autoRejoin: true,
floodProtection: true,
floodProtectionDelay: 800,
}
);
pClient.query("/").then(function(res) {
}, function(err) {
throw err;
}).then(function() {
// Owner-only private messages
const privMsg = (from, msg) => {
if(from == owner) {
if(msg.indexOf("join ") == 0) {
var newChannel = msg.split("join ")[1].trim();
var result = pmCommands.join(ircClient, curChannel, newChannel);
if(!result) {
ircClient.say(owner, "Invalid channel name specified");
}
else {
curChannel = newChannel;
}
}
else if(msg.indexOf("speak ") == 0) {
ircClient.say(curChannel, msg.split("speak ")[1].trim());
}
}
};
// All channel messages
const chanMsg = function(from, to, msg) {
if(true) {
if(queryCount >= queryLimit) {
let limitMsg = "Rate limit reached.";
if(resetTimeout == null) {
limitDate = new Date();
setTimeout(resetLimit, queryInterval * 1000);
}
let secondsLeft = Math.ceil(((limitDate - new Date()) / 1000) + queryInterval);
limitMsg += ` Back in ${secondsLeft} seconds.`
ircClient.say(curChannel, limitMsg);
return;
}
else {
queryCount++;
resetTimeout = null;
}
}
if(msg == ".plabout") {
commands.about(pClient, owner).then((res) => ircClient.say(curChannel, res));
}
else if(msg == ".pltotal") {
commands.total(pClient).then(function(res) {
ircClient.say(curChannel, res);
});
}
else if(msg == ".plhelp") {
ircClient.say(curChannel, commands.help());
}
else if(msg == ".plrecent") {
commands.recent(pClient, ircClient, curChannel).then(function(res) {
//TODO do this really
//ircClient.say(curChannel, res);
});
}
else if(msg.indexOf(".plsearch") == 0) {
commands.search(pClient, msg).then(function(res) {
ircClient.say(curChannel, res);
}, function(msg) {
ircClient.say(curChannel, msg);
});
}
else if(msg.indexOf(".plactsearch") == 0) {
commands.searchActor(pClient, ircClient, curChannel, msg.split(".plactsearch")[1].trim()).then(function(res) {
ircClient.say(curChannel, res);
});
}
else if(msg == ".plresetlimit" && from == owner) {
ircClient.say(curChannel, "Rate limit reset");
queryCount = 0;
resetTimeout = null;
}
else if(msg == ".plgtfo" && from == owner) {
ircClient.disconnect("requested");
process.exit(0);
}
};
ircClient.addListener("message", chanMsg);
ircClient.addListener("pm", privMsg);
});