コンテンツにスキップ

Context

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

app.command()app.component()app.modal()app.cron() の第2引数で Context を受け取ることできます。

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

Section titled “.env .event .executionCtx .set() .get() .var”

こちらを参照してください。
できるだけ Hono と同じになるようにしています。

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

デフォルトで次の値が含まれています。

  • c.var.OPTION_NAME コマンドオプションの値 (command, autocomplete)
  • c.var.TEXTINPUT_CUSTOM_ID テキストインプットの値 (modal)

簡易参照用のプロパティです

各ハンドラのトリガーです

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)

Resolved の参照

c.ref.attachments = c.interaction.data.resolved.attachments
c.ref.xxx = c.interaction.data.resolved.xxx

command

メッセージやユーザートリガーのコマンドターゲットです

c.ref.target_id = c.interaction.data.target_id

主な使い方

c.ref.messages[c.ref.target_id]?.content // トリガーメッセージのコンテンツを取得

component, modal

ライブラリ独自の受渡し可能な変数です

// コンポーネント定義
makeButton('button', 'ボタン').custom_value('value-string')
// ハンドル内コード
console.log(c.ref.custom_value) // value-string

component

セレクトコンポーネントの values

c.ref.values = c.interaction.data.values

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

公式ドキュメントを参照してください。

scheduled() の第一引数の controller オブジェクトです。

command, autocomplete

const commands = [
makeSlashCommand('slash', 'slash description').options([
makeSubCommand('sub1', 'サブコマンド 1'),
makeSubCommandGroup('group', 'サブコマンドグループ description').options([
makeSubCommand('sub2', 'サブコマンド 2').options([
makeStringOption('text', 'テキスト'),
]),
makeSubCommand('sub3', 'サブコマンド 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)
}
})

SubGroup の第一引数が c.sub.group に入っています。
SubCommand の第一引数が c.sub.command に入っています。
c.sub.string = (c.sub.group ? c.sub.group + ' ' : '') + c.sub.command

autocomplete

const app = new DiscordHono().autocomplete('hello', c => {
console.log(c.focused?.name) // オプション名
console.log(c.focused?.value) // オプションの値
return c.resAutocomplete(...)
})

フォーカスされているオプションのオブジェクト。

公式ドキュメント

c.rest = createRest(c.env.DISCORD_TOKEN)
Rest

command, component, modal

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

第1引数は string または APIInteractionResponseCallbackData です。
第2引数は FileData または FileData[] です。
FileData = { blob: Blob, name: ‘file.name’ }

command, component, modal

const app = new DiscordHono().command('ping', c =>
c.resDefer(async c => await c.followup('Followup テキスト')),
)

3秒以内に Discord のインタラクションに応答しないとエラーが発生します。
時間のかかる処理を行うときは、.resDefer() を使用して、その後の処理を引数に含めるといいでしょう。

autocomplete

const app = new DiscordHono().autocomplete(
'hello',
c =>
c.resAutocomplete([
{ name: 'world', value: 'world!!!' },
{ name: 'hi', value: 'hi!' },
]),
c => c.res(c.var.option),
)

引数は APICommandAutocompleteInteractionResponseCallbackData または APIApplicationCommandOptionChoice の配列です。

command, component

const app = new DiscordHono().command('ping', c =>
c.resModal(
makeModal('custom_id', 'タイトル', [
makeLabel('テキストラベル', makeTextInput('text-id', 'テキストラベル')),
]),
),
)

引数は Modal インスタンスまたは APIModalInteractionResponseCallbackData です。

command, component, modal

const app = new DiscordHono().command('activity', c => c.resActivity())

アクティビティを起動します。(アクティビティが有効になっているアプリでのみ使用可能)

component, modal

c.res()c.resDefer() を送信済みメッセージの上書きモードに変更します。

const app = new DiscordHono().component('button', c =>
c.update().res('Update Text'),
)

command, component, modal

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

.followup() は Defer の後のメッセージの更新に利用します。

第1引数は string または RESTPatchAPIInteractionOriginalResponseJSONBody です。
第2引数は FileData または FileData[] です。
FileData = { blob: Blob, name: ‘file.name’ }

また、引数を空にすることで、元のメッセージを削除します。

return c.update().resDefer(c => c.followup())

command, component, modal

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

こちらのメッセージフラグを追加します。