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
Section titled “.env .event .executionCtx .set() .get() .var”Please refer to here.
We try to make it as similar to Hono as possible.
Regarding .var
Section titled “Regarding .var”const app = new DiscordHono() .command('ping', c => c.res(c.var.OPTION_NAME)) .modal('modal', c => c.res(c.var.TEXTINPUT_CUSTOM_ID))The following values are included by default.
c.var.OPTION_NAMEcommand option value (command, autocomplete)c.var.TEXTINPUT_CUSTOM_IDvalue of text input (modal)
get: ref
Section titled “get: ref”Convenience properties for quick references.
c.ref.key
Section titled “c.ref.key”The trigger for each handler.
c.ref.key = c.interaction.data.name (command, autocomplete)
c.ref.key = c.interaction.data.custom_id (component, modal)
c.ref.key = c.interaction.cron (cron)
c.ref.attachments …etc
Section titled “c.ref.attachments …etc”References to resolved data
c.ref.attachments = c.interaction.data.resolved.attachments
c.ref.xxx = c.interaction.data.resolved.xxx
c.ref.target_id
Section titled “c.ref.target_id”command
The command target for message- or user-triggered commands.
c.ref.target_id = c.interaction.data.target_id
Main usage
c.ref.messages?.[c.ref.target_id]?.content // Get the content of the trigger messagec.ref.custom_value
Section titled “c.ref.custom_value”component, modal
A library-specific transferable variable.
// Component definitionnew Button('button', 'Button').custom_value('value-string')// Inside handler codeconsole.log(c.ref.custom_value) // value-stringc.ref.values
Section titled “c.ref.values”component
Values of select components
c.ref.values = c.interaction.data.values
get: interaction
Section titled “get: interaction”c.interaction = JSON.parse(await c.req.text())
Please refer to the Official Docs.
In case of CronContext
Section titled “In case of CronContext”It has the controller value of the scheduled() first argument.
get: sub
Section titled “get: sub”command, autocomplete
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.var.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: focused
Section titled “get: focused”autocomplete
const app = new DiscordHono().autocomplete('hello', c => { console.log(c.focused?.name) // option name console.log(c.focused?.value) // option value return c.resAutocomplete(...)})The optional object in focus.
get: rest
Section titled “get: rest”c.rest = createRest(c.env.DISCORD_TOKEN)
Rest
.res()
Section titled “.res()”command, component, modal
const app = new DiscordHono() .command('ping', c => c.res('Pong!!')) .command('hello', c => c.res({ content: 'World!!' }))The first argument is a string or APIInteractionResponseCallbackData.
The second argument is FileData or FileData[].
FileData = { blob: Blob, name: ‘file.name’ }
.resDefer()
Section titled “.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.
.resAutocomplete()
Section titled “.resAutocomplete()”autocomplete
const app = new DiscordHono().autocomplete( 'hello', c => c.resAutocomplete( new Autocomplete(c.focused?.value).choices( { name: 'world', value: 'world!!!' }, { name: 'hi', value: 'hi!' }, ), ), c => c.res(c.var.option),)The argument is a Autocomplete instance or APICommandAutocompleteInteractionResponseCallbackData.
.resModal()
Section titled “.resModal()”command, component
const app = new DiscordHono().command('ping', c => c.resModal( new Modal('unique-id', 'Modal Title').row( new TextInput('text-id', 'Label'), ), ),)The argument is a Modal instance or APIModalInteractionResponseCallbackData.
.resActivity()
Section titled “.resActivity()”command, component, modal
const app = new DiscordHono().command('activity', c => c.resActivity())Launch the Activity. (Only available for apps with Activities enabled)
.update()
Section titled “.update()”component, modal
Changes c.res() and c.resDefer() to overwrite mode for sent messages.
const app = new DiscordHono().component('button', c => c.update().res('Update Text'),).followup()
Section titled “.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 RESTPatchAPIInteractionOriginalResponseJSONBody.
The second argument is FileData or FileData[].
FileData = { blob: Blob, name: ‘file.name’ }
Additionally, by leaving the arguments empty, you can delete the original message.
return c.update().resDefer(c => c.followup()).flags()
Section titled “.flags()”command, component, modal
const app = new DiscordHono()app.command('ping', c => c.flags('EPHEMERAL', 'SUPPRESS_NOTIFICATIONS').res('Pong!!'),)Adds the message flags from here.