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 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

View File

@@ -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

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 EmptyObject<K = PropertyKey> = Record<K, never>
type ValuesOf<T> = T[keyof T]
type MaybeArray<T> = T | T[]

View File

@@ -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<string, Command> = {}
const files = await listAllFilesRecursive('src/commands')
const files = listAllFilesRecursive(dir)
const commands = await Promise.all(
files.map(async file => {
const command = await import(file)

View File

@@ -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<string[]> {
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)