diff options
author | 2022-07-05 12:01:19 -0400 | |
---|---|---|
committer | 2022-07-05 09:26:58 -0700 | |
commit | 5d4fbf7f020ed7134b4f447a8119e5a3718fcbb0 (patch) | |
tree | 528cd7f9118c0a55d06372e8c77a62e5d9429312 /examples/discord-interactions/bun_shim/index.js | |
parent | 4a927e09b7cb3c22560599184c675bb185535f60 (diff) | |
download | bun-5d4fbf7f020ed7134b4f447a8119e5a3718fcbb0.tar.gz bun-5d4fbf7f020ed7134b4f447a8119e5a3718fcbb0.tar.zst bun-5d4fbf7f020ed7134b4f447a8119e5a3718fcbb0.zip |
TLA & typescript
Diffstat (limited to 'examples/discord-interactions/bun_shim/index.js')
-rw-r--r-- | examples/discord-interactions/bun_shim/index.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/examples/discord-interactions/bun_shim/index.js b/examples/discord-interactions/bun_shim/index.js index bcdcf5081..1eeca90b3 100644 --- a/examples/discord-interactions/bun_shim/index.js +++ b/examples/discord-interactions/bun_shim/index.js @@ -1,4 +1,6 @@ +import { join, extname } from 'path'; import { Creator } from 'slash-create'; +import { readdirSync, lstatSync } from 'fs'; import { FetchRequestHandler } from './rest.js'; export { default as BunServer } from './server.js'; @@ -7,4 +9,31 @@ export class BunSlashCreator extends Creator { super(...args); this.requestHandler = new FetchRequestHandler(this); } + + async registerCommandsIn(commandPath, customExtensions = []) { + const commands = []; + const extensions = ['.js', '.ts', '.mjs', '.cjs', ...customExtensions]; + + for (const path of find_files_with_extension(commandPath, extensions)) { + try { + commands.push(await import(path)); + } catch (error) { + this.emit('error', new Error(`Failed to load command ${filePath}: ${e}`)); + } + } + + return this.registerCommands(commands, true); + } +} + +function find_files_with_extension(path, extensions, names = []) { + for (const name of readdirSync(path)) { + const p = join(path, name); + const stat = lstatSync(p); + + if (extensions.includes(extname(name))) names.push(p); + else if (stat.isDirectory()) find_files_with_extension(p, extensions, names); + } + + return names; }
\ No newline at end of file |