Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
eno1220 committed Jul 24, 2024
1 parent a809de4 commit 670f3b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 13 additions & 7 deletions scripts/registerSlashCommand.ts
Original file line number Diff line number Diff line change
@@ -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}`,
Expand All @@ -28,4 +34,4 @@ const register = async () => {
});
}

await register()
commands.forEach(register)
12 changes: 12 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 670f3b7

Please sign in to comment.