From 9057229157eb5fac1e4e3206748fad7084e3160e Mon Sep 17 00:00:00 2001 From: DIVISIONSolar Date: Mon, 11 Sep 2023 16:49:23 -0400 Subject: [PATCH] Add a check cmd --- lang/lang.en-US.json | 39 ++++++++++++- src/commands/chat/index.ts | 1 + .../chat/information/check-command.ts | 56 +++++++++++++++++++ src/commands/metadata.ts | 9 +++ src/start-bot.ts | 4 +- 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 src/commands/chat/information/check-command.ts diff --git a/lang/lang.en-US.json b/lang/lang.en-US.json index 2f5ff6a..611e7f6 100644 --- a/lang/lang.en-US.json +++ b/lang/lang.en-US.json @@ -37,6 +37,39 @@ "title": "**Dynmap for {{SERVER}}**", "description": ["{{LINK}}"] }, + "checkresponse": { + "title": "Status for {{HOSTNAME}}", + "fields": [ + { + "name": "**HOSTNAME**", + "value": "```{{HOSTNAME}}```" + }, + { + "name": "**PLAYERS**", + "value": "```{{PLAYERS}}```" + }, + { + "name": "**VERSION**", + "value": "```{{VERSION}}```" + }, + { + "name": "**MOTD**", + "value": "```{{MOTD}}```" + }, + { + "name": "**STATUS**", + "value": "```Online: {{ONLINE}}```" + }, + { + "name": "**PING**", + "value": "```{{PING}}```" + }, + { + "name": "**QUERY**", + "value": "```{{QUERY}}```" + } + ] + }, "helpContactSupport": { "title": "Help - {{REF:helpOptions.contactSupport}}", "description": [ @@ -216,7 +249,8 @@ "info": "info", "test": "test", "howtojoin": "howtojoin", - "map": "map" + "map": "map", + "checkstatus": "check" }, "userCommands": { "viewDateJoined": "View Date Joined" @@ -236,7 +270,8 @@ "info": "View bot info.", "test": "Run the test command.", "howtojoin": "How To Join.", - "map": "The dynmap for the server." + "map": "The dynmap for the server.", + "checkstatus": "Check the servers status." }, "argDescs": { "devCommand": "Command.", diff --git a/src/commands/chat/index.ts b/src/commands/chat/index.ts index c08c8f0..7e0bf9c 100644 --- a/src/commands/chat/index.ts +++ b/src/commands/chat/index.ts @@ -4,3 +4,4 @@ export { InfoCommand } from './info-command.js'; export { TestCommand } from './test-command.js'; export { HowToJoinCommand } from './information/howtojoin-command.js'; export { MapCommand } from './information/map-command.js'; +export { CheckCommand } from './information/check-command.js'; diff --git a/src/commands/chat/information/check-command.ts b/src/commands/chat/information/check-command.ts new file mode 100644 index 0000000..05fef49 --- /dev/null +++ b/src/commands/chat/information/check-command.ts @@ -0,0 +1,56 @@ +import { ChatInputCommandInteraction, PermissionsString } from 'discord.js'; +import { RateLimiter } from 'discord.js-rate-limiter'; +import axios from 'axios'; +import { Language } from '../../../models/enum-helpers/index.js'; +import { EventData } from '../../../models/internal-models.js'; +import { Lang } from '../../../services/index.js'; +import { InteractionUtils } from '../../../utils/index.js'; +import { Command, CommandDeferType } from '../../index.js'; + +export class CheckCommand implements Command { + public names = [Lang.getRef('chatCommands.checkstatus', Language.Default)]; // Change the command name as needed + public cooldown = new RateLimiter(1, 5000); + public deferType = CommandDeferType.PUBLIC; + public requireClientPerms: PermissionsString[] = []; + + public async execute(intr: ChatInputCommandInteraction, data: EventData): Promise { + // Construct the request URL + const url = 'https://api.mcsrvstat.us/2/play.pxlvrs.net'; + + try { + // Make the HTTP GET request + const response = await axios.get(url, { + headers: { + 'User-Agent': 'PixelVerse Helper, me@joshsevero.dev', + }, + }); + + const serverInfo = response.data; + + await InteractionUtils.send( + intr, + Lang.getEmbed('displayEmbeds.checkresponse', data.lang, { + IP: serverInfo.ip, + HOSTNAME: serverInfo.hostname || 'N/A', + PLAYERS: serverInfo.players + ? `${serverInfo.players.online}/${serverInfo.players.max}` + : 'N/A', + VERSION: serverInfo.version || 'N/A', + MOTD: serverInfo.motd ? serverInfo.motd.clean : 'N/A', + ONLINE: serverInfo.online || 'N/A', + PING: serverInfo.ping || 'N/A', + QUERY: serverInfo.query || 'N/A', + }) + ); + } catch (error) { + console.error(error); + // Handle error if the request fails + await InteractionUtils.send( + intr, + Lang.getEmbed('errorEmbeds.command', data.lang, { + ERROR_CODE: error.code, + }) + ); + } + } +} diff --git a/src/commands/metadata.ts b/src/commands/metadata.ts index e1550c1..5279ead 100644 --- a/src/commands/metadata.ts +++ b/src/commands/metadata.ts @@ -69,6 +69,15 @@ export const ChatCommandMetadata: { dm_permission: true, default_member_permissions: undefined, }, + CHECKSTATUS: { + type: ApplicationCommandType.ChatInput, + name: Lang.getRef('chatCommands.checkstatus', Language.Default), + name_localizations: Lang.getRefLocalizationMap('chatCommands.checkstatus'), + description: Lang.getRef('commandDescs.checkstatus', Language.Default), + description_localizations: Lang.getRefLocalizationMap('commandDescs.checkstatus'), + dm_permission: true, + default_member_permissions: undefined, + }, HOWTOJOIN: { type: ApplicationCommandType.ChatInput, name: Lang.getRef('chatCommands.howtojoin', Language.Default), diff --git a/src/start-bot.ts b/src/start-bot.ts index a256b62..313d832 100644 --- a/src/start-bot.ts +++ b/src/start-bot.ts @@ -9,7 +9,8 @@ import { InfoCommand, TestCommand, HowToJoinCommand, - MapCommand + MapCommand, + CheckCommand, } from './commands/chat/index.js'; import { ChatCommandMetadata, @@ -77,6 +78,7 @@ async function start(): Promise { // TODO: Add new commands here new HowToJoinCommand(), new MapCommand(), + new CheckCommand(), ]; // Buttons