mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-18 08:43:57 +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 { 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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
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 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[]
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user