koishi-plugin-group-control 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,23 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "group-control";
3
+ export interface Config {
4
+ welcomeMessage: string;
5
+ blacklistMessage: string;
6
+ quitMessage: string;
7
+ enableBlacklist: boolean;
8
+ quitCommandEnabled: boolean;
9
+ quitCommandAuthority: number;
10
+ }
11
+ export interface BlacklistedGuild {
12
+ platform: string;
13
+ guildId: string;
14
+ timestamp: number;
15
+ reason: string;
16
+ }
17
+ declare module 'koishi' {
18
+ interface Tables {
19
+ blacklisted_guild: BlacklistedGuild;
20
+ }
21
+ }
22
+ export declare const Config: Schema<Config>;
23
+ export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js ADDED
@@ -0,0 +1,211 @@
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 = "group-control";
30
+ var Config = import_koishi.Schema.object({
31
+ welcomeMessage: import_koishi.Schema.string().default("你好,我是机器人。感谢邀请我加入!").description("机器人加入群聊时发送的欢迎消息"),
32
+ blacklistMessage: import_koishi.Schema.string().default("此群聊已被拉黑,机器人将自动退出。").description("被拉入黑名单群后在群内发送的提示"),
33
+ quitMessage: import_koishi.Schema.string().default("收到来自{userId}的指令,即将退出群聊。").description("用户发送quit指令后在群内发送的提示,支持变量{userId}(指令发送者id)"),
34
+ enableBlacklist: import_koishi.Schema.boolean().default(true).description("启用黑名单"),
35
+ quitCommandEnabled: import_koishi.Schema.boolean().default(true).description("启用quit"),
36
+ quitCommandAuthority: import_koishi.Schema.number().default(3).description("quit指令所需权限")
37
+ });
38
+ function isBlacklistEnabled(config) {
39
+ if (!config.enableBlacklist) return "黑名单功能未启用。";
40
+ return null;
41
+ }
42
+ __name(isBlacklistEnabled, "isBlacklistEnabled");
43
+ function parseGuildId(input) {
44
+ const match = input.trim().match(/^onebot:(\d+)$/);
45
+ return match ? match[1] : /^\d+$/.test(input.trim()) ? input.trim() : null;
46
+ }
47
+ __name(parseGuildId, "parseGuildId");
48
+ var BLACKLIST_PLATFORM = "onebot";
49
+ async function getBlacklistedGuild(ctx, guildId) {
50
+ return await ctx.model.get("blacklisted_guild", { platform: BLACKLIST_PLATFORM, guildId });
51
+ }
52
+ __name(getBlacklistedGuild, "getBlacklistedGuild");
53
+ async function removeBlacklistedGuild(ctx, guildId) {
54
+ return await ctx.model.remove("blacklisted_guild", { platform: BLACKLIST_PLATFORM, guildId });
55
+ }
56
+ __name(removeBlacklistedGuild, "removeBlacklistedGuild");
57
+ async function createBlacklistedGuild(ctx, guildId, reason) {
58
+ return await ctx.model.create("blacklisted_guild", {
59
+ platform: BLACKLIST_PLATFORM,
60
+ guildId,
61
+ timestamp: Math.floor(Date.now() / 1e3),
62
+ reason
63
+ });
64
+ }
65
+ __name(createBlacklistedGuild, "createBlacklistedGuild");
66
+ async function getAllBlacklistedGuilds(ctx) {
67
+ return await ctx.model.get("blacklisted_guild", { platform: BLACKLIST_PLATFORM });
68
+ }
69
+ __name(getAllBlacklistedGuilds, "getAllBlacklistedGuilds");
70
+ async function clearBlacklistedGuilds(ctx) {
71
+ return await ctx.model.remove("blacklisted_guild", { platform: BLACKLIST_PLATFORM });
72
+ }
73
+ __name(clearBlacklistedGuilds, "clearBlacklistedGuilds");
74
+ function formatDate(timestamp) {
75
+ return new Date(timestamp * 1e3).toLocaleString();
76
+ }
77
+ __name(formatDate, "formatDate");
78
+ function apply(ctx, config) {
79
+ ctx.model.extend("blacklisted_guild", {
80
+ platform: "string",
81
+ guildId: "string",
82
+ timestamp: "integer",
83
+ reason: "string"
84
+ }, { primary: ["platform", "guildId"] });
85
+ ctx.on("guild-added", async (session) => {
86
+ const { guildId, platform } = session;
87
+ if (!config.enableBlacklist) {
88
+ if (config.welcomeMessage) {
89
+ try {
90
+ await session.bot.sendMessage(guildId, config.welcomeMessage, platform);
91
+ } catch (error) {
92
+ console.error("发送欢迎消息失败:", error);
93
+ }
94
+ }
95
+ return;
96
+ }
97
+ const [blacklisted] = await ctx.model.get("blacklisted_guild", { platform, guildId });
98
+ if (blacklisted) {
99
+ try {
100
+ await session.bot.sendMessage(guildId, config.blacklistMessage, platform);
101
+ } catch (error) {
102
+ console.error("发送黑名单提示失败:", error);
103
+ }
104
+ try {
105
+ await session.bot.internal.setGroupLeave(parseInt(guildId));
106
+ } catch (error) {
107
+ console.error("退出黑名单群聊失败:", error);
108
+ }
109
+ return;
110
+ }
111
+ if (config.welcomeMessage) {
112
+ try {
113
+ await session.bot.sendMessage(guildId, config.welcomeMessage, platform);
114
+ } catch (error) {
115
+ console.error("发送欢迎消息失败:", error);
116
+ }
117
+ }
118
+ });
119
+ ctx.on("guild-removed", async (session) => {
120
+ const { guildId, platform } = session;
121
+ if (!config.enableBlacklist) {
122
+ return;
123
+ }
124
+ await ctx.model.upsert("blacklisted_guild", [{
125
+ platform,
126
+ guildId,
127
+ timestamp: Math.floor(Date.now() / 1e3),
128
+ reason: "kicked"
129
+ }]);
130
+ });
131
+ if (config.quitCommandEnabled) {
132
+ ctx.command("quit", "让机器人主动退出当前群聊", { authority: config.quitCommandAuthority }).action(async ({ session }) => {
133
+ if (!session.guildId) return "quit 指令只能在群聊中使用。";
134
+ const { guildId, platform, userId } = session;
135
+ const message = config.quitMessage.replace("{userId}", userId);
136
+ try {
137
+ await session.bot.sendMessage(session.guildId, message, platform);
138
+ } catch (sendError) {
139
+ console.error("发送退出提示消息失败:", sendError);
140
+ }
141
+ try {
142
+ await session.bot.internal.setGroupLeave(parseInt(guildId));
143
+ } catch (leaveError) {
144
+ console.error("退出群聊失败:", leaveError);
145
+ const failMessage = `退出失败: ${leaveError.message || "OneBot API 调用失败"}`;
146
+ try {
147
+ await session.bot.sendMessage(session.guildId, failMessage, platform);
148
+ } catch (reSendError) {
149
+ console.error("发送退出失败提示也失败:", reSendError);
150
+ }
151
+ return failMessage;
152
+ }
153
+ return "";
154
+ });
155
+ }
156
+ async function viewBlacklist({ session }) {
157
+ const errorMsg = isBlacklistEnabled(config);
158
+ if (errorMsg) return errorMsg;
159
+ const records = await getAllBlacklistedGuilds(ctx);
160
+ if (records.length === 0) return "黑名单为空。";
161
+ let msg = "黑名单列表:\n";
162
+ records.forEach((r) => {
163
+ const time = formatDate(r.timestamp);
164
+ msg += `- ${r.guildId} (时间: ${time})
165
+ `;
166
+ });
167
+ return msg.trim();
168
+ }
169
+ __name(viewBlacklist, "viewBlacklist");
170
+ ctx.command("view-blacklist", "查看被拉黑的群聊列表", { authority: 4 }).action(viewBlacklist);
171
+ async function removeFromBlacklist({}, input) {
172
+ const errorMsg = isBlacklistEnabled(config);
173
+ if (errorMsg) return errorMsg;
174
+ const guildId = parseGuildId(input);
175
+ if (!guildId) return `输入格式错误。请输入纯群号或 onebot:群号 格式。`;
176
+ const removed = await removeBlacklistedGuild(ctx, guildId);
177
+ return removed ? `已移除群聊 ${guildId}` : `群聊 ${guildId} 不在黑名单中。`;
178
+ }
179
+ __name(removeFromBlacklist, "removeFromBlacklist");
180
+ ctx.command("remove-from-blacklist <groupId:text>", "从黑名单移除指定群聊", { authority: 4 }).action(removeFromBlacklist);
181
+ async function addToBlacklist({}, input) {
182
+ const errorMsg = isBlacklistEnabled(config);
183
+ if (errorMsg) return errorMsg;
184
+ const guildId = parseGuildId(input);
185
+ if (!guildId) return `输入格式错误。请输入纯群号或 onebot:群号 格式。`;
186
+ const existing = await getBlacklistedGuild(ctx, guildId);
187
+ if (existing.length > 0) return `群聊 ${guildId} 已在黑名单中。`;
188
+ await createBlacklistedGuild(ctx, guildId, "manual_add");
189
+ return `已添加群聊 ${guildId} 到黑名单。`;
190
+ }
191
+ __name(addToBlacklist, "addToBlacklist");
192
+ ctx.command("add-to-blacklist <groupId:text>", "手动添加群聊到黑名单 (输入群号)", { authority: 4 }).action(addToBlacklist);
193
+ async function clearBlacklist({ session }) {
194
+ const errorMsg = isBlacklistEnabled(config);
195
+ if (errorMsg) return errorMsg;
196
+ const records = await getAllBlacklistedGuilds(ctx);
197
+ const count = records.length;
198
+ if (count === 0) return "黑名单已是空的。";
199
+ await clearBlacklistedGuilds(ctx);
200
+ return `已清空黑名单,共移除 ${count} 个群聊。`;
201
+ }
202
+ __name(clearBlacklist, "clearBlacklist");
203
+ ctx.command("clear-blacklist", "清空黑名单", { authority: 4 }).action(clearBlacklist);
204
+ }
205
+ __name(apply, "apply");
206
+ // Annotate the CommonJS export names for ESM import in node:
207
+ 0 && (module.exports = {
208
+ Config,
209
+ apply,
210
+ name
211
+ });
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "koishi-plugin-group-control",
3
+ "description": "Koishi 机器人进群/退群事件处理插件,支持自定义机器人进群消息、踢出后自动拉黑功能、自定义提示消息和主动退群指令。",
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
+ "koishi",
14
+ "plugin",
15
+ "group",
16
+ "control",
17
+ "join",
18
+ "leave",
19
+ "blacklist",
20
+ "welcome-message",
21
+ "quit-command",
22
+ "onebot",
23
+ "qq"
24
+ ],
25
+ "peerDependencies": {
26
+ "koishi": "^4.18.7"
27
+ }
28
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-group-control
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-group-control?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-group-control)
4
+
5
+ Koishi 机器人进群/退群事件处理插件,支持自定义机器人进群消息、踢出后自动拉黑功能、自定义提示消息和主动退群指令。