feat(bots/discord/utils): allow loading commands from custom dir

This commit is contained in:
PalmDevs
2024-03-29 17:24:53 +07:00
parent 7eeb631270
commit 8b690b879b
5 changed files with 21 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ import { createLogger } from '@revanced/bot-shared'
import { ActivityType, Client as DiscordClient, Partials } from 'discord.js' import { ActivityType, Client as DiscordClient, Partials } from 'discord.js'
import config from '../config' import config from '../config'
import { LabeledResponseDatabase } from './classes/Database' import { LabeledResponseDatabase } from './classes/Database'
import { pathJoinCurrentDir } from './utils/fs' with { type: 'macro' }
export { config } export { config }
export const logger = createLogger({ export const logger = createLogger({
@@ -52,5 +53,5 @@ export const discord = {
], ],
}, },
}), }),
commands: await loadCommands(), commands: await loadCommands(pathJoinCurrentDir(import.meta.url, 'commands')),
} as const } as const

View File

@@ -1,16 +1,17 @@
import { listAllFilesRecursive } from '$utils/fs' // import { listAllFilesRecursive, pathJoinCurrentDir } from '$utils/fs'
import { getMissingEnvironmentVariables } from '@revanced/bot-shared' import { getMissingEnvironmentVariables } from '@revanced/bot-shared'
import { api, discord, logger } from './context' import { api, discord, logger } from './context'
import { listAllFilesRecursive, pathJoinCurrentDir } from './utils/fs' with { type: 'macro' }
for (const apiEvents of await listAllFilesRecursive('src/events/api')) { for (const event of listAllFilesRecursive(pathJoinCurrentDir(import.meta.url, 'events', 'api'))) {
await import(apiEvents) await import(event)
} }
const { client: apiClient } = api const { client: apiClient } = api
await apiClient.ws.connect() await apiClient.ws.connect()
for (const discordEvents of await listAllFilesRecursive('src/events/discord')) { for (const event of listAllFilesRecursive(pathJoinCurrentDir(import.meta.url, 'events', 'discord'))) {
await import(discordEvents) await import(event)
} }
const { client: discordClient } = discord const { client: discordClient } = discord

View File

@@ -2,3 +2,4 @@ type IfExtends<T, U, True, False> = T extends U ? True : False
type IfTrue<Condition, True, False> = IfExtends<Condition, true, True, False> type IfTrue<Condition, True, False> = IfExtends<Condition, true, True, False>
type EmptyObject<K = PropertyKey> = Record<K, never> type EmptyObject<K = PropertyKey> = Record<K, never>
type ValuesOf<T> = T[keyof T] type ValuesOf<T> = T[keyof T]
type MaybeArray<T> = T | T[]

View File

@@ -1,9 +1,9 @@
import type { Command } from '$commands' import type { Command } from '$commands'
import { listAllFilesRecursive } from '$utils/fs' import { listAllFilesRecursive } from '$utils/fs'
export const loadCommands = async () => { export const loadCommands = async (dir: string) => {
const commandsMap: Record<string, Command> = {} const commandsMap: Record<string, Command> = {}
const files = await listAllFilesRecursive('src/commands') const files = listAllFilesRecursive(dir)
const commands = await Promise.all( const commands = await Promise.all(
files.map(async file => { files.map(async file => {
const command = await import(file) const command = await import(file)

View File

@@ -1,17 +1,21 @@
import { join } from 'path' import { readdirSync, statSync } from 'fs'
import { readdir, stat } from 'fs/promises' import { dirname, join } from 'path'
import { fileURLToPath } from 'bun'
export async function listAllFilesRecursive(dir: string): Promise<string[]> { export const listAllFilesRecursive = (dir: string): string[] => {
const files = await readdir(dir) const files = readdirSync(dir)
const result: string[] = [] const result: string[] = []
for (const file of files) { for (const file of files) {
const filePath = join(dir, file) const filePath = join(dir, file)
const fileStat = await stat(filePath) const fileStat = statSync(filePath)
if (fileStat.isDirectory()) { if (fileStat.isDirectory()) {
result.push(...(await listAllFilesRecursive(filePath))) result.push(...listAllFilesRecursive(filePath))
} else { } else {
result.push(filePath) result.push(filePath)
} }
} }
return result return result
} }
export const pathJoinCurrentDir = (importMetaUrl: string, ...objects: [string, ...string[]]) =>
join(dirname(fileURLToPath(importMetaUrl)), ...objects)