diff --git a/README.md b/README.md index aa2e741..fce0f00 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ $ bun install 1. [Discord Developer Potal](https://discord.com/developers/applications) から新規に Discord App を登録し、`Application ID`, `Public Key`, `Token` を取得します. 2. Discord App の OAuth2 から `bot` スコープを選択し、`Manage Roles`, `Send Messages`, `Use Slash Commands` の権限を付与します. 3. 2 で生成された URL にアクセスし、bot をサーバーに追加します. -4. `bun run ./scriptd/registerSlashCommands.ts` を実行し、Slash Commands を登録します. +4. `bun run ./scripts/registerSlashCommands.ts` を実行し、Slash Commands を登録します.このとき、`.env` ファイルに `DISCORD_APPLICATION_ID`を設定してください. 5. 1 で取得した情報をそれぞれ`DISCORD_APPLICATION_ID`, `DISCORD_PUBLIC_KEY`, `DISCORD_TOKEN` を環境変数として設定します. ```bash diff --git a/scripts/registerSlashCommand.ts b/scripts/registerSlashCommand.ts index 08a925f..1557dd8 100644 --- a/scripts/registerSlashCommand.ts +++ b/scripts/registerSlashCommand.ts @@ -1,13 +1,19 @@ // ref: https://discord.com/developers/docs/tutorials/hosting-on-cloudflare-workers#registering-commands -import { REGISTER_COMMAND } from "../src/commands" +import { REGISTER_COMMAND, HELP_COMMAND, PING_COMMAND } from "../src/commands" -const register = async () => { +const commands = [ + REGISTER_COMMAND, + HELP_COMMAND, + PING_COMMAND +] + +const register = async (command: any) => { const json = { - name: REGISTER_COMMAND.name, - description: REGISTER_COMMAND.description, - type: REGISTER_COMMAND.type, - options: REGISTER_COMMAND.options, + name: command.name, + description: command.description, + type: command.type, + options: command?.options, } const headers = { 'Authorization': `Bot ${process.env.DISCORD_TOKEN}`, @@ -28,4 +34,4 @@ const register = async () => { }); } -await register() +commands.forEach(register) diff --git a/src/commands.ts b/src/commands.ts index 590cb9b..e2dbd48 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -12,3 +12,15 @@ export const REGISTER_COMMAND = { }, ], } + +export const HELP_COMMAND = { + name: 'help', + description: 'ヘルプを表示します', + type: 1, +} + +export const PING_COMMAND = { + name: 'ping', + description: 'Ping!', + type: 1, +} diff --git a/src/index.ts b/src/index.ts index 6c6f67c..b45f0cc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { Hono } from 'hono' import { logger } from 'hono/logger' import { APIInteraction, InteractionType, InteractionResponseType, APIInteractionResponse, ApplicationCommandType } from 'discord-api-types/v10' import { verifyDiscordInteraction } from './verifyDiscordInteraction' -import { REGISTER_COMMAND } from './commands' +import { HELP_COMMAND, PING_COMMAND, REGISTER_COMMAND } from './commands' import { assignRoleToUser, getGuildRoles, createGuildRole, removeUserRole } from './role' const app = new Hono() @@ -14,6 +14,10 @@ app.get('/', (c) => { return c.text('Hello World') }) +app.get('/status', (c) => { + return c.json({ status: 'ok' }) +}); + app.post('/', verifyDiscordInteraction, async (c) => { const message: APIInteraction = await c.req.json() @@ -94,6 +98,18 @@ app.post('/', verifyDiscordInteraction, async (c) => { }); }; } + case HELP_COMMAND.name.toLowerCase(): + return c.json({ + type: InteractionResponseType.ChannelMessageWithSource, data: { + content: '使い方: /register --year <入学年度>\n' + } + }); + case PING_COMMAND.name.toLowerCase(): + return c.json({ + type: InteractionResponseType.ChannelMessageWithSource, data: { + content: 'Pong!' + } + }); default: return c.json({ error: 'Unknown Command' }, 400); }