diff --git a/commands/list.js b/commands/list.js index 9ebded4..22ffe73 100644 --- a/commands/list.js +++ b/commands/list.js @@ -10,7 +10,9 @@ module.exports.config = { }; module.exports.run = async (bot, message) => { - const { server, config } = bot + const { server, config } = bot, + warn = c.keyword('yellow').bold, + warns = config.settings.warns if(!server.work) return @@ -20,13 +22,13 @@ module.exports.run = async (bot, message) => { icon = server.icon ? server.icon : message.guild.icon if(server.type === 'java') { - util.status(ip1, { port: port1 }) - .then((response) => { + util.status(ip1, port1) + .then((result) => { const serverEmbed = new Discord.MessageEmbed() .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) .setDescription(`:white_check_mark: **ONLINE**`) .addFields( - { name: "Players", value: `**${response.onlinePlayers}**/**${response.maxPlayers}**` + (response.samplePlayers ? "\n\`\`\`" + response.samplePlayers.map(p => ` ${p.name} `).join('\n') + "\`\`\`":"") , inline: false }, + { name: "Players", value: `**${result.players.online}**/**${result.players.max}**` + (result.players.sample ? "\n\`\`\`" + result.players.sample.map(p => ` ${p.name} `).join('\n') + "\`\`\`":"") , inline: false }, ) .setColor(config.embeds.color) message.channel.send({ embeds: [serverEmbed] }); @@ -38,10 +40,11 @@ module.exports.run = async (bot, message) => { .setColor(config.embeds.error) message.channel.send({ embeds: [errorEmbed] }); - throw error; + if (warns) console.log(warn(`Error when using command ${module.exports.config.name}! Error:\n`) + error) }); } else { //Doesn't work for bedrock edition, sorry. + message.reply('Sorry, but this function is not working for Bedrock servers.') } }; \ No newline at end of file diff --git a/commands/status.js b/commands/status.js index 33b0c87..6389c51 100644 --- a/commands/status.js +++ b/commands/status.js @@ -1,6 +1,7 @@ const { commands, settings } = require("../config"); const util = require('minecraft-server-util'); const Discord = require('discord.js'); +const c = require('chalk') module.exports.config = { name: "status", //Name of command - RENAME THE FILE TOO!!! @@ -10,7 +11,10 @@ module.exports.config = { }; module.exports.run = async (bot, message) => { - const { server, config } = bot + const { server, config } = bot, + settings = config.settings, + warn = c.keyword('yellow').bold, + warns = config.settings.warns if(!server.work) return @@ -20,13 +24,10 @@ module.exports.run = async (bot, message) => { icon = server.icon ? server.icon : message.guild.icon if(server.type === 'java') { - util.status(ip1, { port: port1 }) - .then((response) => { - const - description1 = response.description.descriptionText, - description11 = description1.toString().replace(/\u00A7[0-9A-FK-OR]/ig, '') - - const versionOriginal = response.version + util.status(ip1, port1) + .then((result) => { + const description11 = result.motd.clean + const versionOriginal = result.version.name if(settings.split) { if(versionOriginal.includes("Spigot")) { var versionAdvanced = versionOriginal.replace("Spigot", "") @@ -45,7 +46,7 @@ module.exports.run = async (bot, message) => { { name: "Description", value: `${description11}` , inline: false }, { name: "IP Address", value: `\`${server.ip}\`:\`${server.port}\`` , inline: false }, { name: "Version", value: `JAVA **${version}**` , inline: true }, - { name: "Players", value: `**${response.onlinePlayers}**/**${response.maxPlayers}**` , inline: true }, + { name: "Players", value: `**${result.players.online}**/**${result.players.max}**` , inline: true }, ) .setColor(config.embeds.color) message.channel.send({ embeds: [serverEmbed] }); @@ -57,37 +58,35 @@ module.exports.run = async (bot, message) => { .setColor(config.embeds.error) message.channel.send({ embeds: [errorEmbed] }); - throw error; + if (warns) console.log(warn(`Error when using command ${module.exports.config.name}! Error:\n`) + error) }); } else { - util.statusBedrock(ip1, { port: port1 }) - .then((response) => { - const - description1 = response.motdLine1.descriptionText + '\n' + response.motdLine2.descriptionText, - description11 = description1.toString().replace(/\u00A7[0-9A-FK-OR]/ig, '') + util.statusBedrock(ip1, port1) + .then((result) => { + const description11 = result.motd.clean - const version = response.version + const version = result.version.name const serverEmbed = new Discord.MessageEmbed() - .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) - .setDescription(`:white_check_mark: **ONLINE**`) - .addFields( - { name: "Description", value: `${description11}` , inline: false }, - { name: "IP Address", value: `\`${server.ip}\`:\`${server.port}\`` , inline: false }, - { name: "Version", value: `BEDROCK **${version}**` , inline: true }, - { name: "Players", value: `**${response.onlinePlayers}**/**${response.maxPlayers}**` , inline: true }, - ) - .setColor('#77fc03') + .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) + .setDescription(`:white_check_mark: **ONLINE**`) + .addFields( + { name: "Description", value: `${description11}` , inline: false }, + { name: "IP Address", value: `\`${server.ip}\`:\`${server.port}\`` , inline: false }, + { name: "Version", value: `BEDROCK **${version}**` , inline: true }, + { name: "Players", value: `**${result.players.online}**/**${result.players.max}**` , inline: true }, + ) + .setColor('#77fc03') message.channel.send({ embeds: [serverEmbed] }); }) .catch((error) => { const errorEmbed = new Discord.MessageEmbed() - .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) - .setDescription(':x: **OFFLINE**') - .setColor('#f53636') + .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) + .setDescription(':x: **OFFLINE**') + .setColor('#f53636') message.channel.send({ embeds: [errorEmbed] }); - throw error; + if (warns) console.log(warn(`Error when using command ${module.exports.config.name}! Error:\n`) + error) }); } diff --git a/commands/version.js b/commands/version.js index 8a92296..6ba089f 100644 --- a/commands/version.js +++ b/commands/version.js @@ -22,8 +22,8 @@ module.exports.run = async (bot, message, args) => { if(server.type === 'java') { try { - const response = await util.status(server.ip, { port: server.port }) - var versionOriginal = response.version; + const result = await util.status(server.ip, server.port) + var versionOriginal = result.version.name } catch(e) { if (warns) console.log(warn(`Couldn't get version from server! Getting it from config..`)) var versionOriginal = config.server.version; @@ -50,8 +50,8 @@ module.exports.run = async (bot, message, args) => { message.channel.send({ embeds: [versionEmbed] }); } else { try { - const response = await util.statusBedrock(server.ip, { port: server.port }) - var versionOriginal = response.version + const result = await util.statusBedrock(server.ip, server.port) + var versionOriginal = result.version.name } catch(e) { if (warns) console.log(warn(`Couldn't get version from server! Getting it from config..`)) var versionOriginal = config.server.version diff --git a/events/ready.js b/events/ready.js index add7fbd..2b4d3a4 100644 --- a/events/ready.js +++ b/events/ready.js @@ -10,6 +10,7 @@ warn = chalk.keyword('yellow').bold; module.exports = async (bot) => { const {server, config, info, settings} = bot const debug = config.settings.debug + var warns = config.settings.warns if(bot.status) { if(bot.status.includes("{onlinePlayers}") | bot.status.includes("{maxPlayers}")) { @@ -17,24 +18,44 @@ module.exports = async (bot) => { let status = bot.status; if(server.type === 'java') { - var response = await util.status(server.ip, { port: server.port }); + try { + var result = await util.status(server.ip, server.port) + } catch(err) { + console.log(err) + var errored = true + } } else { - var response = await util.statusBedrock(server.ip, { port: server.port }); + try { + var result = await util.statusBedrock(server.ip, server.port) + } catch(err) { + console.log(err) + var errored = true + } }; - if(status.includes("{onlinePlayers}")) { - status = status.replace("{onlinePlayers}", response.onlinePlayers) - }; - - if(status.includes("{maxPlayers}")) { - status = status.replace("{maxPlayers}", response.maxPlayers) - }; + if(!errored) { + if(status.includes("{onlinePlayers}")) { + status = status.replace("{onlinePlayers}", result.players.online) + }; + + if(status.includes("{maxPlayers}")) { + status = status.replace("{maxPlayers}", result.players.max) + }; - try { - bot.user.setActivity(status, {type: bot.activity}) //Sets bot activity - if(debug) console.log('✅ Successfully set status to ' + gr(`${bot.activity.toLowerCase()} ${status}`)) - } catch(e) { - console.log() + try { + bot.user.setActivity(status, {type: bot.activity}) //Sets bot activity + if(debug) console.log('✅ Successfully set status to ' + gr(`${bot.activity.toLowerCase()} ${status}`)) + } catch(e) { + console.log() + } + } else { + const status = "Offline" + try { + bot.user.setActivity(status, {type: bot.activity}) //Sets bot activity + if(debug) console.log(warn('Server was not found! Status set to ') + gr(`${bot.activity.toLowerCase()} ${status}`)) + } catch(e) { + console.log() + } } }, ms(config.autoStatus.time)); @@ -83,9 +104,9 @@ module.exports = async (bot) => { port1 = server.port if(server.type === 'java') { - util.status(ip1, { port: port1 }) - .then((response) => { - const versionOriginal = response.version + util.status(ip1, port1) + .then((result) => { + const versionOriginal = result.version.name if(settings.split) { if(versionOriginal.includes("Spigot")) { var versionAdvanced = versionOriginal.replace("Spigot", "") @@ -101,7 +122,7 @@ module.exports = async (bot) => { .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) .setDescription(`:white_check_mark: **ONLINE**`) .addFields( - { name: "PLAYERS", value: `${response.onlinePlayers}/${response.maxPlayers}` + (response.samplePlayers ? "\n\`\`\`" + response.samplePlayers.map(p => ` ${p.name} `).join('\n') + "\`\`\`":"") , inline: false }, + { name: "PLAYERS", value: `${result.players.online}/${result.players.max}` + (result.samplePlayers ? "\n\`\`\`" + result.samplePlayers.map(p => ` ${p.name} `).join('\n') + "\`\`\`":"") , inline: false }, { name: "INFO", value: `${server.type.toUpperCase()} ${version}\n\`${server.ip}\`:\`${server.port}\`` , inline: true } ) .setColor(config.embeds.color) @@ -111,19 +132,19 @@ module.exports = async (bot) => { }) .catch((error) => { const errorEmbed = new Discord.MessageEmbed() - .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) - .setDescription(':x: **OFFLINE**') - .setColor(config.embeds.error) - .setFooter('Updated') - .setTimestamp() + .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) + .setDescription(':x: **OFFLINE**') + .setColor(config.embeds.error) + .setFooter('Updated') + .setTimestamp() msg.edit({ embeds: [errorEmbed] }); - - throw error; + + if (warns) console.log(warn(`Error when posting status message! Error:\n`) + error) }) } else { - util.statusBedrock(ip1, { port: port1 }) - .then((response) => { - const versionOriginal = response.version + util.statusBedrock(ip1, port1) + .then((result) => { + const versionOriginal = result.version.name if(settings.split) { if(versionOriginal.includes("Spigot")) { var versionAdvanced = versionOriginal.replace("Spigot", "") @@ -139,7 +160,7 @@ module.exports = async (bot) => { .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) .setDescription(`:white_check_mark: **ONLINE**`) .addFields( - { name: "PLAYERS", value: `${response.onlinePlayers}/${response.maxPlayers}` , inline: false }, + { name: "PLAYERS", value: `${result.players.online}/${result.players.max}` , inline: false }, { name: "INFO", value: `${server.type.toUpperCase()} ${version}\n\`${server.ip}\`:\`${server.port}\`` , inline: true } ) .setColor(config.embeds.color) @@ -149,14 +170,14 @@ module.exports = async (bot) => { }) .catch((error) => { const errorEmbed = new Discord.MessageEmbed() - .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) - .setDescription(':x: **OFFLINE**') - .setColor(config.embeds.error) - .setFooter('Updated') - .setTimestamp() + .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) + .setDescription(':x: **OFFLINE**') + .setColor(config.embeds.error) + .setFooter('Updated') + .setTimestamp() msg.edit({ embeds: [errorEmbed] }); - throw error; + if (warns) console.log(warn(`Error when posting status message! Error:\n`) + error) }) } @@ -164,9 +185,9 @@ module.exports = async (bot) => { if(server.type === 'java') { setInterval(() => - util.status(ip1, { port: port1 }) - .then((response) => { - const versionOriginal = response.version + util.status(ip1, port1) + .then((result) => { + const versionOriginal = result.version.name if(settings.split) { if(versionOriginal.includes("Spigot")) { var versionAdvanced = versionOriginal.replace("Spigot", "") @@ -182,7 +203,7 @@ module.exports = async (bot) => { .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) .setDescription(`:white_check_mark: **ONLINE**`) .addFields( - { name: "PLAYERS", value: `${response.onlinePlayers}/${response.maxPlayers}` + (response.samplePlayers ? "\n\`\`\`" + response.samplePlayers.map(p => ` ${p.name} `).join('\n') + "\`\`\`":"") , inline: false }, + { name: "PLAYERS", value: `${result.players.online}/${result.players.max}` + (result.samplePlayers ? "\n\`\`\`" + result.samplePlayers.map(p => ` ${p.name} `).join('\n') + "\`\`\`":"") , inline: false }, { name: "INFO", value: `${server.type.toUpperCase()} ${version}\n\`${server.ip}\`:\`${server.port}\`` , inline: true } ) .setColor(config.embeds.color) @@ -192,20 +213,20 @@ module.exports = async (bot) => { }) .catch((error) => { const errorEmbed = new Discord.MessageEmbed() - .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) - .setDescription(':x: **OFFLINE**') - .setColor(config.embeds.error) - .setFooter('Updated') - .setTimestamp() + .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) + .setDescription(':x: **OFFLINE**') + .setColor(config.embeds.error) + .setFooter('Updated') + .setTimestamp() msg.edit({ embeds: [errorEmbed] }); - - throw error; + + if (warns) console.log(warn(`Error when posting status message! Error:\n`) + error) }), ms(info.time)); } else { setInterval(() => - util.statusBedrock(ip1, { port: port1 }) - .then((response) => { - const versionOriginal = response.version + util.statusBedrock(ip1, port1) + .then((result) => { + const versionOriginal = result.version.name if(settings.split) { if(versionOriginal.includes("Spigot")) { var versionAdvanced = versionOriginal.replace("Spigot", "") @@ -218,27 +239,27 @@ module.exports = async (bot) => { const version = versionAdvanced ? versionAdvanced : versionOriginal const serverEmbed = new Discord.MessageEmbed() - .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) - .setDescription(`:white_check_mark: **ONLINE**`) - .addFields( - { name: "PLAYERS", value: `${response.onlinePlayers}/${response.maxPlayers}` , inline: false }, - { name: "INFO", value: `${server.type.toUpperCase()} ${version}\n\`${server.ip}\`:\`${server.port}\`` , inline: true } - ) - .setColor(config.embeds.color) - .setFooter('Updated') - .setTimestamp() + .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) + .setDescription(`:white_check_mark: **ONLINE**`) + .addFields( + { name: "PLAYERS", value: `${result.players.online}/${result.players.max}` , inline: false }, + { name: "INFO", value: `${server.type.toUpperCase()} ${version}\n\`${server.ip}\`:\`${server.port}\`` , inline: true } + ) + .setColor(config.embeds.color) + .setFooter('Updated') + .setTimestamp() msg.edit({ embeds: [serverEmbed] }); }) .catch((error) => { const errorEmbed = new Discord.MessageEmbed() - .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) - .setDescription(':x: **OFFLINE**') - .setColor(config.embeds.error) - .setFooter('Updated') - .setTimestamp() + .setAuthor(config.server.name ? config.server.name : message.guild.name, icon) + .setDescription(':x: **OFFLINE**') + .setColor(config.embeds.error) + .setFooter('Updated') + .setTimestamp() msg.edit({ embeds: [errorEmbed] }); - throw error; + if (warns) console.log(warn(`Error when posting status message! Error:\n`) + error) }), ms(info.time)); } @@ -246,24 +267,24 @@ module.exports = async (bot) => { if(bot.readyScan && server.work) { if(server.type === 'java') { - util.status(server.ip, { port: server.port }) - .then((response) => { + util.status(server.ip, server.port) + .then((result) => { console.log(`✅ Successfully located ${gr(server.type)} server ${gr(server.ip)}!\n` + chalk.blue.bold('Server info:\n') - + bl('IP: ') + `${server.ip}:${response.port ? response.port : server.port}\n` - + bl('VERSION: ') + `${response.version ? response.version : 'unknown'}\n` - + bl('PLAYERS: ') + gr(`${response.onlinePlayers ? response.onlinePlayers : '0'}`) + '/' + gr(`${response.maxPlayers ? response.maxPlayers : '0'}`) + + bl('IP: ') + `${server.ip}:${result.port ? result.port : server.port}\n` + + bl('VERSION: ') + `${result.version.name ? result.version.name : 'unknown'}\n` + + bl('PLAYERS: ') + gr(`${result.players.online ? result.players.online: '0'}`) + '/' + gr(`${result.players.max ? result.players.max : '0'}`) ) }) .catch((error) => { console.log(warn(`Could not find ${server.type} server ${server.ip} with port ${server.port}! Error:\n`) + error) }); } else if(server.type === 'bedrock') { - util.statusBedrock(server.ip, { port: server.port }) - .then((response) => { + util.statusBedrock(server.ip, server.port) + .then((result) => { console.log(`✅ Successfully located ${gr(server.type)} server ${gr(server.ip)}!\n` + chalk.blue.bold('Server info:\n') - + bl('IP: ') + `${server.ip}:${response.port ? response.port : server.port}\n` - + bl('VERSION: ') + `${response.version ? response.version : 'unknown'}\n` - + bl('PLAYERS: ') + gr(`${response.onlinePlayers ? response.onlinePlayers : '0'}`) + '/' + gr(`${response.maxPlayers ? response.maxPlayers : '0'}`) + + bl('IP: ') + `${server.ip}:${result.port ? result.port : server.port}\n` + + bl('VERSION: ') + `${result.version.name ? result.version.name : 'unknown'}\n` + + bl('PLAYERS: ') + gr(`${result.players.online ? result.players.online : '0'}`) + '/' + gr(`${result.players.max ? result.players.max : '0'}`) ) }) .catch((error) => { diff --git a/package.json b/package.json index 20458b7..59a9d3c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minecraft-bot", - "version": "1.3.5", + "version": "1.3.6", "description": "Discord minecraft bot, with which you can check your minecraft server status (online players, version, online status etc.)", "main": "index.js", "scripts": { @@ -29,9 +29,9 @@ "homepage": "https://github.com/PetyXbron/minecraft-bot#readme", "dependencies": { "chalk": "^4.1.2", - "discord.js": "^13.2.0", + "discord.js": "^13.3.1", "fs": "^0.0.1-security", - "minecraft-server-util": "^3.7.2", + "minecraft-server-util": "^5.1.2", "ms": "^2.1.3", "quick.db": "^7.1.3" } diff --git a/templates/command.js b/templates/command.js index 2d997f5..87284da 100644 --- a/templates/command.js +++ b/templates/command.js @@ -1,15 +1,18 @@ const { commands, settings } = require("../config"); const util = require('minecraft-server-util'); const Discord = require('discord.js'); +const c = require('chalk') module.exports.config = { name: "test", //Name of command - RENAME THE FILE TOO!!! description: "Test command", //Description of command - you can change it :) aliases: commands.test, //Command's aliases - set them in config.js - enable: true //Enable this command? - true or false (boolean) + enable: false //Enable this command? - true or false (boolean) }; module.exports.run = async (bot, message, args) => { - const { server } = bot + const { server, config, text } = bot, + warn = c.keyword('yellow').bold, + warns = config.settings.warns //action }; \ No newline at end of file