This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
84 lines (71 loc) · 2.45 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
const name = 'IDA-DC-Bot';
const DCkeys = require('../private/DCkeys.inc.js');
let disctoken = DCkeys.token();
let discchannelid = DCkeys.channelid();
let apikeys = DCkeys.apikeys();
var http = require('http');
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log(name + ' connected!');
});
client.login(disctoken);
http.createServer(function (request, res) {
if (request.url === '/favicon.ico') {
res.writeHead(200, {'Content-Type': 'image/x-icon'} );
res.end();
return;
}
if (request.url === '/api') {
if(request.method === 'POST') {
var body = '';
// var post = '';
request.on('data', function (data) {
body += data;
if (body.length > 1e6) {
request.connection.destroy();
}
});
request.on('end', function () {
// post = qs.parse(body);
var obj = JSON.parse(body);
var event = obj['Event'];
var cmdr = obj['CMDR'];
var combatrank = obj['CombatRank'];
var isplayer = obj['IsPlayer'];
var timestamp = obj['timestamp'];
var interdictor = obj['Interdictor'];
var submitted = obj['Submitted'];
var submittedtext = '';
if (submitted == true) {
var submittedtext = 'true';
} else {
var submittedtext = 'false';
}
console.log('-----------');
console.log(obj['key']);
console.log('-----------');
if (apikeys.includes(obj['key'])) {
console.log('sending discord message');
var generalChannel = client.channels.get(discchannelid);
generalChannel.send("Distress Call: " + cmdr + " has been interdicted by " + interdictor + " (rank: " + combatrank + ", submitted: " + submittedtext + ")");
res.writeHead(200, 'success' );
res.write('200: Distress Call received and sent on discord channel'); //write a response to the client
res.end(); //end the response
} else {
res.writeHead(402, 'Incorrect API key' );
res.write('402: Incorrect API key');
res.end();
}
});
} else {
res.writeHead(401, 'No POST data received' );
res.write('401: No POST data received');
res.end();
}
} else {
res.writeHead(404, 'Page not found' );
res.write('Nothing to see here, move along...'); //write a response to the client
res.end(); //end the response
}
}).listen(3001);