-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (48 loc) · 1.41 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
var express = require('express');
var login = require('facebook-chat-api');
var port = 8080;
var app = express();
var config = require('./config.js');
// Sets up static app
app.use(express.static(__dirname + '/public'));
var email = config.email;
var pass = config.pass;
var commandJSON = require('./commands.json');
var commands = commandJSON.map(function(cmd) {
return require('./' + cmd['path']);
});
login({email: email, password: pass}, loginCallback);
// parse messages for handling
function loginCallback(err, api) {
if(err) return console.error(err);
api.listen(function callback(err, message) {
// Comand defined as starting with /
if(isCommand(message)) {
var commandString = message.body.slice(1);
var trigger = commandString.substring(0, endOfCmd(commandString));
commands.forEach(function(cmd, index) {
if(trigger == commandJSON[index]['trigger']) {
cmd.trigger(commandString.slice(trigger.length+1), api, message);
}
});
}
});
}
// determine the end of the command
function endOfCmd(cmd) {
if(cmd.indexOf(' ') > 0) {
return cmd.indexOf(' ');
} else {
return cmd.length;
}
}
// determine if a received message is a command
function isCommand(message) {
if (!(message && message.body)) {
return false;
} else {
return message.body.startsWith('/');
}
}
app.listen(port);
console.log("listening on port: " + port + "...");