-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
executable file
·85 lines (72 loc) · 2.17 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
79
80
81
82
83
84
85
// Require Simple-XMPP and Slack-Node libraries
var xmpp = require("simple-xmpp");
var Slack = require("slack-node");
// Get the config
var config = require("./config.json");
// Function that returns the current time
var currentTime = function() {
return new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
};
// Output text to the terminal
var log = function(message) {
console.log("%s - %s", currentTime(), message);
};
// PHP Style inArray
var inArray = function(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
xmpp.on("online", function(data) {
log("Connected with JID: " + data.jid.user);
log("Settings presence to available");
xmpp.setPresence("online", "I'm right here - maybe..");
});
xmpp.on("error", function(err) {
log(err);
});
xmpp.on("close", function() {
log("Connection closed...");
});
// Handle Ping Broadcasts and messages (Issue is the "chat" function only handles messages where type is "chat", and pings have no type)
xmpp.on("stanza", function(stanza) {
// If it's a message, we handle it just like the library does.
if(stanza.is("message")) {
var body = stanza.getChild("body");
if(body) {
var message = body.getText();
var userString = stanza.attrs.from;
var from = userString.split("/")[0];
log("Received message/ping from ("+ from +"): " + message);
// Pass message on to Discord
slack = new Slack();
slack.setWebhook(config.webhookurl);
if(message.indexOf("to titan ==") > -1) {
slack.webhook({
channel: config.specialWebhooks.titan.webhookChannel,
username: config.specialWebhooks.titan.webhookUsername,
text: "@everyone **" + message + "**"
}, function(err, response) {
log(response);
});
} else {
slack.webhook({
channel: config.webhookChannel,
username: config.webhookUsername,
text: "@everyone **" + message + "**"
}, function(err, response) {
log(response);
});
}
}
}
});
xmpp.connect({
jid: config.jid,
password: config.password,
host: config.host,
port: config.port
});
xmpp.getRoster();