-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
208 lines (180 loc) · 9.3 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const Discord = require("discord.js")
const request = require('request')
const rcon = require("./rcon/app.js")
const SourceQuery = require('sourcequery')
const fs = require('fs');
const configdir = './config';
const maxServers = 10;
// Create dir if not exist
if (!fs.existsSync(configdir)){
fs.mkdirSync(configdir);
}
// Create config file if not exist
fs.readdir(configdir, (err, files) => {
try {
if (files.length < 1 )
var writeConfig = '{"debug":false,"token":"","apiSite":4,"apiUrl":"https://full uri here","serverIp":"","serverPort":"28015","enableRcon":"0","rconhost":"","rconport":"","rconpass":"","prefix":"!","roles":["Administrator","admins"],"queueMessage":"currently waiting in queue.","updateInterval":"3"}'
var jsonData = JSON.parse(writeConfig);
fs.writeFile("config/server1.json", JSON.stringify(jsonData, null, 2), 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
console.log("Config file created");
});
} catch (error) {
}
});
fs.readdir(configdir, (err, files) => {
for (var i = 1; i <= files.length; i++){
if (i > maxServers) {
console.log("Max servers is over " + maxServers)
console.log("Please verify max servers and try again")
process.exit()
}
// Functions
function updateActivity() {
if (apiSite == 1) {
require("tls").DEFAULT_ECDH_CURVE = "auto"
request({ url: apiUrl, headers: { json: true, Referer: 'discord-rustserverstatus' }, timeout: 10000 }, function (err, res, body) {
if (!err && res.statusCode == 200) {
const server = JSON.parse(body)
const is_online = server.status
if (is_online == "Online") {
const players = server.players
const maxplayers = server.players_max
if (debug) console.log("Updated rust-servers.info")
status = `${players}/${maxplayers}`
return client.user.setActivity(status, { type: statusType })
} else {
return client.user.setActivity("Offline")
}
}
})
}
if (apiSite == 2) {
request({ url: apiUrl, headers: { Referer: 'discord-rustserverstatus' }, timeout: 10000 }, function (error, response, body) {
if (!error && response.statusCode == 200) {
const server = JSON.parse(body)
const is_online = server.is_online
if (is_online == 1) {
const players = server.players
const maxplayers = server.maxplayers
if (debug) console.log("Updated rust-servers.net")
let status = `${players}/${maxplayers}`
return client.user.setActivity(status, { type: statusType })
} else {
return client.user.setActivity("Offline")
}
}
})
}
if (apiSite == 3) {
require("tls").DEFAULT_ECDH_CURVE = "auto"
request({ url: apiUrl, headers: { json: true, Referer: 'discord-rustserverstatus' }, timeout: 10000 }, function (err, res, body) {
if (!err && res.statusCode == 200) {
const jsonData = JSON.parse(body)
const server = jsonData.data.attributes
const is_online = server.status
if (is_online == "online") {
const players = server.players
const maxplayers = server.maxPlayers
const queue = server.details.rust_queued_players
let status = `${players}/${maxplayers}`
if (typeof queue !== "undefined" && queue != "0") {
status += ` (${queue} ${queueMessage})`
}
if (debug) console.log("Updated from battlemetrics, serverid: " + server.id)
return client.user.setActivity(status, { type: statusType })
} else {
return client.user.setActivity("Offline")
}
}
})
}
if (apiSite == 4) {
if (!serverIp || !serverPort) {
console.log("You have to configure serverIP/port")
process.exit()
} else {
const sq = new SourceQuery(1000) // 1000ms timeout
sq.open(serverIp, serverPort)
sq.getInfo(function(err, info) {
if (err) { return client.user.setActivity("Offline") }
else {
if (debug) { console.log('Server Info: \nIP: %s\nPort: %s\nName: %s\nPlayers: %s/%s', serverIp, serverPort, info.name, info.players, info.maxplayers) }
const players = info.players
const maxplayers = info.maxplayers
let status = `${players}/${maxplayers}`
return client.user.setActivity(status, { type: statusType })
}
})
}
}
}
// End Functions
try {
var config = require("./config/server"+i+".json");
} catch (error) {
}
const client = new Discord.Client()
const updateInterval = (1000 * 60) * 3 || (1000 * 60) * process.env.updateInterval || (1000 * 60) * config.updateInterval
const debug = process.env.debug || config.debug
const apiUrl = process.env.apiUrl || config.apiUrl
const apiSite = process.env.apiSite || config.apiSite
const serverIp = process.env.serverIp || config.serverIp
const serverPort = process.env.serverPort || config.serverPort
const enableRcon = process.env.enableRcon || config.enableRcon
const prefix = process.env.prefix || config.prefix
const roles = process.env.roles || config.roles
const queueMessage = process.env.queueMessage || config.queueMessage
const statusType = process.env.statusType || config.statusType
client.on("ready", () => {
console.log(`Bot has started, with ${client.users.cache.size} users, in ${client.channels.cache.size} channels of ${client.guilds.cache.size} guilds.`)
updateActivity()
setInterval(function () {
updateActivity()
}, updateInterval)
})
if (enableRcon == 1) {
client.on("message", async message => {
if(message.author.bot) return
if(message.content.indexOf(prefix) !== 0) return
var args = message.content.slice(prefix.length).trim().split(/ +/g)
var command = args.shift().toLowerCase()
if(command === "rcon") {
// Checks for discord permission
if(!message.member.roles.cache.some(r=>roles.includes(r.name)) )
return message.reply("Sorry, you don't have permissions to use this!")
var getMessage = args.join(" ")
// Rcon message.
argumentString = `${getMessage}`
// Rcon Config
rconhost = process.env.rconhost || config.rconhost
rconport = process.env.rconport || config.rconport
rconpass = process.env.rconpass || config.rconpass
// Run rcon command.
rcon.RconApp(argumentString, rconhost, rconport,rconpass, debug)
// Send message back to discord that we are trying to relay the command.
message.channel.send(`Trying to relay command: ${getMessage}`)
}
})
}
else if (debug) console.log("Rcon mode disabled")
client.on("guildCreate", guild => {
console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`)
})
client.on("guildDelete", guild => {
console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`)
})
client.on('error', function (error) {
if (debug) console.log(error)
})
process.on('unhandledRejection', error => {
if (error.code == 'TOKEN_INVALID')
return console.log("Error: An invalid token was provided.\nYou have maybe added client secret instead of BOT token.\nPlease set BOT token")
return console.error('Unhandled promise rejection:', error);
});
client.login(process.env.token || config.token)
}
});