mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(bots/discord/utils): allow loading commands from custom dir
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
1
bots/discord/src/types.d.ts
vendored
1
bots/discord/src/types.d.ts
vendored
@@ -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[]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user