132 lines
4 KiB
JavaScript
132 lines
4 KiB
JavaScript
const findMc = require("./findMc");
|
|
const colors = require("colors");
|
|
require("dotenv").config();
|
|
const Discord = require("discord.js");
|
|
const client = new Discord.Client({ intents: ["Guilds"] });
|
|
|
|
unavailEmote = "<:unreachable:1255354739382816798>"
|
|
|
|
pingEmotes = [
|
|
"<:ping_1:1255354721145978950>",
|
|
"<:ping_2:1255354720281821205>",
|
|
"<:ping_3:1255354719409279056>",
|
|
"<:ping_4:1255354718150987828>",
|
|
"<:ping_5:1255354717660250172>"
|
|
]
|
|
|
|
const {
|
|
REST,
|
|
Routes
|
|
} = require('discord.js');
|
|
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
|
|
|
|
const commands = [{
|
|
name: "check",
|
|
description: "Find a Minecraft server",
|
|
options: [{
|
|
name: "address",
|
|
description: "The address of the server",
|
|
type: 3,
|
|
required: true
|
|
}],
|
|
integration_types: [0, 1],
|
|
contexts: [0, 1, 2]
|
|
}];
|
|
|
|
const pingEmote = (ping) => {
|
|
// If ping is less than 100, use the last emote, go from there for between 100 and 199, 200, 299 and 300+
|
|
if (ping < 100) return pingEmotes[4];
|
|
if (ping < 200) return pingEmotes[3];
|
|
if (ping < 300) return pingEmotes[2];
|
|
if (ping < 400) return pingEmotes[1];
|
|
return pingEmotes[0];
|
|
}
|
|
|
|
const filterMOTD = (motd) => {
|
|
lines = motd.split("\n");
|
|
lines.forEach((line, index) => {
|
|
lines[index] = line.replace(/§./g, "").trim();
|
|
});
|
|
return lines.join("\n");
|
|
}
|
|
|
|
client.on('ready', () => {
|
|
console.log(`${colors.cyan("[INFO]")} Logged in as ${colors.green(client.user.tag)}`);
|
|
(async () => {
|
|
try {
|
|
console.log(`${colors.cyan("[INFO]")} Registering Commands...`)
|
|
let start = Date.now()
|
|
//Global
|
|
await rest.put(Routes.applicationCommands(client.user.id), { body: commands })
|
|
console.log(`${colors.cyan("[INFO]")} Successfully registered commands. Took ${colors.green((Date.now() - start) / 1000)} seconds.`);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
})();
|
|
})
|
|
|
|
client.on('interactionCreate', async interaction => {
|
|
if (!interaction.isCommand()) return;
|
|
console.log(`${colors.cyan("[INFO]")} Command ${colors.green(interaction.commandName)} executed by ${colors.green(interaction.user.displayName)}`);
|
|
switch (interaction.commandName) {
|
|
case "check":
|
|
await interaction.deferReply();
|
|
const address = interaction.options.getString("address");
|
|
findMc(address, 5000).then((data) => {
|
|
data.description = filterMOTD(data.description);
|
|
msgData = {
|
|
embeds: [{
|
|
title: `Server Info for ${address}`,
|
|
description: `Ping: ${pingEmote(data.ping)} ${data.ping}ms`,
|
|
fields: [
|
|
{
|
|
name: "Server Version",
|
|
value: data.version.name,
|
|
inline: true
|
|
},
|
|
{
|
|
name: "Players",
|
|
value: `${data.players?.online}/${data.players?.max}`,
|
|
inline: true
|
|
},
|
|
{
|
|
name: "MOTD",
|
|
value: `\`${data.description?.split("\n")[0]}\`\n\`${data.description?.split("\n")[1]}\``
|
|
},
|
|
data.players.sample?.length > 0 ? {
|
|
name: "Players",
|
|
value: `\`\`\`\n${data.players.sample.map(player => player.name).join("\n")}\n\`\`\``
|
|
} : {
|
|
name: "Players",
|
|
value: "No players online."
|
|
}
|
|
]
|
|
}]
|
|
}
|
|
// if data.favicon is not null, decode the base64 as a buffer, and make the json object for the attachment
|
|
if (data.favicon) {
|
|
const buffer = Buffer.from(data.favicon.split(",")[1], "base64");
|
|
msgData.files = [{
|
|
attachment: buffer,
|
|
name: "favicon.png"
|
|
}]
|
|
msgData.embeds[0].thumbnail = {
|
|
url: "attachment://favicon.png"
|
|
}
|
|
}
|
|
interaction.editReply(msgData);
|
|
}).catch((err) => {
|
|
if (err.toString().startsWith("Error: The client timed out")) {
|
|
return interaction.editReply({ content: `${unavailEmote} The server did not respond or is offline.`, ephemeral: true });
|
|
}
|
|
if (err.toString().startsWith("Error: Invalid hostname")) {
|
|
return interaction.editReply({ content: "Invalid hostname.", ephemeral: true });
|
|
}
|
|
console.log(err)
|
|
return interaction.editReply({ content: "An error occurred while trying to ping the server.", ephemeral: true });
|
|
})
|
|
break;
|
|
}
|
|
});
|
|
|
|
client.login(process.env.TOKEN); |