koishi-plugin-minecraft-adapter 0.4.2 → 0.4.4
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.d.ts +1 -0
- package/lib/index.js +45 -3
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ declare class MinecraftService extends Service implements MinecraftServiceAPI {
|
|
|
32
32
|
private wsReconnectTimer?;
|
|
33
33
|
private wsCurrentInterval?;
|
|
34
34
|
private debugEnabled;
|
|
35
|
+
private internalBot?;
|
|
35
36
|
constructor(ctx: Context, config: MinecraftService.Config);
|
|
36
37
|
private registerWebhook;
|
|
37
38
|
start(): void;
|
package/lib/index.js
CHANGED
|
@@ -26,6 +26,8 @@ class MinecraftService extends koishi_1.Service {
|
|
|
26
26
|
wsCurrentInterval;
|
|
27
27
|
// Debug
|
|
28
28
|
debugEnabled = false;
|
|
29
|
+
// 内置简易 Bot(用于在无正式 Adapter 时注入会话)
|
|
30
|
+
internalBot;
|
|
29
31
|
constructor(ctx, config) {
|
|
30
32
|
super(ctx, 'minecraft', true);
|
|
31
33
|
this.ctx = ctx;
|
|
@@ -34,6 +36,10 @@ class MinecraftService extends koishi_1.Service {
|
|
|
34
36
|
if (config.webhook?.enabled) {
|
|
35
37
|
this.registerWebhook();
|
|
36
38
|
}
|
|
39
|
+
// 创建一个内部 Bot,便于把聊天注入 Koishi 标准消息事件
|
|
40
|
+
if (config.adapterCompat?.injectMessage) {
|
|
41
|
+
this.internalBot = new MinecraftBot(ctx, { selfId: config.websocket?.serverName || 'minecraft' }, this);
|
|
42
|
+
}
|
|
37
43
|
// 注册 Koishi 指令,便于 ChatLuna 通过自然语言“触发命令”
|
|
38
44
|
if (config.commands?.enabled !== false) {
|
|
39
45
|
const authority = config.commands?.authority ?? 2;
|
|
@@ -302,13 +308,14 @@ class MinecraftService extends koishi_1.Service {
|
|
|
302
308
|
channelId: `mc:${userId}`,
|
|
303
309
|
guildId: 'minecraft',
|
|
304
310
|
content,
|
|
311
|
+
bot: this.internalBot,
|
|
305
312
|
};
|
|
306
313
|
this.dlog('inject.message', { session: { ...session, content: truncate(session.content, 200) } });
|
|
307
314
|
this.ctx.emit('message', session);
|
|
308
315
|
}
|
|
309
316
|
}
|
|
310
317
|
catch (e) {
|
|
311
|
-
logger.warn('WS
|
|
318
|
+
logger.warn('WS 处理失败: ' + e?.message);
|
|
312
319
|
}
|
|
313
320
|
});
|
|
314
321
|
ws.on('close', () => {
|
|
@@ -577,13 +584,27 @@ function mapQueqiaoEvent(type, data) {
|
|
|
577
584
|
case 'advancement_earned':
|
|
578
585
|
return { type: 'advancement', payload: { player: data.player || data.name, advancement: data.advancement || data.key, raw: data } };
|
|
579
586
|
}
|
|
580
|
-
// 回退:QueQiao
|
|
581
|
-
if (data
|
|
587
|
+
// 回退:QueQiao 标准格式
|
|
588
|
+
if (data?.post_type === 'message') {
|
|
582
589
|
if (data.sub_type === 'chat' || data.event_name?.toLowerCase()?.includes('chatevent')) {
|
|
583
590
|
const playerName = data.player?.nickname || data.player?.name || data.player?.display_name || data.name;
|
|
584
591
|
return { type: 'chat', payload: { player: playerName, message: data.message || data.text, raw: data } };
|
|
585
592
|
}
|
|
586
593
|
}
|
|
594
|
+
if (data?.post_type === 'notice') {
|
|
595
|
+
if (data.sub_type === 'join' || data.event_name?.toLowerCase()?.includes('loggedinevent')) {
|
|
596
|
+
const playerName = data.player?.nickname || data.player?.name || data.player?.display_name || data.name;
|
|
597
|
+
return { type: 'join', payload: { player: playerName, raw: data } };
|
|
598
|
+
}
|
|
599
|
+
if (data.sub_type === 'quit' || data.event_name?.toLowerCase()?.includes('loggedoutevent')) {
|
|
600
|
+
const playerName = data.player?.nickname || data.player?.name || data.player?.display_name || data.name;
|
|
601
|
+
return { type: 'leave', payload: { player: playerName, raw: data } };
|
|
602
|
+
}
|
|
603
|
+
if (data.sub_type === 'achievement' || data.event_name?.toLowerCase()?.includes('advancement')) {
|
|
604
|
+
const playerName = data.player?.nickname || data.player?.name || data.player?.display_name || data.name;
|
|
605
|
+
return { type: 'advancement', payload: { player: playerName, advancement: data.advancement?.name || data.advancement?.key, raw: data } };
|
|
606
|
+
}
|
|
607
|
+
}
|
|
587
608
|
return { type, payload: data };
|
|
588
609
|
}
|
|
589
610
|
function buildWsOutboundPayload(config, api, data) {
|
|
@@ -604,3 +625,24 @@ function truncate(str, len = 1000) {
|
|
|
604
625
|
return str;
|
|
605
626
|
return str.length > len ? str.slice(0, len) + '…' : str;
|
|
606
627
|
}
|
|
628
|
+
// 极简 Bot:用于让 session.bot 存在,避免 Koishi Processor 报错
|
|
629
|
+
class MinecraftBot extends koishi_1.Bot {
|
|
630
|
+
config;
|
|
631
|
+
svc;
|
|
632
|
+
constructor(ctx, config, svc) {
|
|
633
|
+
super(ctx, {
|
|
634
|
+
platform: 'minecraft',
|
|
635
|
+
selfId: config.selfId,
|
|
636
|
+
}, {});
|
|
637
|
+
this.config = config;
|
|
638
|
+
this.svc = svc;
|
|
639
|
+
}
|
|
640
|
+
async sendMessage(channelId, content) {
|
|
641
|
+
await this.svc.wsBroadcast(content);
|
|
642
|
+
return [];
|
|
643
|
+
}
|
|
644
|
+
async sendPrivateMessage(userId, content) {
|
|
645
|
+
await this.svc.wsTell(userId, content);
|
|
646
|
+
return [];
|
|
647
|
+
}
|
|
648
|
+
}
|