@elizaos/plugin-discord 2.0.0-alpha.4 → 2.0.0-alpha.537

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.
Files changed (139) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +549 -0
  3. package/dist/accounts.d.ts +164 -0
  4. package/dist/accounts.d.ts.map +1 -0
  5. package/dist/actions/actionResultSemantics.d.ts +9 -0
  6. package/dist/actions/actionResultSemantics.d.ts.map +1 -0
  7. package/dist/actions/chatWithAttachments.d.ts +4 -0
  8. package/dist/actions/chatWithAttachments.d.ts.map +1 -0
  9. package/dist/actions/createPoll.d.ts +4 -0
  10. package/dist/actions/createPoll.d.ts.map +1 -0
  11. package/dist/actions/deleteMessage.d.ts +4 -0
  12. package/dist/actions/deleteMessage.d.ts.map +1 -0
  13. package/dist/actions/downloadMedia.d.ts +4 -0
  14. package/dist/actions/downloadMedia.d.ts.map +1 -0
  15. package/dist/actions/editMessage.d.ts +4 -0
  16. package/dist/actions/editMessage.d.ts.map +1 -0
  17. package/dist/actions/getUserInfo.d.ts +4 -0
  18. package/dist/actions/getUserInfo.d.ts.map +1 -0
  19. package/dist/actions/joinChannel.d.ts +4 -0
  20. package/dist/actions/joinChannel.d.ts.map +1 -0
  21. package/dist/actions/leaveChannel.d.ts +4 -0
  22. package/dist/actions/leaveChannel.d.ts.map +1 -0
  23. package/dist/actions/listChannels.d.ts +4 -0
  24. package/dist/actions/listChannels.d.ts.map +1 -0
  25. package/dist/actions/pinMessage.d.ts +4 -0
  26. package/dist/actions/pinMessage.d.ts.map +1 -0
  27. package/dist/actions/reactToMessage.d.ts +4 -0
  28. package/dist/actions/reactToMessage.d.ts.map +1 -0
  29. package/dist/actions/readChannel.d.ts +4 -0
  30. package/dist/actions/readChannel.d.ts.map +1 -0
  31. package/dist/actions/searchMessages.d.ts +4 -0
  32. package/dist/actions/searchMessages.d.ts.map +1 -0
  33. package/dist/actions/sendDM.d.ts +4 -0
  34. package/dist/actions/sendDM.d.ts.map +1 -0
  35. package/dist/actions/sendMessage.d.ts +4 -0
  36. package/dist/actions/sendMessage.d.ts.map +1 -0
  37. package/dist/actions/serverInfo.d.ts +4 -0
  38. package/dist/actions/serverInfo.d.ts.map +1 -0
  39. package/dist/actions/setup-credentials.d.ts +25 -0
  40. package/dist/actions/setup-credentials.d.ts.map +1 -0
  41. package/dist/actions/summarizeConversation.d.ts +4 -0
  42. package/dist/actions/summarizeConversation.d.ts.map +1 -0
  43. package/dist/actions/transcribeMedia.d.ts +4 -0
  44. package/dist/actions/transcribeMedia.d.ts.map +1 -0
  45. package/dist/actions/unpinMessage.d.ts +4 -0
  46. package/dist/actions/unpinMessage.d.ts.map +1 -0
  47. package/dist/allowlist.d.ts +198 -0
  48. package/dist/allowlist.d.ts.map +1 -0
  49. package/dist/attachments.d.ts +93 -0
  50. package/dist/attachments.d.ts.map +1 -0
  51. package/dist/banner.d.ts +39 -0
  52. package/dist/banner.d.ts.map +1 -0
  53. package/dist/compat.d.ts +57 -0
  54. package/dist/compat.d.ts.map +1 -0
  55. package/dist/config.d.ts +156 -0
  56. package/dist/config.d.ts.map +1 -0
  57. package/dist/constants.d.ts +2 -0
  58. package/dist/constants.d.ts.map +1 -0
  59. package/dist/debouncer.d.ts +25 -0
  60. package/dist/debouncer.d.ts.map +1 -0
  61. package/dist/discord-commands.d.ts +16 -0
  62. package/dist/discord-commands.d.ts.map +1 -0
  63. package/dist/discord-events.d.ts +54 -0
  64. package/dist/discord-events.d.ts.map +1 -0
  65. package/dist/discord-history.d.ts +49 -0
  66. package/dist/discord-history.d.ts.map +1 -0
  67. package/dist/discord-interactions.d.ts +45 -0
  68. package/dist/discord-interactions.d.ts.map +1 -0
  69. package/dist/discord-local-service.d.ts +94 -0
  70. package/dist/discord-local-service.d.ts.map +1 -0
  71. package/dist/discord-reactions.d.ts +29 -0
  72. package/dist/discord-reactions.d.ts.map +1 -0
  73. package/dist/draft-chunking.d.ts +9 -0
  74. package/dist/draft-chunking.d.ts.map +1 -0
  75. package/dist/draft-stream.d.ts +23 -0
  76. package/dist/draft-stream.d.ts.map +1 -0
  77. package/dist/environment.d.ts +23 -0
  78. package/dist/environment.d.ts.map +1 -0
  79. package/dist/generated/prompts/typescript/prompts.d.ts +44 -0
  80. package/dist/generated/prompts/typescript/prompts.d.ts.map +1 -0
  81. package/dist/generated/specs/spec-helpers.d.ts +49 -0
  82. package/dist/generated/specs/spec-helpers.d.ts.map +1 -0
  83. package/dist/generated/specs/specs.d.ts +90 -0
  84. package/dist/generated/specs/specs.d.ts.map +1 -0
  85. package/dist/identity.d.ts +8 -0
  86. package/dist/identity.d.ts.map +1 -0
  87. package/dist/inbound-envelope.d.ts +8 -0
  88. package/dist/inbound-envelope.d.ts.map +1 -0
  89. package/dist/index.browser.d.ts +34 -0
  90. package/dist/index.browser.d.ts.map +1 -0
  91. package/dist/index.d.ts +19 -2
  92. package/dist/index.d.ts.map +1 -0
  93. package/dist/index.js +15379 -0
  94. package/dist/index.js.map +72 -0
  95. package/dist/messages.d.ts +62 -0
  96. package/dist/messages.d.ts.map +1 -0
  97. package/dist/messaging.d.ts +129 -0
  98. package/dist/messaging.d.ts.map +1 -0
  99. package/dist/native-commands.d.ts +142 -0
  100. package/dist/native-commands.d.ts.map +1 -0
  101. package/dist/owner-pairing-service.d.ts +73 -0
  102. package/dist/owner-pairing-service.d.ts.map +1 -0
  103. package/dist/permissionEvents.d.ts +42 -0
  104. package/dist/permissionEvents.d.ts.map +1 -0
  105. package/dist/permissions.d.ts +36 -0
  106. package/dist/permissions.d.ts.map +1 -0
  107. package/dist/profileSync.d.ts +8 -0
  108. package/dist/profileSync.d.ts.map +1 -0
  109. package/dist/providers/channelState.d.ts +14 -0
  110. package/dist/providers/channelState.d.ts.map +1 -0
  111. package/dist/providers/guildInfo.d.ts +15 -0
  112. package/dist/providers/guildInfo.d.ts.map +1 -0
  113. package/dist/providers/voiceState.d.ts +12 -0
  114. package/dist/providers/voiceState.d.ts.map +1 -0
  115. package/dist/reasoning-tags.d.ts +2 -0
  116. package/dist/reasoning-tags.d.ts.map +1 -0
  117. package/dist/service.d.ts +207 -0
  118. package/dist/service.d.ts.map +1 -0
  119. package/dist/setup-routes.d.ts +23 -0
  120. package/dist/setup-routes.d.ts.map +1 -0
  121. package/dist/slash-commands.d.ts +39 -0
  122. package/dist/slash-commands.d.ts.map +1 -0
  123. package/dist/status-reactions.d.ts +11 -0
  124. package/dist/status-reactions.d.ts.map +1 -0
  125. package/dist/test-setup.d.ts +1 -0
  126. package/dist/test-setup.d.ts.map +1 -0
  127. package/dist/tests.d.ts +123 -0
  128. package/dist/tests.d.ts.map +1 -0
  129. package/dist/types.d.ts +329 -0
  130. package/dist/types.d.ts.map +1 -0
  131. package/dist/typing.d.ts +7 -0
  132. package/dist/typing.d.ts.map +1 -0
  133. package/dist/utils.d.ts +48 -0
  134. package/dist/utils.d.ts.map +1 -0
  135. package/dist/vitest.config.d.ts +3 -0
  136. package/dist/vitest.config.d.ts.map +1 -0
  137. package/dist/voice.d.ts +234 -0
  138. package/dist/voice.d.ts.map +1 -0
  139. package/package.json +25 -20
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Shaw Walters and elizaOS Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,549 @@
1
+ # @elizaos/plugin-discord
2
+
3
+ A Discord plugin implementation for ElizaOS, enabling rich integration with Discord servers for managing interactions, voice, and message handling.
4
+
5
+ ## Features
6
+
7
+ - Handle server join events and manage initial configurations
8
+ - Voice event management via the voice manager
9
+ - Manage and process new messages with the message manager
10
+ - Slash command registration and interaction handling
11
+ - Support for Discord attachments and media files
12
+ - Voice channel join/leave functionality
13
+ - Conversation summarization
14
+ - Media transcription capabilities
15
+ - Channel state and voice state providers
16
+ - Channel restriction support (limit bot to specific channels)
17
+ - Robust permissions management and audit event tracking
18
+ - Event-driven architecture with comprehensive event handling
19
+ - History backfill with efficient batch processing
20
+
21
+ ## Installation
22
+
23
+ As this is a workspace package, it's installed as part of the ElizaOS monorepo:
24
+
25
+ ```bash
26
+ bun install
27
+ ```
28
+
29
+ ## Configuration
30
+
31
+ The plugin requires the following environment variables:
32
+
33
+ ```bash
34
+ # Discord API Credentials (Required)
35
+ DISCORD_APPLICATION_ID=your_application_id
36
+ DISCORD_API_TOKEN=your_api_token
37
+
38
+ # Channel Restrictions (Optional)
39
+ # Comma-separated list of Discord channel IDs to restrict the bot to.
40
+ # If not set, the bot operates in all channels.
41
+ # These channels cannot be removed via the leaveChannel action.
42
+ CHANNEL_IDS=123456789012345678,987654321098765432
43
+
44
+ # Listen-only channels (Optional)
45
+ # Comma-separated list of channel IDs where the bot will only listen (not respond).
46
+ DISCORD_LISTEN_CHANNEL_IDS=123456789012345678
47
+
48
+ # Voice Channel (Optional)
49
+ # ID of the voice channel the bot should auto-join when scanning a guild.
50
+ # If not set, the bot selects based on member activity.
51
+ DISCORD_VOICE_CHANNEL_ID=123456789012345678
52
+
53
+ # Behavior Settings (Optional)
54
+ # If true, ignore messages from other bots (default: true)
55
+ DISCORD_SHOULD_IGNORE_BOT_MESSAGES=true
56
+
57
+ # If true, ignore direct messages by default (default: true).
58
+ # DMs can still be allowed explicitly via DISCORD_ALLOW_FROM / pairing allowlist.
59
+ DISCORD_SHOULD_IGNORE_DIRECT_MESSAGES=true
60
+
61
+ # If true, only respond when explicitly @mentioned (default: true)
62
+ DISCORD_SHOULD_RESPOND_ONLY_TO_MENTIONS=true
63
+
64
+ # Testing (Optional)
65
+ DISCORD_TEST_CHANNEL_ID=123456789012345678
66
+ ```
67
+
68
+ Settings can also be configured in your character file under `settings.discord`:
69
+
70
+ ```json
71
+ {
72
+ "settings": {
73
+ "discord": {
74
+ "shouldIgnoreBotMessages": true,
75
+ "shouldIgnoreDirectMessages": true,
76
+ "shouldRespondOnlyToMentions": true,
77
+ "allowedChannelIds": ["123456789012345678"]
78
+ }
79
+ }
80
+ }
81
+ ```
82
+
83
+ ## Usage
84
+
85
+ ```json
86
+ {
87
+ "plugins": ["@elizaos/plugin-discord"]
88
+ }
89
+ ```
90
+
91
+ ## Slash Command Permissions
92
+
93
+ The plugin uses a hybrid permission system that combines Discord's native features with ElizaOS-specific controls.
94
+
95
+ ### Permission Layers
96
+
97
+ Commands go through multiple permission checks in this order:
98
+
99
+ 1. **Discord Native Checks** (before interaction fires):
100
+ - User must have required Discord permissions
101
+ - Command must be available in the current context (guild vs DM)
102
+
103
+ 2. **ElizaOS Channel Whitelist** (if `CHANNEL_IDS` is set):
104
+ - Commands only work in whitelisted channels
105
+ - Unless command has `bypassChannelWhitelist: true`
106
+
107
+ 3. **Custom Validator** (if provided):
108
+ - Runs custom validation logic
109
+ - Full programmatic control
110
+
111
+ ### Registering Commands
112
+
113
+ ```typescript
114
+ import { PermissionFlagsBits } from "discord.js";
115
+
116
+ // Simple command (works everywhere)
117
+ const helpCommand = {
118
+ name: "help",
119
+ description: "Show help information",
120
+ };
121
+
122
+ // Guild-only command
123
+ const serverInfoCommand = {
124
+ name: "serverinfo",
125
+ description: "Show server information",
126
+ guildOnly: true,
127
+ };
128
+
129
+ // Requires Discord permission
130
+ const configCommand = {
131
+ name: "config",
132
+ description: "Configure bot settings",
133
+ requiredPermissions: PermissionFlagsBits.ManageGuild,
134
+ };
135
+
136
+ // Bypasses channel whitelist
137
+ const utilityCommand = {
138
+ name: "export",
139
+ description: "Export data",
140
+ bypassChannelWhitelist: true,
141
+ };
142
+
143
+ // Advanced: custom validation
144
+ const adminCommand = {
145
+ name: "admin",
146
+ description: "Admin-only command",
147
+ validator: async (interaction, runtime) => {
148
+ const adminIds = runtime.getSetting("ADMIN_USER_IDS")?.split(",") ?? [];
149
+ return adminIds.includes(interaction.user.id);
150
+ },
151
+ };
152
+
153
+ // Register commands
154
+ await runtime.emitEvent(["DISCORD_REGISTER_COMMANDS"], {
155
+ commands: [
156
+ helpCommand,
157
+ serverInfoCommand,
158
+ configCommand,
159
+ utilityCommand,
160
+ adminCommand,
161
+ ],
162
+ });
163
+ ```
164
+
165
+ ### Permission Options
166
+
167
+ | Option | Type | Description |
168
+ | ------------------------ | ------------------ | --------------------------------------------------------------------- |
169
+ | `guildOnly` | `boolean` | If true, command only works in guilds (not DMs) |
170
+ | `bypassChannelWhitelist` | `boolean` | If true, bypasses `CHANNEL_IDS` restrictions |
171
+ | `requiredPermissions` | `bigint \| string` | Discord permission bitfield (e.g., `PermissionFlagsBits.ManageGuild`) |
172
+ | `contexts` | `number[]` | Raw Discord contexts (0=Guild, 1=BotDM, 2=PrivateChannel) |
173
+ | `guildIds` | `string[]` | Register only in specific guilds (instant updates) |
174
+ | `validator` | `function` | Custom validation function for advanced logic |
175
+
176
+ ### Common Permission Values
177
+
178
+ From Discord.js `PermissionFlagsBits`:
179
+
180
+ - `ManageGuild` - Server settings
181
+ - `ManageChannels` - Channel management
182
+ - `ManageMessages` - Delete messages
183
+ - `BanMembers` - Ban users
184
+ - `KickMembers` - Kick users
185
+ - `ModerateMembers` - Timeout users
186
+ - `ManageRoles` - Role management
187
+ - `Administrator` - Full access
188
+
189
+ ### Design Rationale
190
+
191
+ **Why Hybrid Approach?**
192
+
193
+ - Discord's native permissions are powerful but limited to role-based access
194
+ - ElizaOS needs programmatic control for channel restrictions and custom logic
195
+ - Combining both gives developers the best of both worlds
196
+
197
+ **Why Simple Flags?**
198
+
199
+ - `guildOnly: true` is clearer than `contexts: [0]`
200
+ - Abstracts Discord API details
201
+ - Sensible defaults: zero config should "just work"
202
+
203
+ **Why Keep Channel Whitelist?**
204
+
205
+ - Discord's channel permissions are UI-based (Server Settings > Integrations)
206
+ - Programmatic control is better for developer experience
207
+ - Allows dynamic, runtime-based channel restrictions
208
+
209
+ ### Available Actions
210
+
211
+ The plugin provides the following actions:
212
+
213
+ | Action | Description |
214
+ | ----------------------- | ------------------------------------------ |
215
+ | **chatWithAttachments** | Handle messages with Discord attachments |
216
+ | **createPoll** | Create a poll in a Discord channel |
217
+ | **downloadMedia** | Download media files from Discord messages |
218
+ | **getUserInfo** | Get information about a Discord user |
219
+ | **joinVoice** | Join a voice channel |
220
+ | **leaveVoice** | Leave a voice channel |
221
+ | **listChannels** | List channels in a Discord server |
222
+ | **pinMessage** | Pin a message in a channel |
223
+ | **reactToMessage** | Add a reaction to a message |
224
+ | **readChannel** | Read messages from a channel |
225
+ | **searchMessages** | Search for messages in a channel |
226
+ | **sendDM** | Send a direct message to a user |
227
+ | **serverInfo** | Get information about the current server |
228
+ | **summarize** | Summarize conversation history |
229
+ | **transcribeMedia** | Transcribe audio/video media to text |
230
+ | **unpinMessage** | Unpin a message from a channel |
231
+
232
+ ### Providers
233
+
234
+ The plugin includes two state providers:
235
+
236
+ 1. **channelStateProvider** - Provides state information about Discord channels
237
+ 2. **voiceStateProvider** - Provides state information about voice channels and connection status
238
+
239
+ ### Event Types
240
+
241
+ The plugin emits the following Discord-specific events:
242
+
243
+ | Event | Description |
244
+ | ------------------------------------- | ----------------------------------------- |
245
+ | `DISCORD_MESSAGE_RECEIVED` | When a message is received |
246
+ | `DISCORD_MESSAGE_SENT` | When a message is sent |
247
+ | `DISCORD_SLASH_COMMAND` | When a slash command is invoked |
248
+ | `DISCORD_MODAL_SUBMIT` | When a modal form is submitted |
249
+ | `DISCORD_REACTION_RECEIVED` | When a reaction is added to a message |
250
+ | `DISCORD_REACTION_REMOVED` | When a reaction is removed from a message |
251
+ | `DISCORD_WORLD_JOINED` | When the bot joins a guild |
252
+ | `DISCORD_SERVER_CONNECTED` | When connected to a server |
253
+ | `DISCORD_USER_JOINED` | When a user joins a guild |
254
+ | `DISCORD_USER_LEFT` | When a user leaves a guild |
255
+ | `DISCORD_VOICE_STATE_CHANGED` | When voice state changes |
256
+ | `DISCORD_CHANNEL_PERMISSIONS_CHANGED` | When channel permissions change |
257
+ | `DISCORD_ROLE_PERMISSIONS_CHANGED` | When role permissions change |
258
+ | `DISCORD_MEMBER_ROLES_CHANGED` | When a member's roles change |
259
+ | `DISCORD_ROLE_CREATED` | When a role is created |
260
+ | `DISCORD_ROLE_DELETED` | When a role is deleted |
261
+
262
+ ## Key Components
263
+
264
+ ### DiscordService
265
+
266
+ Main service class that extends ElizaOS Service:
267
+
268
+ - Handles authentication and session management
269
+ - Manages Discord client connection
270
+ - Processes events and interactions
271
+ - Supports channel history backfill with efficient batch processing
272
+
273
+ ### MessageManager
274
+
275
+ - Processes incoming messages and responses
276
+ - Handles attachments and media files
277
+ - Supports message formatting and templating
278
+ - Manages conversation context
279
+
280
+ ### VoiceManager
281
+
282
+ - Manages voice channel interactions
283
+ - Handles joining and leaving voice channels
284
+ - Processes voice events and audio streams
285
+ - Integrates with transcription services
286
+
287
+ ### Attachment Handler
288
+
289
+ - Downloads and processes Discord attachments
290
+ - Supports various media types
291
+ - Integrates with media transcription
292
+
293
+ ## Developer Guide
294
+
295
+ ### Custom Slash Commands
296
+
297
+ Register slash commands via the `DISCORD_REGISTER_COMMANDS` event, then listen for interactions:
298
+
299
+ ```typescript
300
+ // Register custom slash commands
301
+ await runtime.emitEvent(["DISCORD_REGISTER_COMMANDS"], {
302
+ commands: [
303
+ {
304
+ name: "mycommand",
305
+ description: "My custom command",
306
+ options: [
307
+ {
308
+ name: "input",
309
+ description: "User input",
310
+ type: 3, // STRING type
311
+ required: true,
312
+ },
313
+ ],
314
+ },
315
+ {
316
+ name: "serverinfo",
317
+ description: "Get server information",
318
+ guildOnly: true, // Only works in guilds, not DMs
319
+ },
320
+ ],
321
+ });
322
+
323
+ // Listen for slash command events to handle the interaction
324
+ runtime.registerEvent({
325
+ name: "DISCORD_SLASH_COMMAND",
326
+ handler: async (payload) => {
327
+ const { interaction, client, commands } = payload;
328
+
329
+ if (interaction.commandName === "mycommand") {
330
+ const input = interaction.options.getString("input");
331
+ await interaction.reply(`You said: ${input}`);
332
+ }
333
+ },
334
+ });
335
+ ```
336
+
337
+ ### Building on the Listen System
338
+
339
+ The `DISCORD_LISTEN_CHANNEL_IDS` setting creates "listen-only" channels where the bot receives messages but doesn't respond. This is useful for:
340
+
341
+ - **Monitoring channels** - Track activity without interrupting conversations
342
+ - **Data collection** - Gather messages for analysis or training
343
+ - **Conditional responses** - Build custom logic that decides when to respond
344
+
345
+ ```typescript
346
+ // Check if a channel is listen-only
347
+ const listenChannels = runtime.getSetting("DISCORD_LISTEN_CHANNEL_IDS");
348
+ const listenChannelIds = listenChannels?.split(",").map((s) => s.trim()) || [];
349
+
350
+ runtime.registerEvent({
351
+ name: "DISCORD_MESSAGE_RECEIVED",
352
+ handler: async (payload) => {
353
+ const { message } = payload;
354
+ const channelId = message.content.channelId;
355
+
356
+ if (listenChannelIds.includes(channelId)) {
357
+ // This is a listen-only channel - process without responding
358
+ await processMessageSilently(message);
359
+ }
360
+ },
361
+ });
362
+ ```
363
+
364
+ ### Handling Modal and Component Interactions
365
+
366
+ Modal submits and message components (buttons, select menus) bypass channel whitelists to support multi-step UI flows:
367
+
368
+ ```typescript
369
+ // Listen for modal submissions
370
+ runtime.registerEvent({
371
+ name: "DISCORD_MODAL_SUBMIT",
372
+ handler: async (payload) => {
373
+ const { interaction } = payload;
374
+ const fieldValue = interaction.fields.getTextInputValue("myField");
375
+ await interaction.reply(`Received: ${fieldValue}`);
376
+ },
377
+ });
378
+ ```
379
+
380
+ ### Permission Audit System
381
+
382
+ The plugin includes a comprehensive permission audit system that tracks all permission changes with full audit log integration. This is useful for:
383
+
384
+ - **Security monitoring** - Detect unauthorized permission escalations
385
+ - **Compliance logging** - Maintain records of who changed what and when
386
+ - **Bot self-protection** - Detect when the bot's permissions are modified
387
+
388
+ #### Event Payloads
389
+
390
+ **DISCORD_CHANNEL_PERMISSIONS_CHANGED** - When channel overwrites change:
391
+
392
+ ```typescript
393
+ interface ChannelPermissionsChangedPayload {
394
+ runtime: IAgentRuntime;
395
+ guild: { id: string; name: string };
396
+ channel: { id: string; name: string };
397
+ target: { type: "role" | "user"; id: string; name: string };
398
+ action: "CREATE" | "UPDATE" | "DELETE";
399
+ changes: Array<{
400
+ permission: string; // e.g., 'ManageMessages', 'Administrator'
401
+ oldState: "ALLOW" | "DENY" | "NEUTRAL";
402
+ newState: "ALLOW" | "DENY" | "NEUTRAL";
403
+ }>;
404
+ audit: {
405
+ executorId: string;
406
+ executorTag: string;
407
+ reason: string | null;
408
+ } | null;
409
+ }
410
+ ```
411
+
412
+ **DISCORD_ROLE_PERMISSIONS_CHANGED** - When role permissions change:
413
+
414
+ ```typescript
415
+ interface RolePermissionsChangedPayload {
416
+ runtime: IAgentRuntime;
417
+ guild: { id: string; name: string };
418
+ role: { id: string; name: string };
419
+ changes: PermissionDiff[];
420
+ audit: AuditInfo | null;
421
+ }
422
+ ```
423
+
424
+ **DISCORD_MEMBER_ROLES_CHANGED** - When a member's roles change:
425
+
426
+ ```typescript
427
+ interface MemberRolesChangedPayload {
428
+ runtime: IAgentRuntime;
429
+ guild: { id: string; name: string };
430
+ member: { id: string; tag: string };
431
+ added: Array<{ id: string; name: string; permissions: string[] }>;
432
+ removed: Array<{ id: string; name: string; permissions: string[] }>;
433
+ audit: AuditInfo | null;
434
+ }
435
+ ```
436
+
437
+ **DISCORD_ROLE_CREATED / DISCORD_ROLE_DELETED** - Role lifecycle:
438
+
439
+ ```typescript
440
+ interface RoleLifecyclePayload {
441
+ runtime: IAgentRuntime;
442
+ guild: { id: string; name: string };
443
+ role: { id: string; name: string; permissions: string[] };
444
+ audit: AuditInfo | null;
445
+ }
446
+ ```
447
+
448
+ #### Example: Security Monitoring
449
+
450
+ ```typescript
451
+ import { DiscordEventTypes } from "@elizaos/plugin-discord";
452
+
453
+ // Alert on dangerous permission grants
454
+ runtime.registerEvent({
455
+ name: DiscordEventTypes.CHANNEL_PERMISSIONS_CHANGED,
456
+ handler: async (payload) => {
457
+ const dangerousPerms = ["Administrator", "ManageGuild", "ManageRoles"];
458
+
459
+ for (const change of payload.changes) {
460
+ if (
461
+ dangerousPerms.includes(change.permission) &&
462
+ change.newState === "ALLOW"
463
+ ) {
464
+ console.warn(`⚠️ Dangerous permission granted!`, {
465
+ channel: payload.channel.name,
466
+ target: payload.target.name,
467
+ permission: change.permission,
468
+ grantedBy: payload.audit?.executorTag || "Unknown",
469
+ });
470
+ }
471
+ }
472
+ },
473
+ });
474
+
475
+ // Track role escalations
476
+ runtime.registerEvent({
477
+ name: DiscordEventTypes.MEMBER_ROLES_CHANGED,
478
+ handler: async (payload) => {
479
+ const adminRoles = payload.added.filter((r) =>
480
+ r.permissions.includes("Administrator"),
481
+ );
482
+
483
+ if (adminRoles.length > 0) {
484
+ console.warn(`⚠️ Admin role granted to ${payload.member.tag}`, {
485
+ roles: adminRoles.map((r) => r.name),
486
+ grantedBy: payload.audit?.executorTag || "Unknown",
487
+ });
488
+ }
489
+ },
490
+ });
491
+
492
+ // Log all role creations
493
+ runtime.registerEvent({
494
+ name: DiscordEventTypes.ROLE_CREATED,
495
+ handler: async (payload) => {
496
+ console.log(`New role created: ${payload.role.name}`, {
497
+ permissions: payload.role.permissions,
498
+ createdBy: payload.audit?.executorTag || "Unknown",
499
+ });
500
+ },
501
+ });
502
+ ```
503
+
504
+ #### Bot Self-Protection
505
+
506
+ Monitor when the bot's own permissions change:
507
+
508
+ ```typescript
509
+ runtime.registerEvent({
510
+ name: DiscordEventTypes.MEMBER_ROLES_CHANGED,
511
+ handler: async (payload) => {
512
+ const botId = runtime.getSetting("DISCORD_APPLICATION_ID");
513
+
514
+ if (payload.member.id === botId && payload.removed.length > 0) {
515
+ console.error(`🚨 Bot roles removed!`, {
516
+ removed: payload.removed.map((r) => r.name),
517
+ by: payload.audit?.executorTag || "Unknown",
518
+ });
519
+ // Could trigger alerts, notifications, etc.
520
+ }
521
+ },
522
+ });
523
+ ```
524
+
525
+ ## Cross-Core Compatibility
526
+
527
+ This plugin includes a compatibility layer (`compat.ts`) that allows it to work with both old and new versions of `@elizaos/core`. The compatibility layer:
528
+
529
+ - Automatically handles `serverId` vs `messageServerId` differences
530
+ - Uses a runtime proxy to intercept and adapt API calls
531
+ - Requires no changes to existing code
532
+
533
+ When migrating to a new core version, see the comments in `compat.ts` for removal instructions.
534
+
535
+ ## Testing
536
+
537
+ The plugin includes a test suite for validating functionality:
538
+
539
+ ```bash
540
+ bun run test
541
+ ```
542
+
543
+ ## Notes
544
+
545
+ - Ensure that your `.env` file includes the required `DISCORD_API_TOKEN`
546
+ - The bot requires appropriate Discord permissions (send messages, connect to voice, etc.)
547
+ - If no token is provided, the plugin will load but remain non-functional with appropriate warnings
548
+ - The plugin uses Discord.js v14.18.0 with comprehensive intent support
549
+ - Slash commands and modal/component interactions bypass channel whitelists