-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
103 lines (91 loc) · 2.84 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const config = require('./config.json');
const Discord = require('discord.js');
const client = new Discord.Client();
const request = require('request');
const querystring = require('querystring');
const oauthTokenUrl = 'https://mtxserv.com/oauth/v2/token?';
const viewerApiUrl =
'https://mtxserv.com/api/v1/viewers/game?ip=' +
config.gameserver_ip +
'&port=' +
config.gameserver_port +
'&type=' +
config.gameserver_type;
const authParams = {
grant_type: 'https://mtxserv.com/grants/api_key',
client_id: config.mtxserv_client_id,
client_secret: config.mtxserv_client_secret,
api_key: config.mtxserv_api_key,
};
const getAccessToken = function(params, callback) {
request(
{
url: oauthTokenUrl + querystring.stringify(params),
json: true,
followRedirect: false,
},
function(error, response, body) {
if (
null !== error ||
response.statusCode !== 200 ||
typeof body.access_token === 'undefined'
) {
console.log(
"Can't retrieve access_token data, check your credentials (" +
response.statusCode +
' ' +
(error !== null ? error : '') +
')',
);
return;
}
callback(body.access_token);
},
);
};
const stringify_viewer_response = function(data) {
const address = data.ip.toUpperCase() + ':' + data.port;
if (!data.is_online) {
return 'Server ' + address + ' is currently offline.';
}
return (
data.params.host_name +
' (' +
data.params.used_slots +
'/' +
data.params.max_slots +
') - ' +
address
);
};
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content !== '!status') {
return;
}
getAccessToken(authParams, function(accessToken) {
request(
{
url: viewerApiUrl + '&access_token=' + accessToken,
json: true,
},
function(error, response, body) {
if (null !== error || response.statusCode !== 200) {
console.log(
"Can't retrieve viewer data (" +
response.statusCode +
' ' +
(error !== null ? error : '') +
')',
);
msg.reply("An error occured, can't retrieve server data");
return;
}
msg.channel.send(stringify_viewer_response(body));
},
);
});
});
client.login(config.discord_bot_token);