From c41079584c1898b5b2c78817ec99ed7e5108bf01 Mon Sep 17 00:00:00 2001 From: DIVISIONSolar Date: Sun, 15 Oct 2023 03:07:24 -0400 Subject: [PATCH] Add command to view ups status --- lang/lang.en-US.json | 37 +++++++- src/commands/args.ts | 7 ++ src/commands/chat/index.ts | 1 + .../chat/information/upsstats-command.ts | 90 +++++++++++++++++++ src/commands/metadata.ts | 15 ++++ src/start-bot.ts | 2 + 6 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 src/commands/chat/information/upsstats-command.ts diff --git a/lang/lang.en-US.json b/lang/lang.en-US.json index 611e7f6..70b9bfc 100644 --- a/lang/lang.en-US.json +++ b/lang/lang.en-US.json @@ -70,6 +70,31 @@ } ] }, + "apcstatus": { + "title": "Status for {{SERVER}}", + "fields": [ + { + "name": "**MODEL**", + "value": "```{{MODEL}}```" + }, + { + "name": "**STATUS**", + "value": "```{{STATUS}}```" + }, + { + "name": "**BCHARGE**", + "value": "```{{BCHARGE}}```" + }, + { + "name": "**TIMELEFT**", + "value": "```{{TIMELEFT}}```" + }, + { + "name": "**STATUS**", + "value": "```{{STATUS}}```" + } + ] + }, "helpContactSupport": { "title": "Help - {{REF:helpOptions.contactSupport}}", "description": [ @@ -250,7 +275,8 @@ "test": "test", "howtojoin": "howtojoin", "map": "map", - "checkstatus": "check" + "checkstatus": "check", + "upsstatus": "upstats" }, "userCommands": { "viewDateJoined": "View Date Joined" @@ -262,7 +288,8 @@ "command": "command", "option": "option", "platform": "platform", - "server": "server" + "server": "server", + "upslocation": "location" }, "commandDescs": { "dev": "Developer use only.", @@ -271,14 +298,16 @@ "test": "Run the test command.", "howtojoin": "How To Join.", "map": "The dynmap for the server.", - "checkstatus": "Check the servers status." + "checkstatus": "Check the servers status.", + "upsstatus": "UPS Status." }, "argDescs": { "devCommand": "Command.", "helpOption": "Option.", "infoOption": "Option.", "platform": "Ex. PC, Xbox, Playstation, Mobile.", - "server": "The server's name" + "server": "The server's name", + "upslocation": "Location you wish to check. (Avail: pxlvrs.net, network)" }, "fields": { "commands": "Commands", diff --git a/src/commands/args.ts b/src/commands/args.ts index 9f279dc..640d2cd 100644 --- a/src/commands/args.ts +++ b/src/commands/args.ts @@ -71,4 +71,11 @@ export class Args { description_localizations: Lang.getRefLocalizationMap('argDescs.server'), type: ApplicationCommandOptionType.String, }; + public static readonly upslocation: APIApplicationCommandBasicOption = { + name: Lang.getRef('arguments.upslocation', Language.Default), + name_localizations: Lang.getRefLocalizationMap('arguments.upslocation'), + description: Lang.getRef('argDescs.upslocation', Language.Default), + description_localizations: Lang.getRefLocalizationMap('argDescs.upslocation'), + type: ApplicationCommandOptionType.String, + }; } diff --git a/src/commands/chat/index.ts b/src/commands/chat/index.ts index 7e0bf9c..16081f2 100644 --- a/src/commands/chat/index.ts +++ b/src/commands/chat/index.ts @@ -5,3 +5,4 @@ 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'; +export { UpsStatusCommand } from './information/upsstats-command.js'; \ No newline at end of file diff --git a/src/commands/chat/information/upsstats-command.ts b/src/commands/chat/information/upsstats-command.ts new file mode 100644 index 0000000..291ab55 --- /dev/null +++ b/src/commands/chat/information/upsstats-command.ts @@ -0,0 +1,90 @@ +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 UpsStatusCommand implements Command { + public names = [Lang.getRef('chatCommands.upsstatus', Language.Default)]; + public cooldown = new RateLimiter(1, 5000); + public deferType = CommandDeferType.PUBLIC; + public requireClientPerms: PermissionsString[] = []; + + public async execute(intr: ChatInputCommandInteraction, data: EventData): Promise { + let args = { + location: intr.options.getString( + Lang.getRef('arguments.upslocation', Language.Default) + ), + }; + + let url = ''; + + // Determine the URL based on the location argument + if (args.location.toLowerCase() === 'pxlvrs.net') { + url = 'https://na-pa-01.joshseveros.cloud/cgi-bin/apcupsd/upsfstats.cgi?host=127.0.0.1'; + } else if (args.location.toLowerCase() === 'network') { + url = 'https://na-pa-04.joshseveros.cloud/cgi-bin/apcupsd/upsfstats.cgi?host=127.0.0.1'; + } else { + await InteractionUtils.send( + intr, + Lang.getEmbed('errorEmbeds.command', data.lang, { + "ERROR_CODE": "Invalid location argument", + }) + ); + return; + } + + try { + const response = await axios.get(url, { + headers: { + 'User-Agent': 'pxlvrs.net, me@joshsevero.dev', + } + }); + + // Assuming the response contains the APC status in text format + const apcStatusText = response.data; + + // Now you can process the apcStatusText to extract the desired information + // For example, you can split the text by lines and extract specific fields + + // Example extraction: + const lines = apcStatusText.split('\n'); + const modelLine = lines.find(line => line.startsWith('MODEL')); + const model = modelLine.split(':')[1].trim(); + + // Extracting BCHARGE and TIMELEFT + const bchargeLine = lines.find(line => line.startsWith('BCHARGE')); + const bcharge = bchargeLine.split(':')[1].trim(); + + const timeleftLine = lines.find(line => line.startsWith('TIMELEFT')); + const timeleft = timeleftLine.split(':')[1].trim(); + + // Extracting STATUS + const statusLine = lines.find(line => line.startsWith('STATUS')); + const status = statusLine.split(':')[1].trim(); + + await InteractionUtils.send( + intr, + Lang.getEmbed('displayEmbeds.apcstatus', data.lang, { + SERVER: args.location, + MODEL: model, + BCHARGE: bcharge, + TIMELEFT: timeleft, + STATUS: status, + // Add other extracted fields here... + }) + ); + } catch (error) { + console.error(error); + 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 5279ead..69d6e7b 100644 --- a/src/commands/metadata.ts +++ b/src/commands/metadata.ts @@ -108,6 +108,21 @@ export const ChatCommandMetadata: { }, ], }, + UPSLOCATION: { + type: ApplicationCommandType.ChatInput, + name: Lang.getRef('chatCommands.upsstatus', Language.Default), + name_localizations: Lang.getRefLocalizationMap('chatCommands.upsstatus'), + description: Lang.getRef('commandDescs.upsstatus', Language.Default), + description_localizations: Lang.getRefLocalizationMap('commandDescs.upsstatus'), + dm_permission: true, + default_member_permissions: undefined, + options: [ + { + ...Args.upslocation, + required: true, + }, + ], + }, }; export const MessageCommandMetadata: { diff --git a/src/start-bot.ts b/src/start-bot.ts index 313d832..0e60673 100644 --- a/src/start-bot.ts +++ b/src/start-bot.ts @@ -11,6 +11,7 @@ import { HowToJoinCommand, MapCommand, CheckCommand, + UpsStatusCommand, } from './commands/chat/index.js'; import { ChatCommandMetadata, @@ -79,6 +80,7 @@ async function start(): Promise { new HowToJoinCommand(), new MapCommand(), new CheckCommand(), + new UpsStatusCommand(), ]; // Buttons