From 624d60493f48db7e5615c7a71e2a10523772e2e8 Mon Sep 17 00:00:00 2001 From: Caleb Date: Fri, 26 Jun 2026 22:52:25 +0300 Subject: [PATCH] refactor: switch to .ts imports for some HidenCloud requirements --- .gitignore | 3 +- src/commands/!misc/premium.ts | 6 +- src/commands/!misc/refreshdb.ts | 4 +- src/commands/!mod/ban.ts | 6 +- src/commands/!mod/checkban.ts | 4 +- src/commands/!mod/unban.ts | 6 +- src/commands/!stats/stats.ts | 16 +++-- src/commands/code/complexity.ts | 4 +- src/commands/code/format.ts | 6 +- src/commands/code/run.ts | 6 +- src/commands/code/suggest.ts | 6 +- src/commands/code/whatlang.ts | 6 +- src/commands/docs/mdn.ts | 2 +- src/commands/docs/wiki.ts | 2 +- src/commands/github/profile.ts | 2 +- src/commands/github/repo.ts | 2 +- src/commands/math/math-breakdown.ts | 2 +- src/commands/misc/help.ts | 2 +- src/commands/misc/ping.ts | 2 +- src/commands/search/npm.ts | 2 +- src/commands/search/pip.ts | 2 +- src/commands/static/github.ts | 2 +- src/commands/support/feature.ts | 4 +- src/commands/support/report.ts | 4 +- src/commands/tools/color.ts | 2 +- src/commands/tools/http.ts | 2 +- src/commands/tools/id.ts | 2 +- src/commands/tools/regex.ts | 2 +- src/commands/tools/scan.ts | 2 +- src/commands/tools/tree.ts | 4 +- src/commands/tools/whois.ts | 2 +- src/events/clientReady/cacheDB.ts | 2 +- src/events/guildCreate/isBanned.ts | 2 +- src/events/messageCreate/handleCommands.ts | 10 +-- src/handlers/eventHandler.ts | 2 +- src/index.ts | 2 +- src/utils/cacheDB.ts | 4 +- src/utils/firestore.ts | 2 +- src/utils/stats.ts | 71 +++++++++++++--------- tsconfig.json | 9 ++- 40 files changed, 120 insertions(+), 101 deletions(-) diff --git a/.gitignore b/.gitignore index 5563af0..c773061 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ .env -data/ \ No newline at end of file +data/ +dist/ diff --git a/src/commands/!misc/premium.ts b/src/commands/!misc/premium.ts index d0ef300..680fcd0 100644 --- a/src/commands/!misc/premium.ts +++ b/src/commands/!misc/premium.ts @@ -1,6 +1,6 @@ -import type { SubcommandCallbackOpts } from "../../types/command.js"; -import { cacheDB, getCachedDB } from "../../utils/cacheDB.js"; -import { createDocument, deleteDocument } from "../../utils/firestore.js"; +import type { SubcommandCallbackOpts } from "../../types/command.ts"; +import { cacheDB, getCachedDB } from "../../utils/cacheDB.ts"; +import { createDocument, deleteDocument } from "../../utils/firestore.ts"; export default { name: "premium", diff --git a/src/commands/!misc/refreshdb.ts b/src/commands/!misc/refreshdb.ts index 4226bde..eb20a3e 100644 --- a/src/commands/!misc/refreshdb.ts +++ b/src/commands/!misc/refreshdb.ts @@ -1,5 +1,5 @@ -import type { CommandCallbackOpts } from "../../types/command.js"; -import { cacheDB } from "../../utils/cacheDB.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; +import { cacheDB } from "../../utils/cacheDB.ts"; export default { name: "refreshdb", diff --git a/src/commands/!mod/ban.ts b/src/commands/!mod/ban.ts index d7b9dbd..47871f6 100644 --- a/src/commands/!mod/ban.ts +++ b/src/commands/!mod/ban.ts @@ -1,7 +1,7 @@ import { EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; -import { createDocument } from "../../utils/firestore.js"; -import getConfig from "../../utils/getConfig.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; +import { createDocument } from "../../utils/firestore.ts"; +import getConfig from "../../utils/getConfig.ts"; export default { name: "ban", diff --git a/src/commands/!mod/checkban.ts b/src/commands/!mod/checkban.ts index c03de24..70bb6a4 100644 --- a/src/commands/!mod/checkban.ts +++ b/src/commands/!mod/checkban.ts @@ -1,6 +1,6 @@ import { EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; -import { getDocument } from "../../utils/firestore.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; +import { getDocument } from "../../utils/firestore.ts"; export default { name: "checkban", diff --git a/src/commands/!mod/unban.ts b/src/commands/!mod/unban.ts index df2a3bc..4617bd3 100644 --- a/src/commands/!mod/unban.ts +++ b/src/commands/!mod/unban.ts @@ -1,7 +1,7 @@ import { EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; -import { deleteDocument } from "../../utils/firestore.js"; -import getConfig from "../../utils/getConfig.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; +import { deleteDocument } from "../../utils/firestore.ts"; +import getConfig from "../../utils/getConfig.ts"; export default { name: "unban", diff --git a/src/commands/!stats/stats.ts b/src/commands/!stats/stats.ts index b10f82f..228fd40 100644 --- a/src/commands/!stats/stats.ts +++ b/src/commands/!stats/stats.ts @@ -1,6 +1,6 @@ import { EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; -import { getServerStats, getStats } from "../../utils/stats.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; +import { getServerStats, getStats } from "../../utils/stats.ts"; export default { name: "stats", @@ -9,8 +9,10 @@ export default { devOnly: true, async callback({ client, message, args }: CommandCallbackOpts) { try { - if (args[0]) { - const serverStats = await getServerStats(args[0] || message.guildId); + if (args[0] || message.guildId) { + const serverStats = await getServerStats( + (args[0] || message.guildId) ?? "", + ); if (!serverStats) { return message.reply({ embeds: [ @@ -54,7 +56,7 @@ export default { return message.reply({ embeds: [serverEmbed] }); } const stats = await getStats(); - const serverStats = await getServerStats(message.guildId); + const serverStats = await getServerStats(message.guildId ?? ""); const globalData = stats.global || {}; const commandsData = stats.commands || {}; @@ -87,7 +89,9 @@ export default { ); // Most used commands embed - const sortedCommands = Object.entries(commandsData) + const sortedCommands = Object.entries( + commandsData as Record, + ) .sort((a, b) => b[1] - a[1]) .slice(0, 10); diff --git a/src/commands/code/complexity.ts b/src/commands/code/complexity.ts index 99e9824..8e9c085 100644 --- a/src/commands/code/complexity.ts +++ b/src/commands/code/complexity.ts @@ -1,9 +1,9 @@ import { EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; import { parseCodeBlock, parseCodeCommandInput, -} from "../../utils/codeInput.js"; +} from "../../utils/codeInput.ts"; export default { name: "complexity", diff --git a/src/commands/code/format.ts b/src/commands/code/format.ts index 8e206f0..45faa30 100644 --- a/src/commands/code/format.ts +++ b/src/commands/code/format.ts @@ -1,11 +1,11 @@ import { EmbedBuilder, codeBlock } from "discord.js"; import prettier from "prettier"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; import { parseCodeBlock, parseCodeCommandInput, -} from "../../utils/codeInput.js"; -import getConfig from "../../utils/getConfig.js"; +} from "../../utils/codeInput.ts"; +import getConfig from "../../utils/getConfig.ts"; const parserMap: Record = { js: "babel", diff --git a/src/commands/code/run.ts b/src/commands/code/run.ts index a66e902..09586f6 100644 --- a/src/commands/code/run.ts +++ b/src/commands/code/run.ts @@ -8,12 +8,12 @@ import { } from "discord.js"; import "dotenv/config"; import { Groq } from "groq-sdk"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; import { parseCodeBlock, parseCodeCommandInput, -} from "../../utils/codeInput.js"; -import getConfig from "../../utils/getConfig.js"; +} from "../../utils/codeInput.ts"; +import getConfig from "../../utils/getConfig.ts"; const groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); diff --git a/src/commands/code/suggest.ts b/src/commands/code/suggest.ts index 29aec3c..2e4348b 100644 --- a/src/commands/code/suggest.ts +++ b/src/commands/code/suggest.ts @@ -1,12 +1,12 @@ import { EmbedBuilder } from "discord.js"; import "dotenv/config"; import { Groq } from "groq-sdk"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; import { parseCodeBlock, parseCodeCommandInput, -} from "../../utils/codeInput.js"; -import getConfig from "../../utils/getConfig.js"; +} from "../../utils/codeInput.ts"; +import getConfig from "../../utils/getConfig.ts"; const groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); diff --git a/src/commands/code/whatlang.ts b/src/commands/code/whatlang.ts index fe2ebbd..1a6acb3 100644 --- a/src/commands/code/whatlang.ts +++ b/src/commands/code/whatlang.ts @@ -1,12 +1,12 @@ import { EmbedBuilder } from "discord.js"; import "dotenv/config"; import { Groq } from "groq-sdk"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; import { parseCodeBlock, parseCodeCommandInput, -} from "../../utils/codeInput.js"; -import getConfig from "../../utils/getConfig.js"; +} from "../../utils/codeInput.ts"; +import getConfig from "../../utils/getConfig.ts"; const groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); diff --git a/src/commands/docs/mdn.ts b/src/commands/docs/mdn.ts index 0c57975..6f19064 100644 --- a/src/commands/docs/mdn.ts +++ b/src/commands/docs/mdn.ts @@ -1,7 +1,7 @@ import { EmbedBuilder } from "discord.js"; import fetch from "node-fetch"; import TurndownService from "turndown"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; const turndown = new TurndownService(); diff --git a/src/commands/docs/wiki.ts b/src/commands/docs/wiki.ts index 1994f3c..26085bf 100644 --- a/src/commands/docs/wiki.ts +++ b/src/commands/docs/wiki.ts @@ -5,7 +5,7 @@ import { EmbedBuilder, } from "discord.js"; import fetch from "node-fetch"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "wiki", diff --git a/src/commands/github/profile.ts b/src/commands/github/profile.ts index a3715a0..2c37696 100644 --- a/src/commands/github/profile.ts +++ b/src/commands/github/profile.ts @@ -4,7 +4,7 @@ import { ButtonStyle, EmbedBuilder, } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "profile", diff --git a/src/commands/github/repo.ts b/src/commands/github/repo.ts index a4ec2c1..3bdaf06 100644 --- a/src/commands/github/repo.ts +++ b/src/commands/github/repo.ts @@ -4,7 +4,7 @@ import { ButtonStyle, EmbedBuilder, } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "repo", diff --git a/src/commands/math/math-breakdown.ts b/src/commands/math/math-breakdown.ts index fea8894..64d9cb9 100644 --- a/src/commands/math/math-breakdown.ts +++ b/src/commands/math/math-breakdown.ts @@ -1,7 +1,7 @@ import { EmbedBuilder } from "discord.js"; import "dotenv/config"; import { Groq } from "groq-sdk"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; const groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); diff --git a/src/commands/misc/help.ts b/src/commands/misc/help.ts index 407cf78..5c08b66 100644 --- a/src/commands/misc/help.ts +++ b/src/commands/misc/help.ts @@ -2,7 +2,7 @@ import type { Client, Message } from "discord.js"; import { EmbedBuilder } from "discord.js"; import path, { join } from "path"; import { fileURLToPath } from "url"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; type Command = { name: string; diff --git a/src/commands/misc/ping.ts b/src/commands/misc/ping.ts index c73f1b8..8f02342 100644 --- a/src/commands/misc/ping.ts +++ b/src/commands/misc/ping.ts @@ -1,4 +1,4 @@ -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "ping", diff --git a/src/commands/search/npm.ts b/src/commands/search/npm.ts index 2d8f8b2..1c75842 100644 --- a/src/commands/search/npm.ts +++ b/src/commands/search/npm.ts @@ -5,7 +5,7 @@ import { EmbedBuilder, } from "discord.js"; import fetch from "node-fetch"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "npm", diff --git a/src/commands/search/pip.ts b/src/commands/search/pip.ts index 3a8ec65..fec37ef 100644 --- a/src/commands/search/pip.ts +++ b/src/commands/search/pip.ts @@ -5,7 +5,7 @@ import { EmbedBuilder, } from "discord.js"; import fetch from "node-fetch"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "pip", diff --git a/src/commands/static/github.ts b/src/commands/static/github.ts index e4c94d1..2f86018 100644 --- a/src/commands/static/github.ts +++ b/src/commands/static/github.ts @@ -1,6 +1,6 @@ import { EmbedBuilder } from "discord.js"; import fetch from "node-fetch"; -import type { SubcommandCallbackOpts } from "../../types/command.js"; +import type { SubcommandCallbackOpts } from "../../types/command.ts"; export default { name: "github", diff --git a/src/commands/support/feature.ts b/src/commands/support/feature.ts index 910604d..cbac1fc 100644 --- a/src/commands/support/feature.ts +++ b/src/commands/support/feature.ts @@ -1,7 +1,7 @@ import { EmbedBuilder } from "discord.js"; import ucid from "unique-custom-id"; -import type { CommandCallbackOpts } from "../../types/command.js"; -import getConfig from "../../utils/getConfig.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; +import getConfig from "../../utils/getConfig.ts"; export default { name: "feature", diff --git a/src/commands/support/report.ts b/src/commands/support/report.ts index 24db211..cb1eed5 100644 --- a/src/commands/support/report.ts +++ b/src/commands/support/report.ts @@ -1,7 +1,7 @@ import { EmbedBuilder } from "discord.js"; import ucid from "unique-custom-id"; -import type { CommandCallbackOpts } from "../../types/command.js"; -import getConfig from "../../utils/getConfig.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; +import getConfig from "../../utils/getConfig.ts"; export default { name: "report", diff --git a/src/commands/tools/color.ts b/src/commands/tools/color.ts index 5b1f3d0..c61083f 100644 --- a/src/commands/tools/color.ts +++ b/src/commands/tools/color.ts @@ -1,6 +1,6 @@ import { createCanvas } from "@napi-rs/canvas"; import { AttachmentBuilder, EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; function hexToRgb(hex: string) { const clean = hex.replace("#", ""); diff --git a/src/commands/tools/http.ts b/src/commands/tools/http.ts index fdb3566..87b409e 100644 --- a/src/commands/tools/http.ts +++ b/src/commands/tools/http.ts @@ -1,6 +1,6 @@ import { EmbedBuilder } from "discord.js"; import fetch from "node-fetch"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "http", diff --git a/src/commands/tools/id.ts b/src/commands/tools/id.ts index 462475f..6123a62 100644 --- a/src/commands/tools/id.ts +++ b/src/commands/tools/id.ts @@ -1,7 +1,7 @@ import { EmbedBuilder } from "discord.js"; import ucid from "unique-custom-id"; import { map } from "unique-custom-id/format"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "id", diff --git a/src/commands/tools/regex.ts b/src/commands/tools/regex.ts index 3c714f5..e605666 100644 --- a/src/commands/tools/regex.ts +++ b/src/commands/tools/regex.ts @@ -1,5 +1,5 @@ import { EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; export default { name: "regex", diff --git a/src/commands/tools/scan.ts b/src/commands/tools/scan.ts index dc33fef..a6b1351 100644 --- a/src/commands/tools/scan.ts +++ b/src/commands/tools/scan.ts @@ -1,6 +1,6 @@ import { ActionRowBuilder, ButtonBuilder, EmbedBuilder } from "discord.js"; import "dotenv/config"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; const GSB_API_KEY = process.env.GSB_API_KEY; const URLSCAN_API_KEY = process.env.URLSCAN_API_KEY; diff --git a/src/commands/tools/tree.ts b/src/commands/tools/tree.ts index 74c4db8..d7515ca 100644 --- a/src/commands/tools/tree.ts +++ b/src/commands/tools/tree.ts @@ -1,6 +1,6 @@ import { AttachmentBuilder, codeBlock, EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; -import getConfig from "../../utils/getConfig.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; +import getConfig from "../../utils/getConfig.ts"; type RepoInfo = { owner: string; diff --git a/src/commands/tools/whois.ts b/src/commands/tools/whois.ts index 4aa65d6..cd04e2f 100644 --- a/src/commands/tools/whois.ts +++ b/src/commands/tools/whois.ts @@ -1,5 +1,5 @@ import { EmbedBuilder } from "discord.js"; -import type { CommandCallbackOpts } from "../../types/command.js"; +import type { CommandCallbackOpts } from "../../types/command.ts"; type WhoisEvent = { eventAction?: string; diff --git a/src/events/clientReady/cacheDB.ts b/src/events/clientReady/cacheDB.ts index aa53e08..e25e588 100644 --- a/src/events/clientReady/cacheDB.ts +++ b/src/events/clientReady/cacheDB.ts @@ -1,5 +1,5 @@ import type { Client } from "discord.js"; -import { cacheDB } from "../../utils/cacheDB.js"; +import { cacheDB } from "../../utils/cacheDB.ts"; export default async (client: Client) => { await cacheDB(); diff --git a/src/events/guildCreate/isBanned.ts b/src/events/guildCreate/isBanned.ts index 4cc7a18..617622b 100644 --- a/src/events/guildCreate/isBanned.ts +++ b/src/events/guildCreate/isBanned.ts @@ -1,5 +1,5 @@ import type { Client, Guild } from "discord.js"; -import { getDocument } from "../../utils/firestore.js"; +import { getDocument } from "../../utils/firestore.ts"; export default async (client: Client, guild: Guild) => { try { diff --git a/src/events/messageCreate/handleCommands.ts b/src/events/messageCreate/handleCommands.ts index 3725265..60637e2 100644 --- a/src/events/messageCreate/handleCommands.ts +++ b/src/events/messageCreate/handleCommands.ts @@ -11,10 +11,10 @@ import "dotenv/config"; import NodeCache from "node-cache"; import path, { join } from "path"; import { fileURLToPath } from "url"; -import { getCachedDB } from "../../utils/cacheDB.js"; -import getAllFiles from "../../utils/getAllFiles.js"; -import getConfig from "../../utils/getConfig.js"; -import { trackCommandStat } from "../../utils/stats.js"; +import { getCachedDB } from "../../utils/cacheDB.ts"; +import getAllFiles from "../../utils/getAllFiles.ts"; +import getConfig from "../../utils/getConfig.ts"; +import { trackCommandStat } from "../../utils/stats.ts"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -238,7 +238,7 @@ export default async (client: Client, message: Message) => { // Track command statistics await trackCommandStat( commandObject.name, - message.guildId, + message.guildId ?? "", commandObject.devOnly || false, client, ); diff --git a/src/handlers/eventHandler.ts b/src/handlers/eventHandler.ts index eeb90f3..5ca865e 100644 --- a/src/handlers/eventHandler.ts +++ b/src/handlers/eventHandler.ts @@ -1,7 +1,7 @@ import type { Client, ClientEvents } from "discord.js"; import path from "path"; import { fileURLToPath } from "url"; -import getAllFiles from "../utils/getAllFiles.js"; +import getAllFiles from "../utils/getAllFiles.ts"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); diff --git a/src/index.ts b/src/index.ts index e1541b4..d0fc81e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { Client, IntentsBitField } from "discord.js"; import "dotenv/config"; -import eventHandler from "./handlers/eventHandler.js"; +import eventHandler from "./handlers/eventHandler.ts"; const client = new Client({ intents: [ diff --git a/src/utils/cacheDB.ts b/src/utils/cacheDB.ts index 2986ad0..5f43680 100644 --- a/src/utils/cacheDB.ts +++ b/src/utils/cacheDB.ts @@ -1,6 +1,6 @@ import path from "path"; -import { readFile, writeFile } from "./fileOps.js"; -import { getAllDocuments } from "./firestore.js"; +import { readFile, writeFile } from "./fileOps.ts"; +import { getAllDocuments } from "./firestore.ts"; import type { DocumentData } from "firebase-admin/firestore"; import fsp from "fs/promises"; diff --git a/src/utils/firestore.ts b/src/utils/firestore.ts index 4bdfd39..fb92f3c 100644 --- a/src/utils/firestore.ts +++ b/src/utils/firestore.ts @@ -1,5 +1,5 @@ import { type DocumentData, WriteResult } from "firebase-admin/firestore"; -import { db } from "../config/firebase.js"; +import { db } from "../config/firebase.ts"; export function createDocument( collection: string, diff --git a/src/utils/stats.ts b/src/utils/stats.ts index fc929bb..d47422d 100644 --- a/src/utils/stats.ts +++ b/src/utils/stats.ts @@ -1,29 +1,37 @@ -import { createDocument, getDocument, updateDocument } from "./firestore.js"; +import { createDocument, getDocument, updateDocument } from "./firestore.ts"; const SAVE_THRESHOLD = 5; let globalCommandCount = 0; -let commandBuffer = {}; -let serverBuffer = {}; +let commandBuffer: Record = {}; +let serverBuffer: Record = + {}; /** * Get the start date of the current period * @param {string} period - 'daily', 'weekly', or 'monthly' * @returns {string} ISO date string (YYYY-MM-DD) */ -function getPeriodStartDate(period) { +/** + * Get the start date of the current period + * @param {string} period - 'daily', 'weekly', or 'monthly' + * @returns {string} ISO date string (YYYY-MM-DD) + */ +function getPeriodStartDate(period: string): string { const now = new Date(); now.setHours(0, 0, 0, 0); if (period === "daily") { - return now.toISOString().split("T")[0]; + return now.toISOString().split("T")[0] || ""; } else if (period === "weekly") { const start = new Date(now); start.setDate(start.getDate() - start.getDay()); - return start.toISOString().split("T")[0]; + return start.toISOString().split("T")[0] || ""; } else if (period === "monthly") { const start = new Date(now.getFullYear(), now.getMonth(), 1); - return start.toISOString().split("T")[0]; + return start.toISOString().split("T")[0] || ""; } + + return now.toISOString().split("T")[0] || ""; } /** @@ -32,16 +40,19 @@ function getPeriodStartDate(period) { * @param {string} period - 'daily', 'weekly', or 'monthly' * @returns {boolean} Whether the period should be reset */ -function isPeriodExpired(storedPeriodStart, period) { +function isPeriodExpired(storedPeriodStart: string, period: string): boolean { const currentStart = getPeriodStartDate(period); return storedPeriodStart !== currentStart; } -function getBufferedCommandCount() { - return Object.values(commandBuffer).reduce((sum, count) => sum + count, 0); +function getBufferedCommandCount(): number { + return (Object.values(commandBuffer) as number[]).reduce( + (sum, count) => sum + count, + 0, + ); } -async function flushStatsOnExit() { +async function flushStatsOnExit(): Promise { const bufferedCommandCount = getBufferedCommandCount(); if (bufferedCommandCount === 0) return; await saveStats(); @@ -52,14 +63,14 @@ async function flushStatsOnExit() { * @param {string} commandName - The original command name (not alias) * @param {string} guildId - The guild/server ID * @param {boolean} isDevOnly - Whether this is a dev-only command - * @param {Client} client - Discord client instance + * @param {any} client - Discord client instance */ export async function trackCommandStat( - commandName, - guildId, - isDevOnly, - client, -) { + commandName: string, + guildId: string, + isDevOnly: boolean, + client: any, +): Promise { if (isDevOnly) return; globalCommandCount++; @@ -86,7 +97,7 @@ export async function trackCommandStat( /** * Save accumulated stats to Firestore */ -async function saveStats() { +async function saveStats(): Promise { try { const bufferedCommandCount = getBufferedCommandCount(); if (bufferedCommandCount === 0) return; @@ -105,9 +116,9 @@ async function saveStats() { /** * Update global command statistics */ -async function updateGlobalStats(commandCount) { +async function updateGlobalStats(commandCount: number): Promise { try { - const globalDoc = await getDocument("stats", "global"); + const globalDoc = (await getDocument("stats", "global")) as any; const dailyStart = getPeriodStartDate("daily"); const weeklyStart = getPeriodStartDate("weekly"); @@ -130,7 +141,7 @@ async function updateGlobalStats(commandCount) { }, }); } else { - const updates = { + const updates: Record = { totalCommandsRan: (globalDoc.totalCommandsRan || 0) + commandCount, }; @@ -183,10 +194,10 @@ async function updateGlobalStats(commandCount) { /** * Update most used commands */ -async function updateMostUsedCommands() { +async function updateMostUsedCommands(): Promise { try { - const commandDoc = await getDocument("stats", "commands"); - const updates = {}; + const commandDoc = (await getDocument("stats", "commands")) as any; + const updates: Record = {}; for (const [commandName, count] of Object.entries(commandBuffer)) { const currentCount = commandDoc?.[commandName] || 0; @@ -208,13 +219,13 @@ async function updateMostUsedCommands() { /** * Update server-specific statistics */ -async function updateServerStats() { +async function updateServerStats(): Promise { try { const dailyStart = getPeriodStartDate("daily"); const weeklyStart = getPeriodStartDate("weekly"); for (const [guildId, serverData] of Object.entries(serverBuffer)) { - const serverDoc = await getDocument("servers", guildId); + const serverDoc = (await getDocument("servers", guildId)) as any; const commandCount = serverData.commandCount; const guildName = serverData.guildName || ""; @@ -232,7 +243,7 @@ async function updateServerStats() { }, }); } else { - const updates = { + const updates: Record = { totalCommandsRan: (serverDoc.totalCommandsRan || 0) + commandCount, }; @@ -278,7 +289,7 @@ async function updateServerStats() { * Get all statistics for display * @returns {Promise} Stats object with global, commands, and servers data */ -export async function getStats() { +export async function getStats(): Promise<{ global: any; commands: any }> { try { const globalDoc = await getDocument("stats", "global"); const commandsDoc = await getDocument("stats", "commands"); @@ -296,9 +307,9 @@ export async function getStats() { /** * Get specific server statistics * @param {string} guildId - * @returns {Promise} Server stats + * @returns {Promise} Server stats */ -export async function getServerStats(guildId) { +export async function getServerStats(guildId: string): Promise { try { return await getDocument("servers", guildId); } catch (error) { diff --git a/tsconfig.json b/tsconfig.json index cec4a3a..a0a7cef 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,8 +2,8 @@ // Visit https://aka.ms/tsconfig to read more about this file "compilerOptions": { // File Layout - // "rootDir": "./src", - // "outDir": "./dist", + "rootDir": "./src", + "outDir": "./dist", // Environment Settings // See also https://aka.ms/tsconfig/module @@ -24,6 +24,9 @@ "noUncheckedIndexedAccess": true, "exactOptionalPropertyTypes": true, + "allowImportingTsExtensions": true, + "noEmit": true, + // Style Options // "noImplicitReturns": true, // "noImplicitOverride": true, @@ -39,6 +42,6 @@ "isolatedModules": true, "noUncheckedSideEffectImports": true, "moduleDetection": "force", - "skipLibCheck": true, + "skipLibCheck": true } }