diff options
author | 2022-07-05 12:01:19 -0400 | |
---|---|---|
committer | 2022-07-05 09:26:58 -0700 | |
commit | 5d4fbf7f020ed7134b4f447a8119e5a3718fcbb0 (patch) | |
tree | 528cd7f9118c0a55d06372e8c77a62e5d9429312 | |
parent | 4a927e09b7cb3c22560599184c675bb185535f60 (diff) | |
download | bun-5d4fbf7f020ed7134b4f447a8119e5a3718fcbb0.tar.gz bun-5d4fbf7f020ed7134b4f447a8119e5a3718fcbb0.tar.zst bun-5d4fbf7f020ed7134b4f447a8119e5a3718fcbb0.zip |
TLA & typescript
-rw-r--r-- | examples/discord-interactions/bun_shim/index.js | 29 | ||||
-rw-r--r-- | examples/discord-interactions/bun_shim/server.js | 8 | ||||
-rw-r--r-- | examples/discord-interactions/commands/hello.js | 2 | ||||
-rw-r--r-- | examples/discord-interactions/run.js | 3 |
4 files changed, 36 insertions, 6 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 diff --git a/examples/discord-interactions/bun_shim/server.js b/examples/discord-interactions/bun_shim/server.js index d2caff328..d162cabc9 100644 --- a/examples/discord-interactions/bun_shim/server.js +++ b/examples/discord-interactions/bun_shim/server.js @@ -10,15 +10,15 @@ export default class BunServer extends Server { super({ alreadyListening: true }); } + createEndpoint(path, handler) { + this.#handler = handler; + } + stop() { if (this.#server) this.#server.close(); else throw new Error('BunServer not started'); } - createEndpoint(path, handler) { - this.#handler = handler; - } - listen(port) { const getHandler = () => this.#handler; diff --git a/examples/discord-interactions/commands/hello.js b/examples/discord-interactions/commands/hello.js index 61d92cf82..df94931ff 100644 --- a/examples/discord-interactions/commands/hello.js +++ b/examples/discord-interactions/commands/hello.js @@ -1,6 +1,6 @@ const { SlashCommand, CommandOptionType } = require('slash-create'); -module.exports = class HelloCommand extends SlashCommand { +export default class HelloCommand extends SlashCommand { constructor(creator) { super(creator, { name: 'hello', diff --git a/examples/discord-interactions/run.js b/examples/discord-interactions/run.js index 749a039d6..108e0f85c 100644 --- a/examples/discord-interactions/run.js +++ b/examples/discord-interactions/run.js @@ -14,8 +14,9 @@ const client = new BunSlashCreator({ client.on('error', console.error); client.withServer(new BunServer()); -client.registerCommandsIn(path.join(__dirname, 'commands')).syncCommands(); +await client.registerCommandsIn(path.join(__dirname, 'commands')); +client.syncCommands(); await client.server.listen(1337); // client.server.stop(); // stop server
\ No newline at end of file |