@chat-adapter/discord 4.17.0 → 4.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +222 -9
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -3,16 +3,18 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@chat-adapter/discord)
|
|
4
4
|
[](https://www.npmjs.com/package/@chat-adapter/discord)
|
|
5
5
|
|
|
6
|
-
Discord adapter for [Chat SDK](https://chat-sdk.dev
|
|
6
|
+
Discord adapter for [Chat SDK](https://chat-sdk.dev). Configure with HTTP Interactions and Gateway WebSocket support.
|
|
7
7
|
|
|
8
8
|
## Installation
|
|
9
9
|
|
|
10
10
|
```bash
|
|
11
|
-
|
|
11
|
+
pnpm add @chat-adapter/discord
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
|
+
The adapter auto-detects `DISCORD_BOT_TOKEN`, `DISCORD_PUBLIC_KEY`, `DISCORD_APPLICATION_ID`, and `DISCORD_MENTION_ROLE_IDS` from environment variables:
|
|
17
|
+
|
|
16
18
|
```typescript
|
|
17
19
|
import { Chat } from "chat";
|
|
18
20
|
import { createDiscordAdapter } from "@chat-adapter/discord";
|
|
@@ -20,11 +22,7 @@ import { createDiscordAdapter } from "@chat-adapter/discord";
|
|
|
20
22
|
const bot = new Chat({
|
|
21
23
|
userName: "mybot",
|
|
22
24
|
adapters: {
|
|
23
|
-
discord: createDiscordAdapter(
|
|
24
|
-
botToken: process.env.DISCORD_BOT_TOKEN!,
|
|
25
|
-
publicKey: process.env.DISCORD_PUBLIC_KEY!,
|
|
26
|
-
applicationId: process.env.DISCORD_APPLICATION_ID!,
|
|
27
|
-
}),
|
|
25
|
+
discord: createDiscordAdapter(),
|
|
28
26
|
},
|
|
29
27
|
});
|
|
30
28
|
|
|
@@ -33,9 +31,224 @@ bot.onNewMention(async (thread, message) => {
|
|
|
33
31
|
});
|
|
34
32
|
```
|
|
35
33
|
|
|
36
|
-
##
|
|
34
|
+
## Discord application setup
|
|
35
|
+
|
|
36
|
+
### 1. Create application
|
|
37
|
+
|
|
38
|
+
1. Go to the [Discord Developer Portal](https://discord.com/developers/applications)
|
|
39
|
+
2. Click **New Application** and give it a name
|
|
40
|
+
3. Note the **Application ID** from the General Information page
|
|
41
|
+
4. Copy the **Public Key** from the General Information page
|
|
42
|
+
|
|
43
|
+
### 2. Create bot
|
|
44
|
+
|
|
45
|
+
1. Go to the **Bot** section in the left sidebar
|
|
46
|
+
2. Click **Reset Token** to generate a new bot token
|
|
47
|
+
3. Copy and save the token (you won't see it again)
|
|
48
|
+
4. Enable these **Privileged Gateway Intents**:
|
|
49
|
+
- Message Content Intent
|
|
50
|
+
- Server Members Intent (if needed)
|
|
51
|
+
|
|
52
|
+
### 3. Configure interactions endpoint
|
|
53
|
+
|
|
54
|
+
1. Go to **General Information**
|
|
55
|
+
2. Set **Interactions Endpoint URL** to `https://your-domain.com/api/webhooks/discord`
|
|
56
|
+
3. Discord sends a PING to verify the endpoint
|
|
57
|
+
|
|
58
|
+
### 4. Add bot to server
|
|
59
|
+
|
|
60
|
+
1. Go to **OAuth2** then **URL Generator**
|
|
61
|
+
2. Select scopes: `bot`, `applications.commands`
|
|
62
|
+
3. Select bot permissions: Send Messages, Send Messages in Threads, Create Public Threads, Read Message History, Add Reactions, Attach Files
|
|
63
|
+
4. Copy the generated URL and open it to invite the bot to your server
|
|
64
|
+
|
|
65
|
+
## Architecture: HTTP Interactions vs Gateway
|
|
66
|
+
|
|
67
|
+
Discord has two ways to receive events:
|
|
68
|
+
|
|
69
|
+
**HTTP Interactions (default):**
|
|
70
|
+
- Receives button clicks, slash commands, and verification pings
|
|
71
|
+
- Works out of the box with serverless
|
|
72
|
+
- Does **not** receive regular messages
|
|
73
|
+
|
|
74
|
+
**Gateway WebSocket (required for messages):**
|
|
75
|
+
- Receives regular messages and reactions
|
|
76
|
+
- Requires a persistent connection
|
|
77
|
+
- In serverless environments, use a cron job to maintain the connection
|
|
78
|
+
|
|
79
|
+
## Gateway setup for serverless
|
|
80
|
+
|
|
81
|
+
### 1. Create Gateway route
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { after } from "next/server";
|
|
85
|
+
import { bot } from "@/lib/bot";
|
|
86
|
+
|
|
87
|
+
export const maxDuration = 800;
|
|
88
|
+
|
|
89
|
+
export async function GET(request: Request): Promise<Response> {
|
|
90
|
+
const cronSecret = process.env.CRON_SECRET;
|
|
91
|
+
if (!cronSecret) {
|
|
92
|
+
return new Response("CRON_SECRET not configured", { status: 500 });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const authHeader = request.headers.get("authorization");
|
|
96
|
+
if (authHeader !== `Bearer ${cronSecret}`) {
|
|
97
|
+
return new Response("Unauthorized", { status: 401 });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const durationMs = 600 * 1000;
|
|
101
|
+
const webhookUrl = `https://${process.env.VERCEL_URL}/api/webhooks/discord`;
|
|
102
|
+
|
|
103
|
+
await bot.initialize();
|
|
104
|
+
|
|
105
|
+
return bot.adapters.discord.startGatewayListener(
|
|
106
|
+
{ waitUntil: (task) => after(() => task) },
|
|
107
|
+
durationMs,
|
|
108
|
+
undefined,
|
|
109
|
+
webhookUrl
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 2. Configure Vercel Cron
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"crons": [
|
|
119
|
+
{
|
|
120
|
+
"path": "/api/discord/gateway",
|
|
121
|
+
"schedule": "*/9 * * * *"
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
This runs every 9 minutes, ensuring overlap with the 10-minute listener duration.
|
|
128
|
+
|
|
129
|
+
### 3. Add environment variables
|
|
130
|
+
|
|
131
|
+
Add `CRON_SECRET` to your Vercel project settings.
|
|
132
|
+
|
|
133
|
+
## Role mentions
|
|
134
|
+
|
|
135
|
+
By default, only direct user mentions (`@BotName`) trigger `onNewMention` handlers. To also trigger on role mentions (e.g., `@AI`):
|
|
136
|
+
|
|
137
|
+
1. Create a role in your Discord server (e.g., "AI")
|
|
138
|
+
2. Assign the role to your bot
|
|
139
|
+
3. Copy the role ID (right-click role in server settings with Developer Mode enabled)
|
|
140
|
+
4. Add to `mentionRoleIds`:
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
createDiscordAdapter({
|
|
144
|
+
mentionRoleIds: ["1457473602180878604"],
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Or set `DISCORD_MENTION_ROLE_IDS` as a comma-separated string in your environment variables.
|
|
149
|
+
|
|
150
|
+
## Configuration
|
|
151
|
+
|
|
152
|
+
All options are auto-detected from environment variables when not provided.
|
|
153
|
+
|
|
154
|
+
| Option | Required | Description |
|
|
155
|
+
|--------|----------|-------------|
|
|
156
|
+
| `botToken` | No* | Discord bot token. Auto-detected from `DISCORD_BOT_TOKEN` |
|
|
157
|
+
| `publicKey` | No* | Application public key. Auto-detected from `DISCORD_PUBLIC_KEY` |
|
|
158
|
+
| `applicationId` | No* | Discord application ID. Auto-detected from `DISCORD_APPLICATION_ID` |
|
|
159
|
+
| `mentionRoleIds` | No | Array of role IDs that trigger mention handlers. Auto-detected from `DISCORD_MENTION_ROLE_IDS` (comma-separated) |
|
|
160
|
+
| `logger` | No | Logger instance (defaults to `ConsoleLogger("info")`) |
|
|
161
|
+
|
|
162
|
+
*`botToken`, `publicKey`, and `applicationId` are required — either via config or env vars.
|
|
163
|
+
|
|
164
|
+
## Environment variables
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
DISCORD_BOT_TOKEN=your-bot-token
|
|
168
|
+
DISCORD_PUBLIC_KEY=your-application-public-key
|
|
169
|
+
DISCORD_APPLICATION_ID=your-application-id
|
|
170
|
+
DISCORD_MENTION_ROLE_IDS=1234567890,0987654321 # Optional
|
|
171
|
+
CRON_SECRET=your-random-secret # For Gateway cron
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Features
|
|
175
|
+
|
|
176
|
+
### Messaging
|
|
177
|
+
|
|
178
|
+
| Feature | Supported |
|
|
179
|
+
|---------|-----------|
|
|
180
|
+
| Post message | Yes |
|
|
181
|
+
| Edit message | Yes |
|
|
182
|
+
| Delete message | Yes |
|
|
183
|
+
| File uploads | Yes |
|
|
184
|
+
| Streaming | Post+Edit fallback |
|
|
185
|
+
|
|
186
|
+
### Rich content
|
|
187
|
+
|
|
188
|
+
| Feature | Supported |
|
|
189
|
+
|---------|-----------|
|
|
190
|
+
| Card format | Embeds |
|
|
191
|
+
| Buttons | Yes |
|
|
192
|
+
| Link buttons | Yes |
|
|
193
|
+
| Select menus | No |
|
|
194
|
+
| Tables | GFM |
|
|
195
|
+
| Fields | Yes |
|
|
196
|
+
| Images in cards | Yes |
|
|
197
|
+
| Modals | No |
|
|
198
|
+
|
|
199
|
+
### Conversations
|
|
200
|
+
|
|
201
|
+
| Feature | Supported |
|
|
202
|
+
|---------|-----------|
|
|
203
|
+
| Slash commands | Yes |
|
|
204
|
+
| Mentions | Yes |
|
|
205
|
+
| Add reactions | Yes |
|
|
206
|
+
| Remove reactions | Yes |
|
|
207
|
+
| Typing indicator | Yes |
|
|
208
|
+
| DMs | Yes |
|
|
209
|
+
| Ephemeral messages | No (DM fallback) |
|
|
210
|
+
|
|
211
|
+
### Message history
|
|
212
|
+
|
|
213
|
+
| Feature | Supported |
|
|
214
|
+
|---------|-----------|
|
|
215
|
+
| Fetch messages | Yes |
|
|
216
|
+
| Fetch single message | No |
|
|
217
|
+
| Fetch thread info | Yes |
|
|
218
|
+
| Fetch channel messages | Yes |
|
|
219
|
+
| List threads | Yes |
|
|
220
|
+
| Fetch channel info | Yes |
|
|
221
|
+
| Post channel message | Yes |
|
|
222
|
+
|
|
223
|
+
## Testing
|
|
224
|
+
|
|
225
|
+
Run a local tunnel (e.g., ngrok) to test webhooks locally:
|
|
226
|
+
|
|
227
|
+
```bash
|
|
228
|
+
ngrok http 3000
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Update the Interactions Endpoint URL in the Discord Developer Portal to your ngrok URL.
|
|
232
|
+
|
|
233
|
+
## Troubleshooting
|
|
234
|
+
|
|
235
|
+
### Bot not responding to messages
|
|
236
|
+
|
|
237
|
+
1. **Check Gateway connection**: Messages require the Gateway WebSocket, not just HTTP interactions
|
|
238
|
+
2. **Verify Message Content Intent**: Enable this in the Bot settings
|
|
239
|
+
3. **Check bot permissions**: Ensure the bot can read messages in the channel
|
|
240
|
+
|
|
241
|
+
### Role mentions not triggering
|
|
242
|
+
|
|
243
|
+
1. **Verify role ID**: Enable Developer Mode in Discord settings, then right-click the role
|
|
244
|
+
2. **Check `mentionRoleIds` config**: Ensure the role ID is in the array
|
|
245
|
+
3. **Confirm bot has the role**: The bot must have the role assigned
|
|
246
|
+
|
|
247
|
+
### Signature verification failing
|
|
37
248
|
|
|
38
|
-
|
|
249
|
+
1. **Check public key format**: Should be a 64-character hex string (lowercase)
|
|
250
|
+
2. **Verify endpoint URL**: Must exactly match what's configured in Discord Developer Portal
|
|
251
|
+
3. **Check for body parsing**: Don't parse the request body before verification
|
|
39
252
|
|
|
40
253
|
## License
|
|
41
254
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chat-adapter/discord",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.19.0",
|
|
4
4
|
"description": "Discord adapter for chat",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"discord-api-types": "^0.37.119",
|
|
20
20
|
"discord-interactions": "^4.4.0",
|
|
21
21
|
"discord.js": "^14.25.1",
|
|
22
|
-
"@chat-adapter/shared": "4.
|
|
23
|
-
"chat": "4.
|
|
22
|
+
"@chat-adapter/shared": "4.19.0",
|
|
23
|
+
"chat": "4.19.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@types/node": "^25.3.2",
|