createFactory
createFactory はコマンド毎のファイル分割を補助する関数です。
また、factory 内で型を抽出し、var の型を自動的に適応してくれます。
// src/init.tsimport { createFactory } from 'discord-hono'export const factory = createFactory<{ Bindings: Env }>()
// src/index.tsimport * as handlers from './handlers'import { factory } from './init'export default factory.discord().loader(Object.values(handlers))
// src/register.tsimport { register } from 'discord-hono'import * as handlers from './handlers/index.js'import { factory } from './init.js'register( factory.getCommands(Object.values(handlers)), process.env.DISCORD_APPLICATION_ID, process.env.DISCORD_TOKEN, // process.env.DISCORD_TEST_GUILD_ID,)// src/handlers/index.tsexport * from './hello.js'export * from './help.js'export * from './utils.js'
// src/handlers/hello.tsimport { makeSlashCommand, makeStringOption } from 'discord-hono'import { factory } from '../init.js'export const command_hello = factory.command( makeSlashCommand('hello', 'Hello, World!').options([ makeStringOption('name', 'Your name'), ]), c => c.res(`Hello, ${c.var.name ?? 'World'}!`),)
// src/handlers/help.tsimport { makeActionRow, makeLinkButton, makeSlashCommand } from 'discord-hono'import { factory } from '../init.js'import { component_delete } from './utils.js'export const command_help = factory.command( makeSlashCommand('help', 'response help'), c => c.res({ components: [ makeActionRow([ makeLinkButton('https://discord-hono.luis.fun', ['📑', 'Docs']), component_delete.component, ]), ], }),)
// src/handlers/utils.tsimport { buttonStyle, makeButton } from 'discord-hono'import { factory } from '../init.js'export const component_delete = factory.component( makeButton('delete', ['🗑️', 'Delete']).style(buttonStyle.Secondary), c => c.update().resDefer(c => c.followup()),)