Skip to content

DiscordHono

import { DiscordHono } from 'discord-hono'
const app = new DiscordHono()
app.command('ping', c => c.res('Pong!!'))
export default app

.command()

const commands = [
new Command('ping', 'response Pong'),
new Command('image', 'response Image'),
]
const app = new DiscordHono()
.command('ping', c => c.res('Pong!!'))
.command('image', c => c.res('Image!!'))

The first argument of Command() must match the first argument of .command().
The second argument of the matched .command() is executed.

The first argument can be a regex or ''.
'' matches all remaining unmatched parts and can be used as a fallback.

.component()

const app = new DiscordHono()
.command('components', c =>
c.res({
content: 'No button clicked yet',
components: new Components().row(
new Button('button-1', 'Button'),
new Button('button-2', 'Second'),
),
}),
)
.component('button-1', c => c.resUpdate('Button clicked'))
.component('button-2', c => c.resUpdate('Second clicked'))

The first argument of the component element Button() must match the first argument of .component().
The second argument of the matched .component() is executed.

The first argument can be a regex or ''.
'' matches all remaining unmatched parts and can be used as a fallback.

const app = new DiscordHono()
.command('modal', c =>
c.resModal(
new Modal('modal-1', 'Modal Title')
.row(new TextInput('text-1', 'Text'))
.row(new TextInput('text-2', 'Second')),
),
)
.modal('modal-1', c => c.res('Modal submitted'))

The first argument of Modal() must match the first argument of .modal().
The second argument of the matched .modal() is executed.

The first argument can be a regex or ''.
'' matches all remaining unmatched parts and can be used as a fallback.

.cron()

const app = new DiscordHono()
.cron('0 0 * * *', async c =>
rest(c.env.DISCORD_TOKEN)
.channels('CHANNEL_ID')
.messages()
.post('Daily Post'),
)
.cron('', async c =>
rest(c.env.DISCORD_TOKEN)
.channels('CHANNEL_ID')
.messages()
.post('Other Cron Triggers Post'),
)
wrangler.toml
name = "example"
main = "src/index.ts"
compatibility_date = "2024-02-08"
[triggers]
crons = [ "0 * * * *", "0 0 * * *" ]

The first argument of .cron() must match the toml file crons.
The second argument of the matched .cron() is executed.

The first argument can be a regex or ''.
'' matches all remaining unmatched parts and can be used as a fallback.

.fetch()

Please refer to here.
We try to be as similar as possible to Hono’s fetch().

.scheduled()

If you use export default app, .scheduled() is included.

Init Options

verify

If it’s a Cloudflare environment, there’s no need to use it.

discord-verify

import { verify, PlatformAlgorithm } from 'discord-verify'
const app = new DiscordHono({
verify: (...args) =>
verify(...args, crypto.webcrypto.subtle, PlatformAlgorithm.OldNode),
})

discord-interactions
Deprecated: When discord-verify doesn’t work well

import { verifyKey } from 'discord-interactions'
const app = new DiscordHono({ verify: verifyKey })

discordEnv

If it’s an environment variable similar to Example, there’s no need to use it.
Use it when you save the environment variable with a different name or when it’s an environment other than Cloudflare.

const app = new DiscordHono({
discordEnv: env => ({
APPLICATION_ID: env.DISCORD_APPLICATION_ID,
PUBLIC_KEY: env.DISCORD_PUBLIC_KEY,
TOKEN: env.DISCORD_TOKEN,
}),
})