koishi-plugin-kick-detection
Version:
自用防踢
173 lines (171 loc) • 5.96 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name2 in all)
__defProp(target, name2, { get: all[name2], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
Config: () => Config,
apply: () => apply,
name: () => name
});
module.exports = __toCommonJS(src_exports);
var import_koishi = require("koishi");
var name = "kick-detection";
var Config = import_koishi.Schema.object({});
function apply(ctx) {
ctx.using(["database"], (ctx2) => {
const SUPER_ADMIN = "1634163370";
ctx2.model.extend("ltyvip", {
id: "unsigned",
uid: "string"
}, {
primary: "id",
autoInc: true
});
ctx2.model.extend("hqfavip", {
id: "unsigned",
uid: "string"
}, {
primary: "id",
autoInc: true
});
ctx2.model.extend("omegavip", {
id: "unsigned",
uid: "string"
}, {
primary: "id",
autoInc: true
});
ctx2.model.extend("tsarvip", {
id: "unsigned",
uid: "string"
}, {
primary: "id",
autoInc: true
});
ctx2.model.extend("spirevip", {
id: "unsigned",
uid: "string"
}, {
primary: "id",
autoInc: true
});
ctx2.model.extend("ryovip", {
id: "unsigned",
uid: "string"
}, {
primary: "id",
autoInc: true
});
const GROUP_TABLE_MAP = {
"759628868": "ltyvip",
"780117539": "hqfavip",
"948135567": "omegavip",
"852428914": "tsarvip",
"920612359": "spirevip",
"881433595": "ryovip"
};
async function checkPermission(session) {
if (session.userId === SUPER_ADMIN) return true;
try {
const memberInfo = await session.bot.getGuildMember(session.guildId, session.userId);
return memberInfo.roles.includes("owner") || memberInfo.roles.includes("admin");
} catch (error) {
ctx2.logger.warn("权限检查失败:", error);
return false;
}
}
__name(checkPermission, "checkPermission");
async function addVip(session, uid) {
if (!session?.channelId) return "请在群聊中使用此命令";
if (!await checkPermission(session)) {
return "权限不足";
}
const tableName = GROUP_TABLE_MAP[session.channelId];
if (!tableName) return "当前群未配置VIP列表";
const allVips = await ctx2.database.get(tableName, {});
const lowerUid = uid.toLowerCase();
const exists = allVips.some((vip) => vip.uid.toLowerCase() === lowerUid);
if (exists) {
return `用户 ${uid} 已是VIP`;
}
await ctx2.database.create(tableName, { uid });
return `已成功添加VIP用户: ${uid}`;
}
__name(addVip, "addVip");
ctx2.command("addvip <uid:string>", "添加VIP用户").action(async ({ session }, uid) => {
return addVip(session, uid);
});
ctx2.middleware(async (session, next) => {
const { content } = session;
const match = content.match(/^[#*]\+v\s+\d+\s+(\w+)\s+\d+$/i);
if (match && session.channelId) {
const uid = match[1];
return addVip(session, uid);
}
return next();
});
ctx2.command("removevip <uid:string>", "移除VIP用户").action(async ({ session }, uid) => {
if (!session?.channelId) return "请在群聊中使用此命令";
if (!await checkPermission(session)) {
return "权限不足";
}
const tableName = GROUP_TABLE_MAP[session.channelId];
if (!tableName) return "当前群未配置VIP列表";
const allVips = await ctx2.database.get(tableName, {});
const lowerUid = uid.toLowerCase();
const target = allVips.find((vip) => vip.uid.toLowerCase() === lowerUid);
if (!target) {
return `用户 ${uid} 不是VIP`;
}
await ctx2.database.remove(tableName, { id: target.id });
return `已成功移除VIP用户: ${uid}`;
});
ctx2.command("viplist", "列出当前群所有VIP用户").action(async ({ session }) => {
if (!session?.channelId) return "请在群聊中使用此命令";
const tableName = GROUP_TABLE_MAP[session.channelId];
if (!tableName) return "当前群未配置VIP列表";
const vipUsers = await ctx2.database.get(tableName, {});
if (vipUsers.length === 0) {
return "当前群组没有VIP用户";
}
return `当前群VIP用户列表:
${vipUsers.map((user) => `- ${user.uid}`).join("\n")}`;
});
ctx2.on("message", async (session) => {
if (!session.channelId) return;
const { content, channelId } = session;
if (!content.includes("在服务器")) return;
const tableName = GROUP_TABLE_MAP[channelId];
if (!tableName) return;
const vipUsers = await ctx2.database.get(tableName, {});
const lowerContent = content.toLowerCase();
const detected = vipUsers.filter((user) => lowerContent.includes(user.uid.toLowerCase()));
if (detected.length > 0) {
await session.send(`检测到VIP用户:${detected.map((u) => u.uid).join(", ")}`);
}
});
});
}
__name(apply, "apply");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Config,
apply,
name
});