diff --git a/bots/discord/src/context.ts b/bots/discord/src/context.ts index c09b7d0..900c66f 100644 --- a/bots/discord/src/context.ts +++ b/bots/discord/src/context.ts @@ -4,6 +4,7 @@ import { createLogger } from '@revanced/bot-shared' import { ActivityType, Client as DiscordClient, Partials } from 'discord.js' import config from '../config' import { LabeledResponseDatabase } from './classes/Database' +import { pathJoinCurrentDir } from './utils/fs' with { type: 'macro' } export { config } export const logger = createLogger({ @@ -52,5 +53,5 @@ export const discord = { ], }, }), - commands: await loadCommands(), + commands: await loadCommands(pathJoinCurrentDir(import.meta.url, 'commands')), } as const diff --git a/bots/discord/src/index.ts b/bots/discord/src/index.ts index 239a244..0e4b836 100644 --- a/bots/discord/src/index.ts +++ b/bots/discord/src/index.ts @@ -1,16 +1,17 @@ -import { listAllFilesRecursive } from '$utils/fs' +// import { listAllFilesRecursive, pathJoinCurrentDir } from '$utils/fs' import { getMissingEnvironmentVariables } from '@revanced/bot-shared' import { api, discord, logger } from './context' +import { listAllFilesRecursive, pathJoinCurrentDir } from './utils/fs' with { type: 'macro' } -for (const apiEvents of await listAllFilesRecursive('src/events/api')) { - await import(apiEvents) +for (const event of listAllFilesRecursive(pathJoinCurrentDir(import.meta.url, 'events', 'api'))) { + await import(event) } const { client: apiClient } = api await apiClient.ws.connect() -for (const discordEvents of await listAllFilesRecursive('src/events/discord')) { - await import(discordEvents) +for (const event of listAllFilesRecursive(pathJoinCurrentDir(import.meta.url, 'events', 'discord'))) { + await import(event) } const { client: discordClient } = discord diff --git a/bots/discord/src/types.d.ts b/bots/discord/src/types.d.ts index 5e2602f..24ab764 100644 --- a/bots/discord/src/types.d.ts +++ b/bots/discord/src/types.d.ts @@ -2,3 +2,4 @@ type IfExtends = T extends U ? True : False type IfTrue = IfExtends type EmptyObject = Record type ValuesOf = T[keyof T] +type MaybeArray = T | T[] diff --git a/bots/discord/src/utils/discord/commands.ts b/bots/discord/src/utils/discord/commands.ts index 008d80c..5c4e454 100644 --- a/bots/discord/src/utils/discord/commands.ts +++ b/bots/discord/src/utils/discord/commands.ts @@ -1,9 +1,9 @@ import type { Command } from '$commands' import { listAllFilesRecursive } from '$utils/fs' -export const loadCommands = async () => { +export const loadCommands = async (dir: string) => { const commandsMap: Record = {} - const files = await listAllFilesRecursive('src/commands') + const files = listAllFilesRecursive(dir) const commands = await Promise.all( files.map(async file => { const command = await import(file) diff --git a/bots/discord/src/utils/fs.ts b/bots/discord/src/utils/fs.ts index 6bc0e4f..07ea43c 100644 --- a/bots/discord/src/utils/fs.ts +++ b/bots/discord/src/utils/fs.ts @@ -1,17 +1,21 @@ -import { join } from 'path' -import { readdir, stat } from 'fs/promises' +import { readdirSync, statSync } from 'fs' +import { dirname, join } from 'path' +import { fileURLToPath } from 'bun' -export async function listAllFilesRecursive(dir: string): Promise { - const files = await readdir(dir) +export const listAllFilesRecursive = (dir: string): string[] => { + const files = readdirSync(dir) const result: string[] = [] for (const file of files) { const filePath = join(dir, file) - const fileStat = await stat(filePath) + const fileStat = statSync(filePath) if (fileStat.isDirectory()) { - result.push(...(await listAllFilesRecursive(filePath))) + result.push(...listAllFilesRecursive(filePath)) } else { result.push(filePath) } } return result } + +export const pathJoinCurrentDir = (importMetaUrl: string, ...objects: [string, ...string[]]) => + join(dirname(fileURLToPath(importMetaUrl)), ...objects)