-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
78 lines (63 loc) · 2.16 KB
/
bot.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
// APIs & Packages
var login = require("facebook-chat-api");
require('./static.js')();
require('./commands.js')();
// Change this later to be a list of references to
// function objects. Override toString() to
// represent command title, not source code.
// Parse command list that way.
// Message spaceout function
// Used to enforce order
function spaceout(api, messages, delay) {
for(i = 0; i < messages.length; i++){
(function(message) {
setTimeout((function() {
api.sendMessage(message, THREAD_ID);
}), delay*i);
}(messages[i]))
}
};
// Login and listen
login({email: EMAIL, password: PASSWORD}, function callback(err, api) {
// Error handling
if(err) return console.error(err);
intro = [BOTNAME + ", at your service.", "To turn me on, type '" + PREFIX + " on'", "For help, type '" + PREFIX + " help.'"]
spaceout(api, intro, 500);
// Basic message responder
var stopListening = api.listen(function(error, message) {
// Message received from host group chat
if(message['threadID'] === THREAD_ID) {
console.log(message);
// Detect & parse commands
var params;
// Bot called by PREFIX, command issued
if(message.body.startsWith(PREFIX)) {
params = message.body.split(" ").slice(1); // split by spaces, ignore PREFIX
} else {
params = null;
}
// If no command issued, params = null
// If command issued, try to execute it
if(params) {
var command = params[0];
if(COMMANDS[command]) {
var commandObj = COMMANDS[command];
console.log(message);
commandObj.run(api, params, message);
} else {
api.sendMessage('Command "' + command + '" not recognized. ', message.threadID);
api.sendMessage("For help and a list of commands, type '" + PREFIX + " help'.", message.threadID);
}
}
// If no command issued, perform:
// Persistence checks & activity
else {
// Persistence by message (implement time later)
for (num in PERSISTENCE) {
enabled = PERSISTENCE[num];
COMMANDS[enabled].run(api, params, message);
console.log("CHECKED");
}
}
}});
});