@zhin.js/adapter-discord 1.0.1

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/src/index.ts ADDED
@@ -0,0 +1,1064 @@
1
+ import {
2
+ Client,
3
+ GatewayIntentBits,
4
+ Message as DiscordMessage,
5
+ TextChannel,
6
+ DMChannel,
7
+ NewsChannel,
8
+ ThreadChannel,
9
+ EmbedBuilder,
10
+ AttachmentBuilder,
11
+ MessageCreateOptions,
12
+ ChannelType,
13
+ User,
14
+ GuildMember,
15
+ SlashCommandBuilder,
16
+ CommandInteraction,
17
+ REST,
18
+ Routes,
19
+ ApplicationCommandData,
20
+ ChatInputCommandInteraction,
21
+ InteractionType,
22
+ InteractionResponseType
23
+ } from 'discord.js';
24
+ import {
25
+ Bot,
26
+ BotConfig,
27
+ Adapter,
28
+ Plugin,
29
+ registerAdapter,
30
+ Message,
31
+ SendOptions,
32
+ SendContent,
33
+ MessageSegment,
34
+ segment,
35
+ useContext
36
+ } from "zhin.js";
37
+ import type { Context } from 'koa';
38
+ import { createReadStream } from 'fs';
39
+ import { promises as fs } from 'fs';
40
+ import path from 'path';
41
+
42
+ // 声明模块,注册 discord 适配器类型
43
+ declare module 'zhin.js' {
44
+ interface RegisteredAdapters {
45
+ discord: Adapter<DiscordBot>
46
+ 'discord-interactions': Adapter<DiscordInteractionsBot>
47
+ }
48
+ }
49
+
50
+ // Discord Gateway 模式配置
51
+ export type DiscordBotConfig = BotConfig & {
52
+ context: 'discord'
53
+ token: string
54
+ name: string
55
+ intents?: GatewayIntentBits[]
56
+ // Discord 特有配置
57
+ enableSlashCommands?: boolean
58
+ globalCommands?: boolean
59
+ defaultActivity?: {
60
+ name: string
61
+ type: 'PLAYING' | 'STREAMING' | 'LISTENING' | 'WATCHING' | 'COMPETING'
62
+ url?: string
63
+ }
64
+ // Slash Commands 定义
65
+ slashCommands?: ApplicationCommandData[]
66
+ }
67
+
68
+ // Discord Interactions 模式配置
69
+ export interface DiscordInteractionsConfig extends BotConfig {
70
+ context: 'discord-interactions'
71
+ token: string
72
+ name: string
73
+ applicationId: string
74
+ publicKey: string // Discord 应用的 Public Key
75
+ interactionsPath: string // 交互端点路径,如 '/discord/interactions'
76
+ // 是否同时使用 Gateway(默认 false)
77
+ useGateway?: boolean
78
+ intents?: GatewayIntentBits[]
79
+ // Slash Commands 定义
80
+ slashCommands?: ApplicationCommandData[]
81
+ globalCommands?: boolean
82
+ defaultActivity?: {
83
+ name: string
84
+ type: 'PLAYING' | 'STREAMING' | 'LISTENING' | 'WATCHING' | 'COMPETING'
85
+ url?: string
86
+ }
87
+ }
88
+
89
+ // Bot 接口
90
+ export interface DiscordBot {
91
+ $config: DiscordBotConfig
92
+ }
93
+
94
+ export interface DiscordInteractionsBot {
95
+ $config: DiscordInteractionsConfig
96
+ }
97
+
98
+ // Discord 消息类型
99
+ type DiscordChannelMessage = DiscordMessage<boolean>;
100
+
101
+ // 主要的 DiscordBot 类
102
+ export class DiscordBot extends Client implements Bot<DiscordChannelMessage, DiscordBotConfig> {
103
+ $connected?: boolean
104
+ private slashCommandHandlers: Map<string, (interaction: ChatInputCommandInteraction) => Promise<void>> = new Map()
105
+
106
+ constructor(private plugin: Plugin, public $config: DiscordBotConfig) {
107
+ const intents = $config.intents || [
108
+ GatewayIntentBits.Guilds,
109
+ GatewayIntentBits.GuildMessages,
110
+ GatewayIntentBits.MessageContent,
111
+ GatewayIntentBits.DirectMessages,
112
+ GatewayIntentBits.GuildMembers,
113
+ GatewayIntentBits.GuildMessageReactions
114
+ ];
115
+
116
+ super({ intents });
117
+ this.$connected = false;
118
+ }
119
+
120
+ private async handleDiscordMessage(msg: DiscordChannelMessage): Promise<void> {
121
+ // 忽略机器人消息
122
+ if (msg.author.bot) return;
123
+
124
+ const message = this.$formatMessage(msg);
125
+ this.plugin.dispatch('message.receive', message);
126
+ this.plugin.logger.info(`recv ${message.$channel.type}(${message.$channel.id}): ${segment.raw(message.$content)}`);
127
+ this.plugin.dispatch(`message.${message.$channel.type}.receive`, message);
128
+ }
129
+
130
+ private async handleSlashCommand(interaction: ChatInputCommandInteraction): Promise<void> {
131
+ const commandName = interaction.commandName;
132
+ const handler = this.slashCommandHandlers.get(commandName);
133
+
134
+ if (handler) {
135
+ try {
136
+ await handler(interaction);
137
+ this.plugin.logger.info(`Executed slash command: /${commandName} by ${interaction.user.tag}`);
138
+ } catch (error) {
139
+ this.plugin.logger.error(`Error executing slash command /${commandName}:`, error);
140
+
141
+ const errorMessage = 'An error occurred while executing this command.';
142
+ if (interaction.replied || interaction.deferred) {
143
+ await interaction.followUp({ content: errorMessage, ephemeral: true });
144
+ } else {
145
+ await interaction.reply({ content: errorMessage, ephemeral: true });
146
+ }
147
+ }
148
+ } else {
149
+ this.plugin.logger.warn(`Unknown slash command: /${commandName}`);
150
+ if (!interaction.replied) {
151
+ await interaction.reply({
152
+ content: 'Unknown command.',
153
+ ephemeral: true
154
+ });
155
+ }
156
+ }
157
+ }
158
+
159
+ async $connect(): Promise<void> {
160
+ return new Promise((resolve, reject) => {
161
+ // 监听消息事件
162
+ this.on('messageCreate', this.handleDiscordMessage.bind(this));
163
+
164
+ // 监听交互事件(Slash Commands)
165
+ if (this.$config.enableSlashCommands) {
166
+ this.on('interactionCreate', async (interaction) => {
167
+ if (interaction.isChatInputCommand()) {
168
+ await this.handleSlashCommand(interaction);
169
+ }
170
+ });
171
+ }
172
+
173
+ // 监听就绪事件
174
+ this.once('ready', async () => {
175
+ this.$connected = true;
176
+ this.plugin.logger.info(`Discord bot ${this.$config.name} connected successfully as ${this.user?.tag}`);
177
+
178
+ // 设置活动状态
179
+ if (this.$config.defaultActivity) {
180
+ this.user?.setActivity(this.$config.defaultActivity.name, {
181
+ type: this.getActivityType(this.$config.defaultActivity.type),
182
+ url: this.$config.defaultActivity.url
183
+ });
184
+ }
185
+
186
+ // 注册 Slash Commands
187
+ if (this.$config.enableSlashCommands && this.$config.slashCommands) {
188
+ await this.registerSlashCommands();
189
+ }
190
+
191
+ resolve();
192
+ });
193
+
194
+ // 监听错误事件
195
+ this.on('error', (error) => {
196
+ this.plugin.logger.error('Discord client error:', error);
197
+ this.$connected = false;
198
+ reject(error);
199
+ });
200
+
201
+ // 登录
202
+ this.login(this.$config.token).catch((error) => {
203
+ this.plugin.logger.error('Failed to login to Discord:', error);
204
+ this.$connected = false;
205
+ reject(error);
206
+ });
207
+ });
208
+ }
209
+
210
+ async $disconnect(): Promise<void> {
211
+ try {
212
+ await this.destroy();
213
+ this.$connected = false;
214
+ this.plugin.logger.info(`Discord bot ${this.$config.name} disconnected`);
215
+ } catch (error) {
216
+ this.plugin.logger.error('Error disconnecting Discord bot:', error);
217
+ throw error;
218
+ }
219
+ }
220
+
221
+ $formatMessage(msg: DiscordChannelMessage): Message<DiscordChannelMessage> {
222
+ // 确定聊天类型和ID
223
+ let channelType: 'private' | 'group' | 'channel';
224
+ let channelId: string;
225
+
226
+ if (msg.channel.type === ChannelType.DM) {
227
+ channelType = 'private';
228
+ channelId = msg.channel.id;
229
+ } else if (msg.channel.type === ChannelType.GroupDM) {
230
+ channelType = 'group';
231
+ channelId = msg.channel.id;
232
+ } else {
233
+ channelType = 'channel';
234
+ channelId = msg.channel.id;
235
+ }
236
+
237
+ // 转换消息内容为 segment 格式
238
+ const content = this.parseMessageContent(msg);
239
+
240
+ const result = Message.from(msg, {
241
+ $id: msg.id,
242
+ $adapter: 'discord',
243
+ $bot: this.$config.name,
244
+ $sender: {
245
+ id: msg.author.id,
246
+ name: msg.member?.displayName || msg.author.displayName
247
+ },
248
+ $channel: {
249
+ id: channelId,
250
+ type: channelType
251
+ },
252
+ $content: content,
253
+ $raw: msg.content,
254
+ $timestamp: msg.createdTimestamp,
255
+ $reply: async (content: SendContent, quote?: boolean | string): Promise<void> => {
256
+ if (!Array.isArray(content)) content = [content];
257
+
258
+ const sendOptions: MessageCreateOptions = {};
259
+
260
+ // 处理回复消息
261
+ if (quote) {
262
+ const replyId = typeof quote === "boolean" ? result.$id : quote;
263
+ try {
264
+ const replyMessage = await msg.channel.messages.fetch(replyId);
265
+ sendOptions.reply = { messageReference: replyMessage };
266
+ } catch (error) {
267
+ this.plugin.logger.warn(`Could not find message to reply to: ${replyId}`);
268
+ }
269
+ }
270
+
271
+ await this.sendContentToChannel(msg.channel as any, content, sendOptions);
272
+ }
273
+ });
274
+
275
+ return result;
276
+ }
277
+
278
+ // 解析 Discord 消息内容为 segment 格式
279
+ parseMessageContent(msg: DiscordChannelMessage): MessageSegment[] {
280
+ const segments: MessageSegment[] = [];
281
+
282
+ // 回复消息处理
283
+ if (msg.reference) {
284
+ segments.push({
285
+ type: 'reply',
286
+ data: {
287
+ id: msg.reference.messageId,
288
+ channel_id: msg.reference.channelId,
289
+ guild_id: msg.reference.guildId
290
+ }
291
+ });
292
+ }
293
+
294
+ // 文本消息(包含提及、表情等)
295
+ if (msg.content) {
296
+ segments.push(...this.parseTextContent(msg.content, msg));
297
+ }
298
+
299
+ // 附件消息
300
+ for (const attachment of msg.attachments.values()) {
301
+ segments.push(...this.parseAttachment(attachment));
302
+ }
303
+
304
+ // Embed 消息
305
+ for (const embed of msg.embeds) {
306
+ segments.push({
307
+ type: 'embed',
308
+ data: {
309
+ title: embed.title,
310
+ description: embed.description,
311
+ color: embed.color,
312
+ url: embed.url,
313
+ thumbnail: embed.thumbnail,
314
+ image: embed.image,
315
+ author: embed.author,
316
+ footer: embed.footer,
317
+ fields: embed.fields,
318
+ timestamp: embed.timestamp
319
+ }
320
+ });
321
+ }
322
+
323
+ // 贴纸消息
324
+ for (const sticker of msg.stickers.values()) {
325
+ segments.push({
326
+ type: 'sticker',
327
+ data: {
328
+ id: sticker.id,
329
+ name: sticker.name,
330
+ url: sticker.url,
331
+ format: sticker.format,
332
+ tags: sticker.tags
333
+ }
334
+ });
335
+ }
336
+
337
+ return segments.length > 0 ? segments : [{ type: 'text', data: { text: '' } }];
338
+ }
339
+
340
+ // 解析文本内容,处理提及、频道引用、角色引用等
341
+ parseTextContent(content: string, msg: DiscordChannelMessage): MessageSegment[] {
342
+ const segments: MessageSegment[] = [];
343
+ let lastIndex = 0;
344
+
345
+ // 匹配用户提及 <@!?用户ID>
346
+ const userMentionRegex = /<@!?(\d+)>/g;
347
+ // 匹配频道提及 <#频道ID>
348
+ const channelMentionRegex = /<#(\d+)>/g;
349
+ // 匹配角色提及 <@&角色ID>
350
+ const roleMentionRegex = /<@&(\d+)>/g;
351
+ // 匹配自定义表情 <:名称:ID> 或 <a:名称:ID>
352
+ const emojiRegex = /<a?:(\w+):(\d+)>/g;
353
+
354
+ const allMatches: Array<{
355
+ match: RegExpExecArray;
356
+ type: 'user' | 'channel' | 'role' | 'emoji';
357
+ }> = [];
358
+
359
+ // 收集所有匹配项
360
+ let match;
361
+ while ((match = userMentionRegex.exec(content)) !== null) {
362
+ allMatches.push({ match, type: 'user' });
363
+ }
364
+ while ((match = channelMentionRegex.exec(content)) !== null) {
365
+ allMatches.push({ match, type: 'channel' });
366
+ }
367
+ while ((match = roleMentionRegex.exec(content)) !== null) {
368
+ allMatches.push({ match, type: 'role' });
369
+ }
370
+ while ((match = emojiRegex.exec(content)) !== null) {
371
+ allMatches.push({ match, type: 'emoji' });
372
+ }
373
+
374
+ // 按位置排序
375
+ allMatches.sort((a, b) => a.match.index! - b.match.index!);
376
+
377
+ // 处理每个匹配项
378
+ for (const { match, type } of allMatches) {
379
+ const matchStart = match.index!;
380
+ const matchEnd = matchStart + match[0].length;
381
+
382
+ // 添加匹配项前的文本
383
+ if (matchStart > lastIndex) {
384
+ const beforeText = content.slice(lastIndex, matchStart);
385
+ if (beforeText.trim()) {
386
+ segments.push({ type: 'text', data: { text: beforeText } });
387
+ }
388
+ }
389
+
390
+ // 添加特殊内容段
391
+ switch (type) {
392
+ case 'user':
393
+ const userId = match[1];
394
+ const user = msg.mentions.users.get(userId);
395
+ segments.push({
396
+ type: 'at',
397
+ data: {
398
+ id: userId,
399
+ name: user?.username || 'Unknown',
400
+ text: match[0]
401
+ }
402
+ });
403
+ break;
404
+
405
+ case 'channel':
406
+ const channelId = match[1];
407
+ const channel = msg.mentions.channels.get(channelId);
408
+ segments.push({
409
+ type: 'channel_mention',
410
+ data: {
411
+ id: channelId,
412
+ name: (channel as any)?.name || 'unknown-channel',
413
+ text: match[0]
414
+ }
415
+ });
416
+ break;
417
+
418
+ case 'role':
419
+ const roleId = match[1];
420
+ const role = msg.mentions.roles.get(roleId);
421
+ segments.push({
422
+ type: 'role_mention',
423
+ data: {
424
+ id: roleId,
425
+ name: role?.name || 'unknown-role',
426
+ text: match[0]
427
+ }
428
+ });
429
+ break;
430
+
431
+ case 'emoji':
432
+ const emojiName = match[1];
433
+ const emojiId = match[2];
434
+ const isAnimated = match[0].startsWith('<a:');
435
+ segments.push({
436
+ type: 'emoji',
437
+ data: {
438
+ id: emojiId,
439
+ name: emojiName,
440
+ animated: isAnimated,
441
+ url: `https://cdn.discordapp.com/emojis/${emojiId}.${isAnimated ? 'gif' : 'png'}`,
442
+ text: match[0]
443
+ }
444
+ });
445
+ break;
446
+ }
447
+
448
+ lastIndex = matchEnd;
449
+ }
450
+
451
+ // 添加最后剩余的文本
452
+ if (lastIndex < content.length) {
453
+ const remainingText = content.slice(lastIndex);
454
+ if (remainingText.trim()) {
455
+ segments.push({ type: 'text', data: { text: remainingText } });
456
+ }
457
+ }
458
+
459
+ return segments.length > 0 ? segments : [{ type: 'text', data: { text: content } }];
460
+ }
461
+
462
+ // 解析附件
463
+ parseAttachment(attachment: any): MessageSegment[] {
464
+ const segments: MessageSegment[] = [];
465
+
466
+ if (attachment.contentType?.startsWith('image/')) {
467
+ segments.push({
468
+ type: 'image',
469
+ data: {
470
+ id: attachment.id,
471
+ name: attachment.name,
472
+ url: attachment.url,
473
+ proxy_url: attachment.proxyURL,
474
+ size: attachment.size,
475
+ width: attachment.width,
476
+ height: attachment.height,
477
+ content_type: attachment.contentType
478
+ }
479
+ });
480
+ } else if (attachment.contentType?.startsWith('audio/')) {
481
+ segments.push({
482
+ type: 'audio',
483
+ data: {
484
+ id: attachment.id,
485
+ name: attachment.name,
486
+ url: attachment.url,
487
+ proxy_url: attachment.proxyURL,
488
+ size: attachment.size,
489
+ content_type: attachment.contentType
490
+ }
491
+ });
492
+ } else if (attachment.contentType?.startsWith('video/')) {
493
+ segments.push({
494
+ type: 'video',
495
+ data: {
496
+ id: attachment.id,
497
+ name: attachment.name,
498
+ url: attachment.url,
499
+ proxy_url: attachment.proxyURL,
500
+ size: attachment.size,
501
+ width: attachment.width,
502
+ height: attachment.height,
503
+ content_type: attachment.contentType
504
+ }
505
+ });
506
+ } else {
507
+ segments.push({
508
+ type: 'file',
509
+ data: {
510
+ id: attachment.id,
511
+ name: attachment.name,
512
+ url: attachment.url,
513
+ proxy_url: attachment.proxyURL,
514
+ size: attachment.size,
515
+ content_type: attachment.contentType
516
+ }
517
+ });
518
+ }
519
+
520
+ return segments;
521
+ }
522
+
523
+ async $sendMessage(options: SendOptions): Promise<void> {
524
+ options = await this.plugin.app.handleBeforeSend(options);
525
+
526
+ try {
527
+ const channel = await this.channels.fetch(options.id);
528
+ if (!channel || !channel.isTextBased()) {
529
+ throw new Error(`Channel ${options.id} is not a text channel`);
530
+ }
531
+
532
+ await this.sendContentToChannel(channel as any, options.content);
533
+ this.plugin.logger.info(`send ${options.type}(${options.id}): ${segment.raw(options.content)}`);
534
+ } catch (error) {
535
+ this.plugin.logger.error('Failed to send Discord message:', error);
536
+ throw error;
537
+ }
538
+ }
539
+
540
+ // 发送内容到频道
541
+ async sendContentToChannel(
542
+ channel: TextChannel | DMChannel | NewsChannel | ThreadChannel,
543
+ content: SendContent,
544
+ extraOptions: MessageCreateOptions = {}
545
+ ): Promise<void> {
546
+ if (!Array.isArray(content)) content = [content];
547
+
548
+ const messageOptions: MessageCreateOptions = { ...extraOptions };
549
+ let textContent = '';
550
+ const embeds: EmbedBuilder[] = [];
551
+ const files: AttachmentBuilder[] = [];
552
+
553
+ for (const segment of content) {
554
+ if (typeof segment === 'string') {
555
+ textContent += segment;
556
+ continue;
557
+ }
558
+
559
+ const { type, data } = segment;
560
+
561
+ switch (type) {
562
+ case 'text':
563
+ textContent += data.text || '';
564
+ break;
565
+
566
+ case 'at':
567
+ textContent += `<@${data.id}>`;
568
+ break;
569
+
570
+ case 'channel_mention':
571
+ textContent += `<#${data.id}>`;
572
+ break;
573
+
574
+ case 'role_mention':
575
+ textContent += `<@&${data.id}>`;
576
+ break;
577
+
578
+ case 'emoji':
579
+ textContent += data.animated ? `<a:${data.name}:${data.id}>` : `<:${data.name}:${data.id}>`;
580
+ break;
581
+
582
+ case 'image':
583
+ case 'audio':
584
+ case 'video':
585
+ case 'file':
586
+ await this.handleFileSegment(data, files, textContent);
587
+ break;
588
+
589
+ case 'embed':
590
+ embeds.push(this.createEmbedFromData(data));
591
+ break;
592
+
593
+ default:
594
+ // 未知类型作为文本处理
595
+ textContent += data.text || `[${type}]`;
596
+ }
597
+ }
598
+
599
+ // 设置消息内容
600
+ if (textContent.trim()) {
601
+ messageOptions.content = textContent.trim();
602
+ }
603
+
604
+ if (embeds.length > 0) {
605
+ messageOptions.embeds = embeds.slice(0, 10); // Discord 限制最多10个embed
606
+ }
607
+
608
+ if (files.length > 0) {
609
+ messageOptions.files = files;
610
+ }
611
+
612
+ // 发送消息
613
+ await channel.send(messageOptions);
614
+ }
615
+
616
+ // 处理文件段
617
+ async handleFileSegment(data: any, files: AttachmentBuilder[], textContent: string): Promise<void> {
618
+ if (data.file && await this.fileExists(data.file)) {
619
+ // 本地文件
620
+ files.push(new AttachmentBuilder(createReadStream(data.file), {
621
+ name: data.name || path.basename(data.file)
622
+ }));
623
+ } else if (data.url) {
624
+ // URL 文件
625
+ files.push(new AttachmentBuilder(data.url, {
626
+ name: data.name || 'attachment'
627
+ }));
628
+ } else if (data.buffer) {
629
+ // Buffer 数据
630
+ files.push(new AttachmentBuilder(data.buffer, {
631
+ name: data.name || 'attachment'
632
+ }));
633
+ }
634
+ }
635
+
636
+ // 从数据创建 Embed
637
+ createEmbedFromData(data: any): EmbedBuilder {
638
+ const embed = new EmbedBuilder();
639
+
640
+ if (data.title) embed.setTitle(data.title);
641
+ if (data.description) embed.setDescription(data.description);
642
+ if (data.color) embed.setColor(data.color);
643
+ if (data.url) embed.setURL(data.url);
644
+ if (data.thumbnail?.url) embed.setThumbnail(data.thumbnail.url);
645
+ if (data.image?.url) embed.setImage(data.image.url);
646
+ if (data.author) embed.setAuthor(data.author);
647
+ if (data.footer) embed.setFooter(data.footer);
648
+ if (data.timestamp) embed.setTimestamp(new Date(data.timestamp));
649
+ if (data.fields && Array.isArray(data.fields)) {
650
+ embed.addFields(data.fields);
651
+ }
652
+
653
+ return embed;
654
+ }
655
+
656
+ // 工具方法:获取活动类型
657
+ private getActivityType(type: string) {
658
+ const activityTypes = {
659
+ 'PLAYING': 0,
660
+ 'STREAMING': 1,
661
+ 'LISTENING': 2,
662
+ 'WATCHING': 3,
663
+ 'COMPETING': 5
664
+ };
665
+ return activityTypes[type as keyof typeof activityTypes] || 0;
666
+ }
667
+
668
+ // 注册 Slash Commands
669
+ private async registerSlashCommands(): Promise<void> {
670
+ if (!this.$config.slashCommands || !this.user) return;
671
+
672
+ try {
673
+ const rest = new REST({ version: '10' }).setToken(this.$config.token);
674
+
675
+ if (this.$config.globalCommands) {
676
+ // 注册全局命令
677
+ await rest.put(
678
+ Routes.applicationCommands(this.user.id),
679
+ { body: this.$config.slashCommands }
680
+ );
681
+ this.plugin.logger.info('Successfully registered global slash commands');
682
+ } else {
683
+ // 为每个服务器注册命令
684
+ for (const guild of this.guilds.cache.values()) {
685
+ await rest.put(
686
+ Routes.applicationGuildCommands(this.user.id, guild.id),
687
+ { body: this.$config.slashCommands }
688
+ );
689
+ }
690
+ this.plugin.logger.info('Successfully registered guild slash commands');
691
+ }
692
+ } catch (error) {
693
+ this.plugin.logger.error('Failed to register slash commands:', error);
694
+ }
695
+ }
696
+
697
+ // 添加 Slash Command 处理器
698
+ addSlashCommandHandler(commandName: string, handler: (interaction: ChatInputCommandInteraction) => Promise<void>) {
699
+ this.slashCommandHandlers.set(commandName, handler);
700
+ }
701
+
702
+ // 移除 Slash Command 处理器
703
+ removeSlashCommandHandler(commandName: string): boolean {
704
+ return this.slashCommandHandlers.delete(commandName);
705
+ }
706
+
707
+ // 工具方法:检查文件是否存在
708
+ private async fileExists(filePath: string): Promise<boolean> {
709
+ try {
710
+ await fs.access(filePath);
711
+ return true;
712
+ } catch {
713
+ return false;
714
+ }
715
+ }
716
+
717
+ // 静态方法:格式化内容为文本(用于日志显示)
718
+ static formatContentToText(content: SendContent): string {
719
+ if (!Array.isArray(content)) content = [content];
720
+
721
+ return content.map(segment => {
722
+ if (typeof segment === 'string') return segment;
723
+
724
+ switch (segment.type) {
725
+ case 'text':
726
+ return segment.data.text || '';
727
+ case 'at':
728
+ return `@${segment.data.name || segment.data.id}`;
729
+ case 'channel_mention':
730
+ return `#${segment.data.name}`;
731
+ case 'role_mention':
732
+ return `@${segment.data.name}`;
733
+ case 'image':
734
+ return '[图片]';
735
+ case 'audio':
736
+ return '[音频]';
737
+ case 'video':
738
+ return '[视频]';
739
+ case 'file':
740
+ return '[文件]';
741
+ case 'embed':
742
+ return '[嵌入消息]';
743
+ case 'emoji':
744
+ return `:${segment.data.name}:`;
745
+ default:
746
+ return `[${segment.type}]`;
747
+ }
748
+ }).join('');
749
+ }
750
+ }
751
+
752
+ // ================================================================================================
753
+ // DiscordInteractionsBot 类(Interactions 端点模式)
754
+ // ================================================================================================
755
+
756
+ import * as nacl from 'tweetnacl';
757
+
758
+ export class DiscordInteractionsBot extends Client implements Bot<any, DiscordInteractionsConfig> {
759
+ $connected?: boolean
760
+ private router: any
761
+ private slashCommandHandlers: Map<string, (interaction: any) => Promise<void>> = new Map()
762
+
763
+ constructor(private plugin: Plugin, router: any, public $config: DiscordInteractionsConfig) {
764
+ const intents = $config.intents || [
765
+ GatewayIntentBits.Guilds,
766
+ GatewayIntentBits.GuildMessages,
767
+ GatewayIntentBits.MessageContent,
768
+ ];
769
+
770
+ super({ intents });
771
+ this.$connected = false;
772
+ this.router = router;
773
+
774
+ // 设置交互端点路由
775
+ this.setupInteractionsEndpoint();
776
+ }
777
+
778
+ private setupInteractionsEndpoint(): void {
779
+ // 设置路由处理 Discord Interactions
780
+ this.router.post(this.$config.interactionsPath, (ctx: Context) => {
781
+ this.handleInteraction(ctx);
782
+ });
783
+ }
784
+
785
+ private async handleInteraction(ctx: Context): Promise<void> {
786
+ try {
787
+ const signature = ctx.get('x-signature-ed25519');
788
+ const timestamp = ctx.get('x-signature-timestamp');
789
+ const bodyString = JSON.stringify((ctx.request as any).body);
790
+
791
+ // 验证请求签名
792
+ if (!this.verifyDiscordSignature(bodyString, signature, timestamp)) {
793
+ this.plugin.logger.warn('Invalid Discord signature');
794
+ ctx.status = 401;
795
+ ctx.body = 'Unauthorized';
796
+ return;
797
+ }
798
+
799
+ const interaction = (ctx.request as any).body;
800
+
801
+ // 处理不同类型的交互
802
+ if (interaction.type === InteractionType.Ping) {
803
+ // PING - Discord 验证端点
804
+ ctx.body = { type: InteractionResponseType.Pong };
805
+ } else if (interaction.type === InteractionType.ApplicationCommand) {
806
+ // APPLICATION_COMMAND - 应用命令
807
+ const response = await this.handleApplicationCommand(interaction);
808
+ ctx.body = response;
809
+ } else {
810
+ // 其他交互类型
811
+ ctx.status = 400;
812
+ ctx.body = 'Unsupported interaction type';
813
+ }
814
+
815
+ } catch (error) {
816
+ this.plugin.logger.error('Interactions error:', error);
817
+ ctx.status = 500;
818
+ ctx.body = 'Internal Server Error';
819
+ }
820
+ }
821
+
822
+ private verifyDiscordSignature(body: string, signature: string, timestamp: string): boolean {
823
+ try {
824
+ const publicKey = Buffer.from(this.$config.publicKey, 'hex');
825
+ const sig = Buffer.from(signature, 'hex');
826
+ const message = Buffer.from(timestamp + body, 'utf8');
827
+
828
+ return nacl.sign.detached.verify(message, sig, publicKey);
829
+ } catch (error) {
830
+ this.plugin.logger.error('Signature verification error:', error);
831
+ return false;
832
+ }
833
+ }
834
+
835
+ private async handleApplicationCommand(interaction: any): Promise<any> {
836
+ // 处理应用命令
837
+ const commandName = interaction.data.name;
838
+
839
+ // 转换为标准消息格式并分发
840
+ const message = this.formatInteractionAsMessage(interaction);
841
+ this.plugin.dispatch('message.receive', message);
842
+
843
+ // 查找自定义处理器
844
+ const handler = this.slashCommandHandlers.get(commandName);
845
+ if (handler) {
846
+ try {
847
+ await handler(interaction);
848
+ } catch (error) {
849
+ this.plugin.logger.error(`Error in slash command handler for ${commandName}:`, error);
850
+ }
851
+ }
852
+
853
+ // 默认响应
854
+ return {
855
+ type: InteractionResponseType.ChannelMessageWithSource,
856
+ data: {
857
+ content: `处理命令: ${commandName}`,
858
+ flags: 64 // EPHEMERAL - 只有用户可见
859
+ }
860
+ };
861
+ }
862
+
863
+ private formatInteractionAsMessage(interaction: any): Message<any> {
864
+ const channelType = interaction.guild_id ? 'channel' : 'private';
865
+ const channelId = interaction.channel_id;
866
+
867
+ // 解析命令参数为内容
868
+ const options = interaction.data.options || [];
869
+ const content = [segment.text(`/${interaction.data.name}`)];
870
+
871
+ for (const option of options) {
872
+ content.push(segment.text(` ${option.name}:${option.value}`));
873
+ }
874
+
875
+ return Message.from(interaction, {
876
+ $id: interaction.id,
877
+ $adapter: 'discord-interactions',
878
+ $bot: this.$config.name,
879
+ $sender: {
880
+ id: interaction.user?.id || interaction.member?.user?.id,
881
+ name: interaction.user?.username || interaction.member?.user?.username
882
+ },
883
+ $channel: {
884
+ id: channelId,
885
+ type: channelType as any
886
+ },
887
+ $raw: JSON.stringify(interaction),
888
+ $timestamp: Date.now(),
889
+ $content: content,
890
+ $reply: async (content: SendContent): Promise<void> => {
891
+ // 通过 REST API 发送后续消息
892
+ await this.sendFollowUp(interaction, content);
893
+ }
894
+ });
895
+ }
896
+
897
+ private async sendFollowUp(interaction: any, content: SendContent): Promise<void> {
898
+ try {
899
+ const rest = new REST({ version: '10' }).setToken(this.$config.token);
900
+ const messageContent = this.formatSendContent(content);
901
+
902
+ await rest.post(
903
+ `/webhooks/${this.$config.applicationId}/${interaction.token}`,
904
+ { body: messageContent }
905
+ );
906
+ } catch (error) {
907
+ this.plugin.logger.error('Failed to send follow-up message:', error);
908
+ }
909
+ }
910
+
911
+ private formatSendContent(content: SendContent): any {
912
+ if (typeof content === 'string') {
913
+ return { content };
914
+ }
915
+
916
+ if (Array.isArray(content)) {
917
+ const textParts: string[] = [];
918
+ let embed: any = null;
919
+
920
+ for (const item of content) {
921
+ if (typeof item === 'string') {
922
+ textParts.push(item);
923
+ } else {
924
+ const segment = item as MessageSegment;
925
+ switch (segment.type) {
926
+ case 'text':
927
+ textParts.push(segment.data.text || segment.data.content || '');
928
+ break;
929
+ case 'embed':
930
+ embed = segment.data;
931
+ break;
932
+ }
933
+ }
934
+ }
935
+
936
+ const result: any = {};
937
+ if (textParts.length > 0) {
938
+ result.content = textParts.join('');
939
+ }
940
+ if (embed) {
941
+ result.embeds = [embed];
942
+ }
943
+
944
+ return result;
945
+ }
946
+
947
+ return { content: String(content) };
948
+ }
949
+
950
+ async $connect(): Promise<void> {
951
+ try {
952
+ // 注册 Slash Commands
953
+ if (this.$config.slashCommands) {
954
+ await this.registerSlashCommands();
955
+ }
956
+
957
+ // 如果启用 Gateway,连接 Discord Gateway
958
+ if (this.$config.useGateway) {
959
+ await this.login(this.$config.token);
960
+
961
+ // 设置活动状态
962
+ if (this.$config.defaultActivity) {
963
+ this.user?.setActivity(this.$config.defaultActivity.name, {
964
+ type: this.getActivityType(this.$config.defaultActivity.type),
965
+ url: this.$config.defaultActivity.url
966
+ });
967
+ }
968
+ }
969
+
970
+ this.$connected = true;
971
+ this.plugin.logger.info(`Discord interactions bot connected: ${this.$config.name}`);
972
+ this.plugin.logger.info(`Interactions endpoint: ${this.$config.interactionsPath}`);
973
+
974
+ } catch (error) {
975
+ this.plugin.logger.error('Failed to connect Discord interactions bot:', error);
976
+ throw error;
977
+ }
978
+ }
979
+
980
+ async $disconnect(): Promise<void> {
981
+ try {
982
+ if (this.isReady()) {
983
+ await this.destroy();
984
+ }
985
+ this.$connected = false;
986
+ this.plugin.logger.info('Discord interactions bot disconnected');
987
+ } catch (error) {
988
+ this.plugin.logger.error('Error disconnecting Discord interactions bot:', error);
989
+ }
990
+ }
991
+
992
+ // Slash Commands 管理
993
+ private async registerSlashCommands(): Promise<void> {
994
+ if (!this.$config.slashCommands) return;
995
+
996
+ try {
997
+ const rest = new REST({ version: '10' }).setToken(this.$config.token);
998
+
999
+ if (this.$config.globalCommands) {
1000
+ await rest.put(
1001
+ Routes.applicationCommands(this.$config.applicationId),
1002
+ { body: this.$config.slashCommands }
1003
+ );
1004
+ this.plugin.logger.info('Successfully registered global slash commands');
1005
+ } else {
1006
+ this.plugin.logger.info('Note: Guild commands registration requires connecting to Gateway first');
1007
+ }
1008
+ } catch (error) {
1009
+ this.plugin.logger.error('Failed to register slash commands:', error);
1010
+ }
1011
+ }
1012
+
1013
+ // 添加 Slash Command 处理器
1014
+ addSlashCommandHandler(commandName: string, handler: (interaction: any) => Promise<void>): void {
1015
+ this.slashCommandHandlers.set(commandName, handler);
1016
+ }
1017
+
1018
+ // 移除 Slash Command 处理器
1019
+ removeSlashCommandHandler(commandName: string): boolean {
1020
+ return this.slashCommandHandlers.delete(commandName);
1021
+ }
1022
+
1023
+ // 工具方法
1024
+ private getActivityType(type: string): any {
1025
+ const activityTypes: any = {
1026
+ 'PLAYING': 0,
1027
+ 'STREAMING': 1,
1028
+ 'LISTENING': 2,
1029
+ 'WATCHING': 3,
1030
+ 'COMPETING': 5
1031
+ };
1032
+ return activityTypes[type] || 0;
1033
+ }
1034
+
1035
+ // 简化实现 - 只支持基本消息格式化和发送
1036
+ $formatMessage(msg: any): Message<any> {
1037
+ return this.formatInteractionAsMessage(msg);
1038
+ }
1039
+
1040
+ async $sendMessage(options: SendOptions): Promise<void> {
1041
+ // 简化实现 - 通过 REST API 发送消息
1042
+ try {
1043
+ const rest = new REST({ version: '10' }).setToken(this.$config.token);
1044
+ const messageContent = this.formatSendContent(options.content);
1045
+
1046
+ await rest.post(
1047
+ Routes.channelMessages(options.id),
1048
+ { body: messageContent }
1049
+ );
1050
+ } catch (error) {
1051
+ this.plugin.logger.error('Failed to send message:', error);
1052
+ }
1053
+ }
1054
+ }
1055
+
1056
+ // 注册 Gateway 模式适配器
1057
+ registerAdapter(new Adapter('discord', (plugin: Plugin, config: any) => new DiscordBot(plugin, config as DiscordBotConfig)))
1058
+
1059
+ // 注册 Interactions 端点模式适配器(需要 router)
1060
+ useContext('router', (router) => {
1061
+ registerAdapter(new Adapter('discord-interactions',
1062
+ (plugin: Plugin, config: any) => new DiscordInteractionsBot(plugin, router, config as DiscordInteractionsConfig)
1063
+ ));
1064
+ });