-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (100 loc) · 2.59 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
104
105
106
107
108
109
110
111
112
113
114
const config = require('./config.json');
const Discord = require('discord.js');
const client = new Discord.Client();
const request = require('request');
const querystring = require('querystring');
const io = require('socket.io-client');
const oauthTokenUrl = 'https://mtxserv.com/oauth/v2/token?';
const invoiceApiUrl =
'https://mtxserv.com/api/v1/invoices/' + config.invoice_id;
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 streamConsole = function(invoice, channel) {
const socket = io('https://' + invoice.host + ':8181', {
'force new connection': true,
});
socket.on('connected', function() {
socket.emit('start', {
invoiceId: invoice.id,
hash: invoice.security_hash,
});
});
socket.on('initialTextData', function() {
socket.removeAllListeners('continuousTextData');
socket.on('continuousTextData', function(data) {
if (typeof data.text === 'undefined') {
return;
}
channel.send(data.text);
});
});
};
getAccessToken(authParams, function(accessToken) {
request(
{
url: invoiceApiUrl + '?access_token=' + accessToken,
json: true,
},
function(error, response, body) {
if (null !== error || response.statusCode !== 200) {
console.log(
"Can't retrieve invoice (" +
response.statusCode +
' ' +
(error !== null ? error : '') +
')',
);
return;
}
const invoice = body;
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
if (!client.channels.has(config.discord_channel_id)) {
throw new Error(
`Channel with ID ${
config.discord_channel_id
} not found`,
);
}
const channel = client.channels.get(config.discord_channel_id);
console.log(
`Start to stream gameserver console (GSID: #${
invoice.gsid
}) on channel "${channel.name}"`,
);
streamConsole(invoice, channel);
});
client.login(config.discord_bot_token);
},
);
});