Skip to content

Context

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

Context can be received as the second argument of app.command(), app.component(), app.modal(), app.cron().

.env .event .executionCtx .set() .get() .var

Please refer to here.
We try to make it as similar to Hono as possible.

.waitUntil()

c.waitUntil = c.executionCtx.waitUntil

get: req

command, component, modal

Interaction Request are included as is.

get: interaction

command, component, modal

c.interaction = JSON.parse(await c.req.text())

Please refer to the Official Docs.

get: sub

command

const commands = [
new Command('slash', 'slash description').options(
new SubCommand('sub1', 'Subcommand 1'),
new SubGroup('group', 'group description').options(
new SubCommand('sub2', 'Subcommand 2').options(
new Option('text', 'text'),
),
new SubCommand('sub3', 'Subcommand 3'),
),
),
]
const app = new DiscordHono().command('slash', c => {
switch (c.sub.string) {
case 'sub1':
return c.res('sub1')
case 'group sub2':
return c.res('g-sub2: ' + c.values.text)
default:
return c.res(c.sub.group + '-' + c.sub.command)
}
})

The first argument of SubGroup is in c.sub.group.
The first argument of SubCommand is in c.sub.command.
c.sub.string = (c.sub.group ? c.sub.group + ' ' : '') + c.sub.command

get: values

command modal

const app = new DiscordHono()
.command('ping', c => c.res(c.values.OPTION_NAME))
.modal('modal', c => c.res(c.values.TEXTINPUT_CUSTOM_ID))

It contains the value of the command option.
In the case of a modal, it contains the string of the custom_id.

get: cronEvent

cron

const app = new DiscordHono().cron('', c => console.log(c.cronEvent.cron))

It has the event value of the scheduled() first argument.

.res()

command, component, modal

const app = new DiscordHono()
.command('ping', c => c.res('Pong!!'))
.command('hello', c => c.res({ content: 'World!!' }))

The argument is a string or APIInteractionResponseCallbackData.

.resDefer()

command, component, modal

const app = new DiscordHono().command('ping', c =>
c.resDefer(async c => await c.followup('Followup Text')),
)

If you don’t respond to a Discord interaction within 3 seconds, an error will occur.
When performing time-consuming tasks, it’s a good idea to use .resDefer() and include the subsequent processing as an argument.

.resUpdate()

component

const app = new DiscordHono().component('button', c =>
c.resUpdate('text or data'),
)

Overwrites a message that has already been sent.

The argument is a string or APIInteractionResponseCallbackData.

.resDeferUpdate()

component

const app = new DiscordHono().component('button', c =>
c.resDeferUpdate(async c => await c.followup('Followup Text')),
)

Delay overwriting messages.

.resModal()

command, component

const app = new DiscordHono().command('ping', c =>
c.resModal(
new Modal('unique-id', 'Title').row(new TextInput('text-id', 'Label')),
),
)

The argument is a Modal instance or APIModalInteractionResponseCallbackData.

.followup()

command, component, modal

const app = new DiscordHono().command('ping', c =>
c.resDefer(
async c =>
await c.followup('Followup Text or Data', {
blob: Blob,
name: 'image-blob.png',
}),
),
)

.followup() is used to update messages after Defer.

The first argument is a string or APIInteractionResponseCallbackData.
The second argument is FileData or FileData[].
FileData = { blob: Blob, name: ‘file.name’ }

.followupDelete()

command, component, modal

.ephemeral()

command, component, modal

const app = new DiscordHono()
app.command('ping', c => c.ephemeral().res('Pong!!'))

Send a message that is only visible to the user who performed the interaction.