koishi-plugin-without-assignee 0.1.0 → 0.1.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.d.ts +2 -3
- package/lib/index.js +35 -31
- package/lib/logger.d.ts +4 -0
- package/lib/reassign.d.ts +3 -0
- package/package.json +1 -1
- package/src/index.ts +12 -49
- package/src/logger.ts +14 -0
- package/src/reassign.ts +22 -0
package/lib/index.d.ts
CHANGED
|
@@ -4,11 +4,10 @@ export declare const reusable = false;
|
|
|
4
4
|
export declare const filter = false;
|
|
5
5
|
export declare const inject: {
|
|
6
6
|
optional: string[];
|
|
7
|
-
required: any[];
|
|
8
7
|
};
|
|
9
|
-
export declare const usage = "\n---\n\n\
|
|
8
|
+
export declare const usage = "\n---\n\n\u7981\u7528 Koishi \u7684 assignee \u673A\u5236\uFF0C\u5141\u8BB8\u540C\u4E00\u9891\u9053\u5185\u7684\u591A\u4E2A\u673A\u5668\u4EBA\u540C\u65F6\u54CD\u5E94\u65E0\u524D\u7F00\u6307\u4EE4\u3002\n\n\u9ED8\u8BA4\u60C5\u51B5\u4E0B\uFF0C\u5F53\u540C\u4E00\u5E73\u53F0\u7684\u540C\u4E00\u9891\u9053\u5185\u6709\u591A\u4E2A\u673A\u5668\u4EBA\u65F6\uFF0CKoishi \u4F1A\u542F\u7528 assignee \u673A\u5236\uFF0C\n\u53EA\u5141\u8BB8\u88AB\u5206\u914D\u7684\u673A\u5668\u4EBA\u54CD\u5E94\u65E0\u524D\u7F00\u6307\u4EE4\uFF0C\u5176\u4ED6\u673A\u5668\u4EBA\u9700\u8981\u901A\u8FC7 @\u673A\u5668\u4EBA \u7684\u65B9\u5F0F\u8C03\u7528\u3002\n\n\u542F\u7528\u6B64\u63D2\u4EF6\u540E\uFF0C\u6240\u6709\u673A\u5668\u4EBA\u90FD\u53EF\u4EE5\u54CD\u5E94\u65E0\u524D\u7F00\u6307\u4EE4\u3002\n\n---\n";
|
|
10
9
|
export interface Config {
|
|
11
|
-
loggerinfo
|
|
10
|
+
loggerinfo: boolean;
|
|
12
11
|
}
|
|
13
12
|
export declare const Config: Schema<Config>;
|
|
14
13
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -30,17 +30,44 @@ __export(src_exports, {
|
|
|
30
30
|
});
|
|
31
31
|
module.exports = __toCommonJS(src_exports);
|
|
32
32
|
var import_koishi = require("koishi");
|
|
33
|
+
|
|
34
|
+
// src/logger.ts
|
|
35
|
+
function createDebugLogger(ctx, config) {
|
|
36
|
+
const logger = ctx.logger("without-assignee");
|
|
37
|
+
return (message, ...args) => {
|
|
38
|
+
if (!config.loggerinfo) return;
|
|
39
|
+
logger.info(message, ...args);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
__name(createDebugLogger, "createDebugLogger");
|
|
43
|
+
|
|
44
|
+
// src/reassign.ts
|
|
45
|
+
function registerAssigneeBypass(ctx, logDebug) {
|
|
46
|
+
ctx.on("attach-channel", (session) => {
|
|
47
|
+
const { channel } = session;
|
|
48
|
+
if (!channel || channel.assignee === session.selfId) return;
|
|
49
|
+
const originalAssignee = channel.assignee;
|
|
50
|
+
channel.$merge({ assignee: session.selfId });
|
|
51
|
+
logDebug(
|
|
52
|
+
"频道 %s 仅在当前会话内忽略 assignee:%s -> %s(不会写入数据库)",
|
|
53
|
+
session.cid,
|
|
54
|
+
originalAssignee || "(空)",
|
|
55
|
+
session.selfId
|
|
56
|
+
);
|
|
57
|
+
}, true);
|
|
58
|
+
}
|
|
59
|
+
__name(registerAssigneeBypass, "registerAssigneeBypass");
|
|
60
|
+
|
|
61
|
+
// src/index.ts
|
|
33
62
|
var name = "without-assignee";
|
|
34
63
|
var reusable = false;
|
|
35
64
|
var filter = false;
|
|
36
65
|
var inject = {
|
|
37
|
-
optional: ["database"
|
|
38
|
-
required: []
|
|
66
|
+
optional: ["database"]
|
|
39
67
|
};
|
|
40
68
|
var usage = `
|
|
41
69
|
---
|
|
42
70
|
|
|
43
|
-
|
|
44
71
|
禁用 Koishi 的 assignee 机制,允许同一频道内的多个机器人同时响应无前缀指令。
|
|
45
72
|
|
|
46
73
|
默认情况下,当同一平台的同一频道内有多个机器人时,Koishi 会启用 assignee 机制,
|
|
@@ -50,35 +77,12 @@ var usage = `
|
|
|
50
77
|
|
|
51
78
|
---
|
|
52
79
|
`;
|
|
53
|
-
var Config = import_koishi.Schema.
|
|
54
|
-
import_koishi.Schema.
|
|
55
|
-
|
|
56
|
-
}).description("调试设置")
|
|
57
|
-
]);
|
|
80
|
+
var Config = import_koishi.Schema.object({
|
|
81
|
+
loggerinfo: import_koishi.Schema.boolean().default(false).description("输出调试日志").experimental()
|
|
82
|
+
}).description("调试设置");
|
|
58
83
|
function apply(ctx, config) {
|
|
59
|
-
ctx
|
|
60
|
-
|
|
61
|
-
if (config.loggerinfo) {
|
|
62
|
-
ctx.logger.info(...args);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
__name(logInfo, "logInfo");
|
|
66
|
-
ctx.on("attach-channel", (session) => {
|
|
67
|
-
if (!ctx.database) {
|
|
68
|
-
ctx.logger.warn("数据库服务未启用,插件无法工作");
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
if (session.isDirect) return;
|
|
72
|
-
const channel = session.channel;
|
|
73
|
-
if (!channel) return;
|
|
74
|
-
const originalAssignee = channel.assignee;
|
|
75
|
-
if (originalAssignee && originalAssignee !== session.selfId) {
|
|
76
|
-
logInfo(`检测到频道 ${session.channelId} 的 assignee 为 ${originalAssignee},当前 bot 为 ${session.selfId}`);
|
|
77
|
-
channel.assignee = session.selfId;
|
|
78
|
-
logInfo(`已将频道 ${session.channelId} 的 assignee 临时修改为 ${session.selfId}`);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
});
|
|
84
|
+
const logDebug = createDebugLogger(ctx, config);
|
|
85
|
+
registerAssigneeBypass(ctx, logDebug);
|
|
82
86
|
}
|
|
83
87
|
__name(apply, "apply");
|
|
84
88
|
// Annotate the CommonJS export names for ESM import in node:
|
package/lib/logger.d.ts
ADDED
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { Context, Schema
|
|
1
|
+
import { Context, Schema } from 'koishi'
|
|
2
|
+
|
|
3
|
+
import { createDebugLogger } from './logger'
|
|
4
|
+
import { registerAssigneeBypass } from './reassign'
|
|
2
5
|
|
|
3
6
|
export const name = 'without-assignee'
|
|
4
7
|
export const reusable = false
|
|
5
8
|
export const filter = false
|
|
6
9
|
|
|
7
10
|
export const inject = {
|
|
8
|
-
optional: ['database'
|
|
9
|
-
required: []
|
|
11
|
+
optional: ['database'],
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export const usage = `
|
|
13
15
|
---
|
|
14
16
|
|
|
15
|
-
|
|
16
17
|
禁用 Koishi 的 assignee 机制,允许同一频道内的多个机器人同时响应无前缀指令。
|
|
17
18
|
|
|
18
19
|
默认情况下,当同一平台的同一频道内有多个机器人时,Koishi 会启用 assignee 机制,
|
|
@@ -21,55 +22,17 @@ export const usage = `
|
|
|
21
22
|
启用此插件后,所有机器人都可以响应无前缀指令。
|
|
22
23
|
|
|
23
24
|
---
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
`
|
|
26
26
|
|
|
27
27
|
export interface Config {
|
|
28
|
-
loggerinfo
|
|
28
|
+
loggerinfo: boolean
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export const Config: Schema<Config> = Schema.
|
|
32
|
-
Schema.
|
|
33
|
-
|
|
34
|
-
}).description("调试设置"),
|
|
35
|
-
])
|
|
31
|
+
export const Config: Schema<Config> = Schema.object({
|
|
32
|
+
loggerinfo: Schema.boolean().default(false).description('输出调试日志').experimental(),
|
|
33
|
+
}).description('调试设置')
|
|
36
34
|
|
|
37
35
|
export function apply(ctx: Context, config: Config) {
|
|
38
|
-
ctx
|
|
39
|
-
|
|
40
|
-
function logInfo(...args: any[]) {
|
|
41
|
-
if (config.loggerinfo) {
|
|
42
|
-
(ctx.logger.info as (...args: any[]) => void)(...args);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// 在 observeChannel 之后、assignee 检查之前触发
|
|
47
|
-
ctx.on('attach-channel', (session: Session) => {
|
|
48
|
-
|
|
49
|
-
// 如果没有数据库服务,无需处理
|
|
50
|
-
if (!ctx.database) {
|
|
51
|
-
ctx.logger.warn('数据库服务未启用,插件无法工作');
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 只处理群组消息
|
|
56
|
-
if (session.isDirect) return;
|
|
57
|
-
|
|
58
|
-
const channel = session.channel;
|
|
59
|
-
if (!channel) return;
|
|
60
|
-
|
|
61
|
-
// 获取原始的 assignee 值
|
|
62
|
-
const originalAssignee = (channel as any).assignee;
|
|
63
|
-
|
|
64
|
-
// 如果 assignee 存在且不是当前 bot
|
|
65
|
-
if (originalAssignee && originalAssignee !== session.selfId) {
|
|
66
|
-
logInfo(`检测到频道 ${session.channelId} 的 assignee 为 ${originalAssignee},当前 bot 为 ${session.selfId}`);
|
|
67
|
-
|
|
68
|
-
// 临时修改 assignee 为当前 bot,绕过后续的 assignee 检查
|
|
69
|
-
(channel as any).assignee = session.selfId;
|
|
70
|
-
|
|
71
|
-
logInfo(`已将频道 ${session.channelId} 的 assignee 临时修改为 ${session.selfId}`);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
})
|
|
36
|
+
const logDebug = createDebugLogger(ctx, config)
|
|
37
|
+
registerAssigneeBypass(ctx, logDebug)
|
|
75
38
|
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Context } from 'koishi'
|
|
2
|
+
|
|
3
|
+
import type { Config } from './index'
|
|
4
|
+
|
|
5
|
+
export type DebugLogger = (message: unknown, ...args: unknown[]) => void
|
|
6
|
+
|
|
7
|
+
export function createDebugLogger(ctx: Context, config: Config): DebugLogger {
|
|
8
|
+
const logger = ctx.logger('without-assignee')
|
|
9
|
+
|
|
10
|
+
return (message, ...args) => {
|
|
11
|
+
if (!config.loggerinfo) return
|
|
12
|
+
logger.info(message, ...args)
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/reassign.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Context, Session } from 'koishi'
|
|
2
|
+
|
|
3
|
+
import type { DebugLogger } from './logger'
|
|
4
|
+
|
|
5
|
+
export function registerAssigneeBypass(ctx: Context, logDebug: DebugLogger) {
|
|
6
|
+
ctx.on('attach-channel', (session: Session<never, 'assignee'>) => {
|
|
7
|
+
const { channel } = session
|
|
8
|
+
if (!channel || channel.assignee === session.selfId) return
|
|
9
|
+
|
|
10
|
+
const originalAssignee = channel.assignee
|
|
11
|
+
|
|
12
|
+
// 只改当前会话里的内存数据,不写入数据库。
|
|
13
|
+
channel.$merge({ assignee: session.selfId })
|
|
14
|
+
|
|
15
|
+
logDebug(
|
|
16
|
+
'频道 %s 仅在当前会话内忽略 assignee:%s -> %s(不会写入数据库)',
|
|
17
|
+
session.cid,
|
|
18
|
+
originalAssignee || '(空)',
|
|
19
|
+
session.selfId,
|
|
20
|
+
)
|
|
21
|
+
}, true)
|
|
22
|
+
}
|