koishi-plugin-xxfd-1 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,25 @@
1
+ import { Context, Schema } from 'koishi';
2
+ declare module 'koishi' {
3
+ interface Tables {
4
+ repeater_record: RepeaterRecord;
5
+ }
6
+ }
7
+ export declare const name = "xxfd-1";
8
+ interface RepeaterRecord {
9
+ id?: number;
10
+ message: string;
11
+ targetGroup: string;
12
+ sendTime: number;
13
+ createdAt: number;
14
+ }
15
+ export interface Config {
16
+ countFrequency: number;
17
+ receiveGroups: string[];
18
+ sendGroups: string[];
19
+ blockUsers: string[];
20
+ minDelay: number;
21
+ maxDelay: number;
22
+ }
23
+ export declare const Config: Schema<Config>;
24
+ export declare function apply(ctx: Context, config: Config): void;
25
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,116 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Config: () => Config,
24
+ apply: () => apply,
25
+ name: () => name
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+ var import_koishi = require("koishi");
29
+ var name = "xxfd-1";
30
+ var Config = import_koishi.Schema.object({
31
+ countFrequency: import_koishi.Schema.number().default(100).required().description("计数频率(每N条消息触发一次复读)"),
32
+ receiveGroups: import_koishi.Schema.array(import_koishi.Schema.string()).required().description("接收消息的群聊列表(填写群号)"),
33
+ sendGroups: import_koishi.Schema.array(import_koishi.Schema.string()).required().description("发送复读消息的群聊列表(填写群号)"),
34
+ blockUsers: import_koishi.Schema.array(import_koishi.Schema.string()).default([]).description("屏蔽消息的QQ号列表(不统计这些人的消息)"),
35
+ minDelay: import_koishi.Schema.number().default(3).required().description("复读延迟最低时间(单位:秒)"),
36
+ maxDelay: import_koishi.Schema.number().default(600).required().description("复读延迟最高时间(单位:秒,默认10分钟=600秒)")
37
+ }).description("复读计数插件配置");
38
+ function apply(ctx, config) {
39
+ if (config.minDelay > config.maxDelay) {
40
+ ctx.logger("repeater").error("配置错误:复读延迟最低时间不能大于最高时间!");
41
+ return;
42
+ }
43
+ if (!config.receiveGroups.length || !config.sendGroups.length) {
44
+ ctx.logger("repeater").error("配置错误:接收/发送群聊列表不能为空!");
45
+ return;
46
+ }
47
+ ctx.model.extend("repeater_record", {
48
+ id: "unsigned",
49
+ message: "text",
50
+ targetGroup: "string",
51
+ sendTime: "unsigned",
52
+ createdAt: "unsigned"
53
+ }, {
54
+ primary: "id",
55
+ autoInc: true
56
+ });
57
+ let messageCount = 0;
58
+ const sendingTasks = /* @__PURE__ */ new Map();
59
+ ctx.on("message", async (session) => {
60
+ try {
61
+ if (!session.guildId) return;
62
+ if (!config.receiveGroups.includes(session.guildId)) return;
63
+ if (config.blockUsers.includes(session.userId ?? "")) return;
64
+ const rawContent = session.content ?? "";
65
+ const originalMsg = rawContent;
66
+ if (!originalMsg) return;
67
+ messageCount++;
68
+ ctx.logger("repeater").info(`当前计数:${messageCount}/${config.countFrequency}`);
69
+ if (messageCount >= config.countFrequency) {
70
+ messageCount = 0;
71
+ const delaySeconds = Math.floor(Math.random() * (config.maxDelay - config.minDelay + 1)) + config.minDelay;
72
+ const sendTimestamp = Date.now() + delaySeconds * 1e3;
73
+ const randomGroup = config.sendGroups[Math.floor(Math.random() * config.sendGroups.length)];
74
+ const bot = session.bot;
75
+ if (!bot) return;
76
+ const record = await ctx.database.create("repeater_record", {
77
+ message: originalMsg,
78
+ targetGroup: randomGroup,
79
+ sendTime: sendTimestamp,
80
+ createdAt: Date.now()
81
+ });
82
+ ctx.logger("repeater").info(
83
+ `已触发复读!消息:${originalMsg.slice(0, 20)}... | 延迟:${delaySeconds}s | 目标群:${randomGroup}`
84
+ );
85
+ const taskId = record.id;
86
+ const timeout = setTimeout(async () => {
87
+ try {
88
+ await bot.sendMessage(randomGroup, originalMsg);
89
+ ctx.logger("repeater").info(`复读成功!群:${randomGroup},内容:${originalMsg.slice(0, 20)}...`);
90
+ await ctx.database.remove("repeater_record", { id: taskId });
91
+ } catch (err) {
92
+ ctx.logger("repeater").error("复读发送失败:", err);
93
+ } finally {
94
+ sendingTasks.delete(taskId);
95
+ }
96
+ }, delaySeconds * 1e3);
97
+ sendingTasks.set(taskId, timeout);
98
+ }
99
+ } catch (err) {
100
+ ctx.logger("repeater").error("消息处理异常:", err);
101
+ }
102
+ });
103
+ ctx.on("dispose", () => {
104
+ sendingTasks.forEach((timeout) => clearTimeout(timeout));
105
+ sendingTasks.clear();
106
+ ctx.logger("repeater").info("插件已停止,所有待发送任务已清理");
107
+ });
108
+ ctx.logger("repeater").info("复读计数插件已启动!");
109
+ }
110
+ __name(apply, "apply");
111
+ // Annotate the CommonJS export names for ESM import in node:
112
+ 0 && (module.exports = {
113
+ Config,
114
+ apply,
115
+ name
116
+ });
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "koishi-plugin-xxfd-1",
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
+ "scripts": {},
13
+ "keywords": [
14
+ "chatbot",
15
+ "koishi",
16
+ "plugin"
17
+ ],
18
+ "devDependencies": {},
19
+ "peerDependencies": {
20
+ "koishi": "^4.18.7"
21
+ }
22
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-xxfd-1
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-koishi?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-koishi)
4
+
5
+ 随机延时复读