const { Client, Intents, Message, ActivityType} = require('discord.js'); const util = require('minecraft-server-util'); const {EmbedBuilder} = require('discord.js'); const { rename } = require('fs'); const wait = require('util').promisify(setTimeout); const options = { timeout: 1000 * 5, // timeout in milliseconds enableSRV: true // SRV record lookup }; const prefix = "!mcstatus"; //prefix const client = new Client({ intents: [ "Guilds", "GuildMessages", "MessageContent" ] }); client.on('ready',() => { console.log('Status Check Online');// so you know the bot started //if you don't want the activity status comment out the line below using // client.user.setPresence({ activities: [{ name:'Booting sequence...', type: ActivityType.Watching }], status: 'idle' });// just a little feature to display whatever server is under server_ip setTimeout(function(){ client.user.setPresence({ activities: [{ name: `Waiting for response...`, type: ActivityType.Watching }], status: 'dnd'}); }, 1000*20) }); const server_ip = "held-corp.craft.playit.gg"; //replace with real ip for the server const server_port = 25565; // replace with the real port also thanks dashcruft for testing the code client.on('messageCreate', (message) => { if (!message.content.startsWith(prefix) || message.author.bot) return; if(message.content.startsWith(prefix)){ util.status(server_ip, server_port, options,) .then((result) => { client.user.setPresence({ activities: [{ name: `${server_ip}`, type: ActivityType.Watching }], status: 'Online' }); const string1 = JSON.stringify(result);// turn the object into a string const string = JSON.parse(string1);// make the string parsable const embed = new EmbedBuilder() .setColor("#00008B") .setTitle("Status Server") .setDescription(`Mai jos sunt informatii in legatura cu serverul de minecraft \n **IP Server:** ${server_ip} \n **Port Server:** ${server_port}`) .addFields( {name:"Versiune", value: `${string.version.name}`}, //{name:"Server Protocol Version", value:`${string.version.protocol}`}, {name:"Players Online", value:`${string.players.online}`}, {name:"Max Players", value:`${string.players.max}`}, {name:"Message of the day", value:`${string.motd.clean}`}, {name:"Latency", value:`${string.roundTripLatency}`}, ) .setTimestamp() message.channel.send({embeds: [embed]})// send the embed }) .catch((error) => { console.log(error);// if the server was unable to be pinged or something else happened client.user.setPresence({ activities: [{ name: `Server is offline`, type: ActivityType.Watching }], status: 'dnd' }); const embed = new EmbedBuilder() .setColor("#FF0000") .setTitle("Status necunoscut") .setDescription(`Serverul este offline, se restarteaza sau are probleme de conexiune `) .setTimestamp() message.channel.send({embeds: [embed]})// send the embed })}}); function intervalFunc() { util.status(server_ip, server_port, options,) .then((result) => { }, console.log('Checking Server Status'), client.user.setPresence({ activities: [{ name: `Checking ${server_ip}`, type: ActivityType.Watching }], status: 'idle' }) ) .then( setTimeout(function(){ client.user.setPresence({ activities: [{ name: `${server_ip}`, type: ActivityType.Watching }], status: 'dnd'}); }, 1000*5) ) .catch((error) => { console.log(error); console.log('SERVER OFFLINE'), setTimeout(function(){ client.user.setPresence({ activities: [{ name: 'Server is offline', type: ActivityType.Watching }], status: 'dnd'}); }, 1000*3) //client.user.setPresence({ activities: [{ name: 'Server is offline', type: ActivityType.Watching }], status: 'dnd'}); } ) } setInterval(intervalFunc, 1000*60*1); client.login("");//replace with your bot token //you may need to give the bot the privileged gateway intents //this was inspired by this gist https://gist.github.com/vegeta897/e3d2fcd4b661bd9d021c397e7505be05 (this is outdated, do not use it)