koishi-plugin-auto-friend-handler 0.0.2 → 0.0.3

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 CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Context, Schema } from 'koishi';
2
+ import 'koishi-plugin-adapter-onebot';
2
3
  export declare const name = "auto-friend-handler";
3
4
  export declare const inject: string[];
4
5
  export interface BlacklistEntry {
@@ -16,6 +17,7 @@ declare module 'koishi' {
16
17
  }
17
18
  export interface Config {
18
19
  replyMessage: string;
20
+ ignoreDisabled: boolean;
19
21
  }
20
22
  export declare const Config: Schema<Config>;
21
23
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -3,18 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Config = exports.inject = exports.name = void 0;
4
4
  exports.apply = apply;
5
5
  const koishi_1 = require("koishi");
6
+ require("koishi-plugin-adapter-onebot");
6
7
  exports.name = 'auto-friend-handler';
7
8
  exports.inject = ['database'];
8
9
  exports.Config = koishi_1.Schema.object({
9
10
  replyMessage: koishi_1.Schema.string().role('textarea')
10
11
  .description('自动通过好友请求后发送的消息内容。')
11
12
  .default('你好!我是机器人,很高兴认识你。'),
13
+ ignoreDisabled: koishi_1.Schema.boolean().description('允许通过已禁用的黑名单').default(false),
12
14
  });
13
15
  function apply(ctx, config) {
14
16
  // 4. 监听好友请求事件
15
17
  ctx.on('friend-request', async (session) => {
16
18
  const { userId, messageId, bot } = session;
17
- // 默认假设应该通过(如果在黑名单中则改为 false)
19
+ // 默认假设应该通过
18
20
  let shouldApprove = true;
19
21
  // 第一步:尝试检查黑名单
20
22
  try {
@@ -24,31 +26,38 @@ function apply(ctx, config) {
24
26
  const blacklistedUser = await ctx.database.get('blacklist_users', {
25
27
  user_id: userId,
26
28
  });
27
- // 逻辑:如果用户在数据库中存在(无论 disabled 状态如何),则不处理
28
29
  if (blacklistedUser.length > 0) {
29
- // 查到了记录,标记为不通过
30
- shouldApprove = false;
31
- ctx.logger.info(`检测到用户 ${userId} 在黑名单中,跳过自动处理。`);
30
+ // 如果在黑名单中,检查是否已被禁用(disabled)且配置允许忽略
31
+ shouldApprove = blacklistedUser[0].disabled && config.ignoreDisabled;
32
32
  }
33
33
  }
34
34
  catch (error) {
35
- // 捕获错误:表不存在、模型未加载或数据库连接失败
36
- // 这里的策略是:如果有错误,就当做“不在黑名单中”,继续执行默认通过逻辑
37
- ctx.logger.warn(`读取黑名单表失败 (可能是表不存在),将默认通过请求。错误信息: ${error.message}`);
35
+ // 数据库错误时默认放行
38
36
  shouldApprove = true;
39
37
  }
40
38
  // 第二步:根据检查结果决定是否通过
41
- if (shouldApprove && messageId && userId) {
39
+ if (shouldApprove && messageId) {
42
40
  try {
43
41
  await bot.handleFriendRequest(messageId, true);
44
42
  ctx.logger.info(`已自动通过用户 ${userId} 的好友请求。`);
45
- // 稍微延迟确保好友关系建立
46
- await new Promise(resolve => setTimeout(resolve, 500));
47
- await bot.sendPrivateMessage(userId, config.replyMessage);
48
43
  }
49
44
  catch (error) {
50
45
  ctx.logger.error(`处理好友请求动作失败 (User: ${userId}):`, error);
51
46
  }
52
47
  }
53
48
  });
49
+ // @ts-ignore
50
+ ctx.on('friend-added', async (session) => {
51
+ // 只有当配置了回复消息时才发送
52
+ console.log(`已添加新好友,用户 ID: ${session.userId}`);
53
+ if (config.replyMessage) {
54
+ try {
55
+ // 使用 session.send 或 bot.sendPrivateMessage 均可
56
+ await session.bot.sendPrivateMessage(session.userId, config.replyMessage);
57
+ }
58
+ catch (error) {
59
+ ctx.logger.error(`发送好友欢迎消息失败 (User: ${session.userId}):`, error);
60
+ }
61
+ }
62
+ });
54
63
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-auto-friend-handler",
3
3
  "description": "自用插件,联动黑名单插件",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [