koishi-plugin-group-command-blocker 0.0.3 → 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.
Files changed (2) hide show
  1. package/lib/index.js +32 -48
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -28,7 +28,7 @@ module.exports = __toCommonJS(src_exports);
28
28
  var import_koishi = require("koishi");
29
29
  var name = "group-command-blocker";
30
30
  var Config = import_koishi.Schema.object({
31
- admins: import_koishi.Schema.array(import_koishi.Schema.string()).description("允许执行管理指令的用户 ID 列表 (例如 QQ 号)")
31
+ admins: import_koishi.Schema.array(import_koishi.Schema.string()).description("允许执行管理指令的用户 ID 列表")
32
32
  });
33
33
  function apply(ctx, config) {
34
34
  ctx.model.extend("group_disabled_plugins", { id: "unsigned", guildId: "string", pluginName: "string" }, { primary: "id", autoInc: true });
@@ -38,69 +38,44 @@ function apply(ctx, config) {
38
38
  if (!config.admins || config.admins.length === 0) return true;
39
39
  return config.admins.includes(session.userId);
40
40
  }, "isAuthorized");
41
- ctx.command("plugin.disable <name:string>", "在本群禁用指定插件").action(async ({ session }, name2) => {
42
- if (!isAuthorized(session)) return "你没有权限执行这个操作";
43
- if (!session?.guildId || !name2) return "参数不对";
44
- const [record] = await ctx.database.get("group_disabled_plugins", { guildId: session.guildId, pluginName: name2 });
45
- if (record) return "该插件早已被禁用";
41
+ ctx.command("plugin.disable <name:string>", "禁用插件").action(async ({ session }, name2) => {
42
+ if (!isAuthorized(session)) return "无权操作";
43
+ if (!session?.guildId || !name2) return "参数缺失";
46
44
  await ctx.database.create("group_disabled_plugins", { guildId: session.guildId, pluginName: name2 });
47
- return `设置成功,已禁用插件: ${name2}`;
45
+ return `已禁用插件: ${name2}`;
48
46
  });
49
- ctx.command("plugin.enable <name:string>", "在本群恢复指定插件").action(async ({ session }, name2) => {
50
- if (!isAuthorized(session)) return "你没有权限执行这个操作";
51
- if (!session?.guildId) return "";
52
- await ctx.database.remove("group_disabled_plugins", { guildId: session.guildId, pluginName: name2 });
53
- return `设置成功,已恢复插件: ${name2}`;
54
- });
55
- ctx.command("cmd.disable <name:string>", "在本群禁用指定指令").action(async ({ session }, name2) => {
56
- if (!isAuthorized(session)) return "你没有权限执行这个操作";
57
- if (!session?.guildId || !name2) return "参数不对";
58
- const [record] = await ctx.database.get("group_disabled_commands", { guildId: session.guildId, commandName: name2 });
59
- 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 "参数缺失";
60
50
  await ctx.database.create("group_disabled_commands", { guildId: session.guildId, commandName: name2 });
61
- return `设置成功,已禁用指令: ${name2}`;
62
- });
63
- ctx.command("cmd.enable <name:string>", "在本群恢复指定指令").action(async ({ session }, name2) => {
64
- if (!isAuthorized(session)) return "你没有权限执行这个操作";
65
- if (!session?.guildId) return "";
66
- await ctx.database.remove("group_disabled_commands", { guildId: session.guildId, commandName: name2 });
67
- return `设置成功,已恢复指令: ${name2}`;
51
+ return `已禁用指令及其子级: ${name2}`;
68
52
  });
69
- ctx.command("block.user <target:user>", "在本群屏蔽特定用户").action(async ({ session }, target) => {
70
- if (!isAuthorized(session)) return "你没有权限执行这个操作";
71
- if (!session?.guildId || !target) return "你得指定一个用户";
53
+ ctx.command("block.user <target:user>", "屏蔽用户").action(async ({ session }, target) => {
54
+ if (!isAuthorized(session)) return "无权操作";
55
+ if (!session?.guildId || !target) return "请指定用户";
72
56
  const userId = import_koishi.h.parse(target)[0]?.attrs?.id || target;
73
- const [record] = await ctx.database.get("group_blocked_users", { guildId: session.guildId, userId });
74
- if (record) return "这人已经在黑名单里呆着了";
75
57
  await ctx.database.create("group_blocked_users", { guildId: session.guildId, userId });
76
- return `设置成功,已屏蔽用户: ${userId}`;
58
+ return `已屏蔽用户: ${userId}`;
77
59
  });
78
- ctx.command("unblock.user <target:user>", "在本群取消屏蔽用户").action(async ({ session }, target) => {
79
- if (!isAuthorized(session)) return "你没有权限执行这个操作";
80
- if (!session?.guildId) return "";
81
- const userId = import_koishi.h.parse(target)[0]?.attrs?.id || target;
82
- await ctx.database.remove("group_blocked_users", { guildId: session.guildId, userId });
83
- return `设置成功,已解除对用户 ${userId} 的屏蔽`;
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}`;
84
64
  });
85
65
  ctx.middleware(async (session, next) => {
86
66
  if (!session?.guildId || !session?.userId || session.userId === session.selfId) return next();
87
- const [blocked] = await ctx.database.get("group_blocked_users", {
88
- guildId: session.guildId,
89
- userId: session.userId
90
- });
67
+ const [blocked] = await ctx.database.get("group_blocked_users", { guildId: session.guildId, userId: session.userId });
91
68
  if (blocked) return;
92
69
  return next();
93
70
  }, true);
94
71
  ctx.on("command/before-execute", async ({ command, session }) => {
95
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
+ }
96
78
  const pluginName = command?.plugin?.name;
97
- const cmdName = command.name;
98
- if (pluginName === "group-command-blocker") return;
99
- const [disabledCmd] = await ctx.database.get("group_disabled_commands", {
100
- guildId: session.guildId,
101
- commandName: cmdName
102
- });
103
- if (disabledCmd) return "";
104
79
  if (pluginName) {
105
80
  const [disabledPlg] = await ctx.database.get("group_disabled_plugins", {
106
81
  guildId: session.guildId,
@@ -108,6 +83,15 @@ function apply(ctx, config) {
108
83
  });
109
84
  if (disabledPlg) return "";
110
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
+ }
111
95
  });
112
96
  }
113
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.3",
4
+ "version": "0.0.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [