// #54F018 (5566487) const config = require("./mapinfo.json"); const discord = require("discord.js"); const srcds = require("srcds-info"); const mysql = require("mysql"); const rcon = require("srcds-rcon"); const client = new discord.Client(); const database = mysql.createConnection({ host : "", user : "", password : "", database : "" }); const remotecon = rcon({ address: '', password: '' }); var recentStats = []; var currentStats = []; var recentMessage = 0; var updateMessage = false; function loadServerStats() { if (config.servers) { config.servers.map((server, index) => { database.query("SELECT serverName, currentMap, numPlayers, maxPlayers FROM servers WHERE id=" + index, (error, result) => { if (error) { console.error(error); return; } if (result) { currentStats[index] = []; currentStats[index].serverName = result[0].serverName; currentStats[index].currentMap = result[0].currentMap; currentStats[index].numPlayers = result[0].numPlayers; currentStats[index].maxPlayers = result[0].maxPlayers; } }); }); } } function updateServerStats() { if (config.servers) { config.servers.map((server, index) => { var query = srcds(server.adress, server.port); query.info((error, result) => { query.close(); if (error) { if (error == "Error: Request timed out") return; console.error(error); return; } if (result) { recentStats[index] = []; recentStats[index].abbr = server.abbr; recentStats[index].adress = server.adress; recentStats[index].port = server.port; recentStats[index].serverName = result.serverName; recentStats[index].currentMap = result.map; recentStats[index].numPlayers = result.numPlayers; recentStats[index].maxPlayers = result.maxPlayers; } }); }); if (recentStats.length) { recentStats.map((stats, index) => { database.query("UPDATE servers SET serverName=?, currentMap=?, numPlayers=?, maxPlayers=? WHERE id=" + index, [stats.serverName, stats.currentMap, stats.numPlayers, stats.maxPlayers]); if (!currentStats[index] || currentStats[index].serverName != stats.serverName) updateMessage = true; if (!currentStats[index] || currentStats[index].currentMap != stats.currentMap) updateMessage = true; if (!currentStats[index] || currentStats[index].numPlayers != stats.numPlayers) updateMessage = true; if (!currentStats[index] || currentStats[index].maxPlayers != stats.maxPlayers) updateMessage = true; currentStats[index] = stats; }); } if (updateMessage) { updateMessage = false; var embedMessage = new discord.RichEmbed(); if (currentStats.length) { currentStats.map((stats, index) => { embedMessage.addField("__" + stats.serverName + "__", "**" + stats.currentMap + " (" + stats.numPlayers + "/" + stats.maxPlayers + ")**");//steam://connect/" + stats.adress + ":" + stats.port); }); } var channel = client.channels.get(config.channel_serverinfo); if (channel) { var infoMessage = channel.messages.get(recentMessage); if (infoMessage) { infoMessage.edit("", {embed: embedMessage}); } else { console.log("Couldn't find previous message, find another bot message."); var foundMessage = false; channel.fetchMessages().then(messages => { console.log("\tFound " + messages.size + " previous messages"); Promise.all(messages.map((curMessage, index) => { if (!foundMessage && curMessage.client == client) { console.log("\t\tFound previous bot message"); foundMessage = true; recentMessage = curMessage.id; } })).then(() => { if (!foundMessage) { console.log("\t\tUnable to find previous bot message, sending new message!"); channel.send("", {embed: embedMessage}).then(message => {recentMessage = message.id}); } }); }); } } } } } function updateClientGame() { } client.on("ready", () => { loadServerStats(); client.user.setActivity('Zombie Escape'); setInterval(updateClientGame, 300000); setInterval(updateServerStats, 10000); }); client.on('message', message => { var rcon_channel = client.channels.get(config.channel_rcon); if (message.channel.id === config.channel_adminchat) { if (!(message.author.bot)){ remotecon.connect().then(() => { remotecon.command('sm_printtoadminchat "{0}" "{1}"'.formatUnicorn(message.author.username, message.content)).then(() => { remotecon.disconnect() }); }); } // } else if (message.channel.id === config.channel_livechat) { // if (!(message.author.bot)){ // remotecon.connect().then(() => { // remotecon.command('sm_printtoallchat "{0}" "{1}"'.formatUnicorn(message.author.username, message.content)).then(() => { // remotecon.disconnect() // }); // }); // } } else if (message.channel.id === config.channel_rcon) { if (!(message.author.bot)){ remotecon.connect().then(() => { remotecon.command(message.content).then(response => rcon_channel.send(`${response}`, {code: true, split: true})).then(() => { remotecon.disconnect() }); }); } } else if (message.isMentioned(client.user)) { message.reply('Hello!'); } else if (message.channel.id === config.channel_tech) { if (!(message.author.bot)){ if (message.content.startsWith("!activity")){ message.channel.send('Changing activity to "{0}"'.formatUnicorn(message.content.substr(10))); client.user.setActivity(message.content.substr(10)); } } } }); String.prototype.formatUnicorn = String.prototype.formatUnicorn || function () { "use strict"; var str = this.toString(); if (arguments.length) { var t = typeof arguments[0]; var key; var args = ("string" === t || "number" === t) ? Array.prototype.slice.call(arguments) : arguments[0]; for (key in args) { str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), args[key]); } } return str; }; client.login("");