Skip to content

Tips

Error: npm run register

Error when register.js cannot be executed, regardless of the success or failure of command registration.

Please consider the following modifications. (Either one or a combination)

  • package.json
    • "type": "module",
  • tsconfig.json
    • "moduleResolution": "Node",
    • "moduleResolution": "Bundler",
  • register.ts
    • Add .js when importing from other files
      import { commands } from './commands.js'
  • esbuild etc.
    • Use a bundling tool to compile register.js into a single file

This error means that you need to take into account that the code index.ts to be deployed and the code register.ts to be registered are executed in different environments.

Tips for various environments (β)

Hono

const discord = new DiscordHono()
discord.command('hello', c => c.res('Discord World'))
const hono = new Hono()
hono.get('/', c => c.text('Hono World'))
hono.mount('/interaction', discord.fetch)
  • Browser
    • https://YOUER_DOMAIN.com -> Display: Hono World
  • Discord Bot
    • /hello -> Response: Discord World
  • Discord Interaction Endpoint
    • Register URL: https://YOUER_DOMAIN.com/interaction

Deno Deploy

import { DiscordHono } from 'npm:discord-hono'
const app = new DiscordHono()
app.command('ping', c => c.res('Pong!!'))
Deno.serve(app.fetch)

Fastly Compute

import { env } from 'fastly:env'
import { DiscordHono } from 'discord-hono'
const app = new DiscordHono({
discordEnv: () => ({
APPLICATION_ID: env('DISCORD_APPLICATION_ID'),
PUBLIC_KEY: env('DISCORD_PUBLIC_KEY'),
TOKEN: env('DISCORD_TOKEN'),
}),
}).command('ping', c => c.res('Pong!!'))
addEventListener('fetch', event =>
event.respondWith(app.fetch(event.request, undefined, event)),
)

Pagination

index.ts
import { Button, Components, DiscordHono, Embed } from 'discord-hono'
type Env = {
Variables: {
content?: string
}
}
const pageContent = (content: string, page: number) => {
const maxPage = 3
const embed = new Embed()
.title('Title')
.description(`${content}\nPage: ${page}`)
const components = new Components().row(
new Button('page', 'Previous')
.custom_id(JSON.stringify([content, page - 1]))
.emoji({ name: '⬅️' })
.disabled(page <= 1),
new Button('page', 'Next')
.custom_id(JSON.stringify([content, page + 1]))
.emoji({ name: '➡️' })
.disabled(maxPage <= page),
)
return { embeds: [embed], components }
}
const app = new DiscordHono<Env>()
.command('page', c => {
return c.res(pageContent(c.var.content || '', 1))
})
.component('page', c => {
const [content, page]: [string, number] = JSON.parse(c.var.custom_id || '')
return c.resUpdate(pageContent(content, page))
})
export default app
register.ts
const commands = [
new Command('page', 'pagination').options(
new Option('content', 'page content').required(),
),
]