From ce0c92b3f70529b37b60d1c9dd3b47f46003cc50 Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Sat, 14 Jan 2023 23:54:50 +0100 Subject: [PATCH] feat: minecraft server status --- commands/handlers/mcping-players.ts | 15 ++++++++++ index.ts | 7 ++++- util/minecraftstatus.ts | 44 ++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 commands/handlers/mcping-players.ts diff --git a/commands/handlers/mcping-players.ts b/commands/handlers/mcping-players.ts new file mode 100644 index 0000000..8f495af --- /dev/null +++ b/commands/handlers/mcping-players.ts @@ -0,0 +1,15 @@ +import { commandModule, CommandType } from '@sern/handler'; +import axios from 'axios'; + +export default commandModule({ + type: CommandType.Button, + plugins: [], + execute: async (ctx) => { + await ctx.deferReply({ ephemeral: true }) + const request = await axios.get('https://api.minetools.eu/query/minecraft.maraturing.com/25565').then(res => res.data) + + await ctx.editReply({ + content: `` + }) + }, +}); \ No newline at end of file diff --git a/index.ts b/index.ts index 209ee37..a64aee7 100644 --- a/index.ts +++ b/index.ts @@ -57,6 +57,7 @@ Sern.init({ client.on('ready', async () => { console.log('Logged on!'); + setInterval(() => { const statuses = [ { name: 'Minecraft', type: ActivityType.Playing }, @@ -87,7 +88,11 @@ client.on('ready', async () => { setIntervalAsync(async () => { await minecraftstatus(client); - }, 30_000); + }, 20_000); + + setIntervalAsync(async () => { + await minecraftstatus(client); + }, 20_000); webserver() } else { diff --git a/util/minecraftstatus.ts b/util/minecraftstatus.ts index ff4beef..b1fa22e 100644 --- a/util/minecraftstatus.ts +++ b/util/minecraftstatus.ts @@ -1,5 +1,47 @@ -import { Client } from "discord.js"; +import { Client, ColorResolvable, EmbedBuilder, Message, TextChannel } from "discord.js"; +import axios from 'axios' +import dayjs from "dayjs"; +import timezone from 'dayjs/plugin/timezone.js' +import utc from 'dayjs/plugin/utc.js' export default async function minecraftstatus(client: Client) { + dayjs.extend(utc) + dayjs.extend(timezone) + + const request = await axios.get('https://api.minetools.eu/ping/minecraft.maraturing.com/25565').then(res => res.data) + const fetchMsg = await (await client.channels.fetch('1063944267258662922')! as TextChannel).messages.fetch('1063950406474010674') as Message + let onlineorelse: string + let colorcode: ColorResolvable + if (request.error) { + onlineorelse = 'Offline' + colorcode = '#8B0000' + } else { + onlineorelse = 'Online' + colorcode = 'Green' + } + + let embed = new EmbedBuilder() + .setThumbnail('https://api.minetools.eu/favicon/minecraft.maraturing.com/25565') + .setColor(colorcode) + .setTitle('Estado del servidor') + .setFooter({ text: `Última actualización: ${dayjs().tz('Europe/Madrid').format('DD/MM/YYYY HH:mm:ss')}` }) + + if (onlineorelse === 'Offline') { + embed = embed.setFields( + { name: 'Estado', value: onlineorelse, inline: true } + ) + } else { + embed = embed.setFields( + { name: 'Estado', value: onlineorelse, inline: true }, + { name: 'Ping', value: parseInt(request.latency.toString()).toString(), inline: true }, + { name: '\u200B', value: '\u200B', inline: true }, + { name: 'Jugadores online', value: request.players.online.toString(), inline: true }, + { name: 'Jugadores máximos', value: request.players.max.toString(), inline: true }, + ) + } + await fetchMsg.edit({ + content: '', + embeds: [embed] + }) } \ No newline at end of file