-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (53 loc) · 1.78 KB
/
Copy pathserver.js
File metadata and controls
74 lines (53 loc) · 1.78 KB
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
var Discord = require("discord.js");
var http = require('http');
var _ = require('lodash');
var fs = require('fs');
var bot = new Discord.Client();
var config = JSON.parse(fs.readFileSync(__dirname + "/config.json"));
bot.on("ready", function () {
console.log("Ready to begin! Serving in " + bot.channels.length + " channels");
});
bot.on("disconnected", function () {
console.log("Disconnected!");
process.exit(1); //exit node.js with an error
});
bot.on("message", function (msg) {
if (msg.content.substring(0, 4) === "ping") {
//send a message to the channel the ping message was sent in.
bot.sendMessage(msg.channel, "pong!");
//alert the console
console.log("pong-ed " + msg.sender.username);
}
});
function handleRequest(req, response){
var fullBody = '';
req.on('data', function(chunk) {
fullBody += chunk.toString();
});
req.on('end', function() {
var json = JSON.parse(fullBody);
if (json.commits) {
repo = _.find(config.repos, function(repo) {
return json.repository.full_name.includes(repo.prefix);
});
if (repo) {
var channel = _.find(bot.channels, function(channel) {
return repo.channel == channel.name && repo.server == channel.server.name
});
}
if (channel) {
msg = `[${json.repository.full_name}] ${json.commits.length} new commits:\n`;
json.commits.map(function(commit) {
msg += `${commit.id.substr(0,6)}: ${commit.message} - ${commit.author.name}\n`;
})
bot.sendMessage(channel, msg);
}
}
})
response.end('Done.');
}
var server = http.createServer(handleRequest);
server.listen(80, "0.0.0.0", function(){
console.log("Server listening on port 80");
});
bot.login(config.auth.username, config.auth.password);