-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
56 lines (45 loc) · 1.98 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
const request = require('request');
const Discord = require('discord.js');
const client = new Discord.Client();
require('console-stamp')(console, 'HH:MM:ss.l');
if (!process.env.DISCORD_API_KEY || process.env.DISCORD_API_KEY.length <= 0) {
console.log('ERROR: Env variable DISCORD_API_KEY does not exists or is empty!');
process.exit(1);
}
if (!process.env.STEAM_API_KEY || process.env.STEAM_API_KEY.length <= 0) {
console.log('ERROR: Env variable STEAM_API_KEY does not exists or is empty!');
process.exit(1);
}
if (!process.env.STEAM_APPID || process.env.STEAM_APPID.length <= 0) {
console.log('ERROR: Env variable STEAM_APPID does not exists or is empty!');
process.exit(1);
}
const discordApiKey = process.env.DISCORD_API_KEY;
const steamApiKey = process.env.STEAM_API_KEY;
const steamAppId = process.env.STEAM_APPID;
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity('? players playing.', { url: 'http://www.dystopia-game.com', type: 'WATCHING' } );
request(options, callback);
});
const options = {
url: 'https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?key=' + steamApiKey + '&appid=' + steamAppId
};
function callback(error, response, body) {
console.log("Got response with http-code: " + response.statusCode);
if (response.statusCode === 200) {
const jsonObject = JSON.parse(body);
var playerCount = jsonObject.response.player_count;
console.log("retrieved player count is: " + playerCount);
}
if (!error && response.statusCode == 200 && playerCount >= 0) {
client.user.setActivity(playerCount + ' players playing', { url: 'http://www.dystopia-game.com', type: 'WATCHING' } );
} else {
client.user.setActivity('? players playing', { url: 'http://www.dystopia-game.com', type: 'WATCHING' } );
}
}
setInterval(function() {
console.log("Requesting...");
request(options, callback);
}, 1000*300);
client.login(discordApiKey);