koishi-plugin-auto-friend-handler 0.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.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "auto-friend-handler";
3
+ export declare const inject: string[];
4
+ export interface BlacklistEntry {
5
+ user_id: string;
6
+ reason: string;
7
+ operator_id?: string;
8
+ source_id?: string;
9
+ disabled: boolean;
10
+ updated_at: Date;
11
+ }
12
+ declare module 'koishi' {
13
+ interface Tables {
14
+ blacklist_users: BlacklistEntry;
15
+ }
16
+ }
17
+ export interface Config {
18
+ replyMessage: string;
19
+ }
20
+ export declare const Config: Schema<Config>;
21
+ export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Config = exports.inject = exports.name = void 0;
4
+ exports.apply = apply;
5
+ const koishi_1 = require("koishi");
6
+ exports.name = 'auto-friend-handler';
7
+ exports.inject = ['database'];
8
+ exports.Config = koishi_1.Schema.object({
9
+ replyMessage: koishi_1.Schema.string()
10
+ .description('自动通过好友请求后发送的消息内容。')
11
+ .default('你好!我是机器人,很高兴认识你。'),
12
+ });
13
+ function apply(ctx, config) {
14
+ // 4. 监听好友请求事件
15
+ ctx.on('friend-request', async (session) => {
16
+ const { userId, messageId, bot } = session;
17
+ // 默认假设应该通过(如果在黑名单中则改为 false)
18
+ let shouldApprove = true;
19
+ // 第一步:尝试检查黑名单
20
+ try {
21
+ // 检查模型是否已加载 (防止 model not found 报错)
22
+ // 注意:ctx.model.tables 在某些版本可能需要改为 ctx.model.config 等,
23
+ // 但最稳妥的方式是直接捕获 database.get 的错误
24
+ const blacklistedUser = await ctx.database.get('blacklist_users', {
25
+ user_id: userId,
26
+ });
27
+ // 逻辑:如果用户在数据库中存在(无论 disabled 状态如何),则不处理
28
+ if (blacklistedUser.length > 0) {
29
+ // 查到了记录,标记为不通过
30
+ shouldApprove = false;
31
+ ctx.logger.info(`检测到用户 ${userId} 在黑名单中,跳过自动处理。`);
32
+ }
33
+ }
34
+ catch (error) {
35
+ // 捕获错误:表不存在、模型未加载或数据库连接失败
36
+ // 这里的策略是:如果有错误,就当做“不在黑名单中”,继续执行默认通过逻辑
37
+ ctx.logger.warn(`读取黑名单表失败 (可能是表不存在),将默认通过请求。错误信息: ${error.message}`);
38
+ shouldApprove = true;
39
+ }
40
+ // 第二步:根据检查结果决定是否通过
41
+ if (shouldApprove && messageId && userId) {
42
+ try {
43
+ await bot.handleFriendRequest(messageId, true);
44
+ ctx.logger.info(`已自动通过用户 ${userId} 的好友请求。`);
45
+ // 稍微延迟确保好友关系建立
46
+ await new Promise(resolve => setTimeout(resolve, 500));
47
+ await bot.sendPrivateMessage(userId, config.replyMessage);
48
+ }
49
+ catch (error) {
50
+ ctx.logger.error(`处理好友请求动作失败 (User: ${userId}):`, error);
51
+ }
52
+ }
53
+ });
54
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "koishi-plugin-auto-friend-handler",
3
+ "description": "自用插件,联动黑名单插件",
4
+ "version": "0.0.1",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "dist"
10
+ ],
11
+ "license": "MIT",
12
+ "keywords": [
13
+ "chatbot",
14
+ "koishi",
15
+ "plugin"
16
+ ],
17
+ "peerDependencies": {
18
+ "koishi": "4.18.9"
19
+ }
20
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-auto-friend-handler
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-auto-friend-handler?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-auto-friend-handler)
4
+
5
+ 自用插件,联动黑名单插件