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