koishi-plugin-group-command-blocker 0.0.2 → 0.0.4

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,6 +1,7 @@
1
1
  import { Context, Schema } from 'koishi';
2
2
  export declare const name = "group-command-blocker";
3
3
  export interface Config {
4
+ admins: string[];
4
5
  }
5
6
  export declare const Config: Schema<Config>;
6
7
  declare module 'koishi' {
@@ -25,4 +26,4 @@ export interface GroupBlockedUser {
25
26
  guildId: string;
26
27
  userId: string;
27
28
  }
28
- export declare function apply(ctx: Context): void;
29
+ export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -27,63 +27,55 @@ __export(src_exports, {
27
27
  module.exports = __toCommonJS(src_exports);
28
28
  var import_koishi = require("koishi");
29
29
  var name = "group-command-blocker";
30
- var Config = import_koishi.Schema.object({});
31
- function apply(ctx) {
30
+ var Config = import_koishi.Schema.object({
31
+ admins: import_koishi.Schema.array(import_koishi.Schema.string()).description("允许执行管理指令的用户 ID 列表")
32
+ });
33
+ function apply(ctx, config) {
32
34
  ctx.model.extend("group_disabled_plugins", { id: "unsigned", guildId: "string", pluginName: "string" }, { primary: "id", autoInc: true });
33
35
  ctx.model.extend("group_disabled_commands", { id: "unsigned", guildId: "string", commandName: "string" }, { primary: "id", autoInc: true });
34
36
  ctx.model.extend("group_blocked_users", { id: "unsigned", guildId: "string", userId: "string" }, { primary: "id", autoInc: true });
35
- ctx.command("plugin.disable <name:string>", "在本群禁用指定插件").action(async ({ session }, name2) => {
36
- if (!session?.guildId || !name2) return "缺少参数";
37
- const [record] = await ctx.database.get("group_disabled_plugins", { guildId: session.guildId, pluginName: name2 });
38
- if (record) return "该插件已在禁用名单";
37
+ const isAuthorized = /* @__PURE__ */ __name((session) => {
38
+ if (!config.admins || config.admins.length === 0) return true;
39
+ return config.admins.includes(session.userId);
40
+ }, "isAuthorized");
41
+ ctx.command("plugin.disable <name:string>", "禁用插件").action(async ({ session }, name2) => {
42
+ if (!isAuthorized(session)) return "无权操作";
43
+ if (!session?.guildId || !name2) return "参数缺失";
39
44
  await ctx.database.create("group_disabled_plugins", { guildId: session.guildId, pluginName: name2 });
40
45
  return `已禁用插件: ${name2}`;
41
46
  });
42
- ctx.command("plugin.enable <name:string>", "在本群启用指定插件").action(async ({ session }, name2) => {
43
- if (!session?.guildId) return "";
44
- await ctx.database.remove("group_disabled_plugins", { guildId: session.guildId, pluginName: name2 });
45
- return `已恢复插件: ${name2}`;
46
- });
47
- ctx.command("cmd.disable <name:string>", "在本群禁用指定指令(如 jrrp)").action(async ({ session }, name2) => {
48
- if (!session?.guildId || !name2) return "缺少参数";
49
- const [record] = await ctx.database.get("group_disabled_commands", { guildId: session.guildId, commandName: name2 });
50
- if (record) return "该指令已在禁用名单";
47
+ ctx.command("cmd.disable <name:string>", "禁用指令(及其所有子指令)").action(async ({ session }, name2) => {
48
+ if (!isAuthorized(session)) return "无权操作";
49
+ if (!session?.guildId || !name2) return "参数缺失";
51
50
  await ctx.database.create("group_disabled_commands", { guildId: session.guildId, commandName: name2 });
52
- return `已禁用指令: ${name2}`;
51
+ return `已禁用指令及其子级: ${name2}`;
53
52
  });
54
- ctx.command("cmd.enable <name:string>", "在本群恢复指定指令").action(async ({ session }, name2) => {
55
- if (!session?.guildId) return "";
56
- await ctx.database.remove("group_disabled_commands", { guildId: session.guildId, commandName: name2 });
57
- return `已恢复指令: ${name2}`;
58
- });
59
- ctx.command("block.user <target:user>", "在本群屏蔽特定用户消息").action(async ({ session }, target) => {
53
+ ctx.command("block.user <target:user>", "屏蔽用户").action(async ({ session }, target) => {
54
+ if (!isAuthorized(session)) return "无权操作";
60
55
  if (!session?.guildId || !target) return "请指定用户";
61
56
  const userId = import_koishi.h.parse(target)[0]?.attrs?.id || target;
62
57
  await ctx.database.create("group_blocked_users", { guildId: session.guildId, userId });
63
58
  return `已屏蔽用户: ${userId}`;
64
59
  });
65
- ctx.command("unblock.user <target:user>", "在本群解除用户屏蔽").action(async ({ session }, target) => {
66
- if (!session?.guildId) return "";
67
- const userId = import_koishi.h.parse(target)[0]?.attrs?.id || target;
68
- await ctx.database.remove("group_blocked_users", { guildId: session.guildId, userId });
69
- return `已解除屏蔽`;
60
+ ctx.command("cmd.enable <name:string>", "恢复指令").action(async ({ session }, name2) => {
61
+ if (!isAuthorized(session) || !session?.guildId) return "";
62
+ await ctx.database.remove("group_disabled_commands", { guildId: session.guildId, commandName: name2 });
63
+ return `已恢复指令: ${name2}`;
70
64
  });
71
65
  ctx.middleware(async (session, next) => {
72
66
  if (!session?.guildId || !session?.userId || session.userId === session.selfId) return next();
73
67
  const [blocked] = await ctx.database.get("group_blocked_users", { guildId: session.guildId, userId: session.userId });
74
68
  if (blocked) return;
75
69
  return next();
76
- });
70
+ }, true);
77
71
  ctx.on("command/before-execute", async ({ command, session }) => {
78
72
  if (!command || !session?.guildId) return;
73
+ let current = command;
74
+ while (current) {
75
+ if (current.plugin?.name === "group-command-blocker") return;
76
+ current = current.parent;
77
+ }
79
78
  const pluginName = command?.plugin?.name;
80
- const cmdName = command.name;
81
- if (pluginName === "group-command-blocker") return;
82
- const [disabledCmd] = await ctx.database.get("group_disabled_commands", {
83
- guildId: session.guildId,
84
- commandName: cmdName
85
- });
86
- if (disabledCmd) return "";
87
79
  if (pluginName) {
88
80
  const [disabledPlg] = await ctx.database.get("group_disabled_plugins", {
89
81
  guildId: session.guildId,
@@ -91,6 +83,15 @@ function apply(ctx) {
91
83
  });
92
84
  if (disabledPlg) return "";
93
85
  }
86
+ let cmdToCheck = command;
87
+ while (cmdToCheck) {
88
+ const [disabledCmd] = await ctx.database.get("group_disabled_commands", {
89
+ guildId: session.guildId,
90
+ commandName: cmdToCheck.name
91
+ });
92
+ if (disabledCmd) return "";
93
+ cmdToCheck = cmdToCheck.parent;
94
+ }
94
95
  });
95
96
  }
96
97
  __name(apply, "apply");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-group-command-blocker",
3
3
  "description": "这个东西也可以用来屏蔽群员",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [