diff --git a/src/helpers/loadCommands.ts b/src/helpers/loadCommands.ts index cdde003..f4aadd6 100644 --- a/src/helpers/loadCommands.ts +++ b/src/helpers/loadCommands.ts @@ -1,11 +1,12 @@ import { Collection } from 'discord.js'; import { Status } from '../interactions/commands/status'; import { Command } from '../interfaces/Commands'; +import { Keyfob } from '../interactions/commands/keyfob'; export type CommandCollection = Collection; export default async function loadCommands(): Promise { - const allCommands = [new Status()]; + const allCommands = [new Status(), new Keyfob()]; const commands = new Collection(); // Include all commands here diff --git a/src/interactions/commands/keyfob.ts b/src/interactions/commands/keyfob.ts new file mode 100644 index 0000000..e9dab74 --- /dev/null +++ b/src/interactions/commands/keyfob.ts @@ -0,0 +1,30 @@ +import { CommandInteraction, CacheType, EmbedBuilder } from "discord.js"; +import { Command } from "../../interfaces/Commands"; +import EventData from "../../interfaces/EventData.interface"; +import HomeAssistant from "../../lib/homeassistant"; + +export class Keyfob extends Command { + title = "keyfob"; + description = "Get the last scanned keyfob value"; + isEphemeral = true; + + async run(interaction: CommandInteraction, data: EventData): Promise { + const homeAssistant = new HomeAssistant(); + try { + const { state, last_changed } = (await homeAssistant.getEntity("cardreader.value")); + const date = new Date(last_changed); + const embed = new EmbedBuilder() + .setTitle("Keyfob") + .setFields([{ + name: "Keyfob ID", + value: state + }]) + .setFooter({ + text: `Last Updated: ${date.getMonth() + 1}/${date.getDate()} @ ${date.getHours() % 12 || 12}:${date.getMinutes().toString().padStart(2, '0')} ${date.getHours() >= 12 ? 'PM' : 'AM'}` + }) + interaction.editReply({ embeds: [embed] }); + } catch (error) { + interaction.editReply("It seems no keyfob has been scanned recently. Try scanning it again"); + } + } +}