From dac548d82c187c55ab37d7c95f5c51af74b3b008 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 9 Nov 2024 18:14:10 -0500 Subject: [PATCH] update Formatters --- util/Formatters.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 util/Formatters.ts diff --git a/util/Formatters.ts b/util/Formatters.ts new file mode 100644 index 0000000..7e37c19 --- /dev/null +++ b/util/Formatters.ts @@ -0,0 +1,55 @@ +import { GuildMember, User } from "discord.js"; +import Partner, { PartnerCommissionType, PartnerRoleType } from "../database/Partner"; +import { FormatNameOptions } from "./MemberUtil"; + +export default class Formatters { + public static formatStandardDate(date: Date | string | number): string { + const resolvedDate = new Date(date); + if (!resolvedDate) return ""; + + const year = resolvedDate.getFullYear(); + const month = String(resolvedDate.getMonth() + 1).padStart(2, "0"); + const day = String(resolvedDate.getDate()).padStart(2, "0"); + const hours = String(resolvedDate.getHours()).padStart(2, "0"); + const minutes = String(resolvedDate.getMinutes()).padStart(2, "0"); + const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; + + return `${year}-${month}-${day} @ ${hours}:${minutes} (${timeZone})`; + } + + // TODO: comments and extended formatting + public static formatName( + target: GuildMember | User, + partner?: Partner | null + ): FormatNameOptions { + console.debug( + `[MemberUtil] Formatting name for ${target.displayName} at url ${target instanceof GuildMember ? target.user.displayAvatarURL() : target.displayAvatarURL()}` + ); + // if the role type is managerial, add a [k] to the end of the name + // if the partner exists, set the iconURL to the organizational logo + if (partner?.roleType == PartnerRoleType.MANAGERIAL) { + return { + text: `${target.displayName} [k]`, + iconURL: target.displayAvatarURL(), + }; + } else if (partner?.commissionType == PartnerCommissionType.CONTRACTUAL) { + // if the commission type is contractual, add a [c] to the end of the name + return { + text: `${target.displayName} [c]`, + iconURL: + target instanceof GuildMember + ? target.user.displayAvatarURL() + : target.displayAvatarURL(), + }; + } else { + // otherwise, just set the author to the member's display name + return { + text: target.displayName, + iconURL: + target instanceof GuildMember + ? target.user.displayAvatarURL() + : target.displayAvatarURL(), + }; + } + } +}