koishi-plugin-receptionist 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,17 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "receptionist";
3
+ export interface Config {
4
+ isAt: boolean;
5
+ }
6
+ export declare const usage = "\n\u5F53\u7FA4\u5458\u52A0\u5165\u65F6\u53D1\u9001\u6B22\u8FCE\u6D88\u606F\n";
7
+ export declare const Config: Schema<Config>;
8
+ declare module "koishi" {
9
+ interface Tables {
10
+ "receptionist-data": WelcomeDB;
11
+ }
12
+ }
13
+ export interface WelcomeDB {
14
+ id: string;
15
+ words: string;
16
+ }
17
+ export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js ADDED
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Config = exports.usage = exports.name = void 0;
4
+ exports.apply = apply;
5
+ const koishi_1 = require("koishi");
6
+ exports.name = 'receptionist';
7
+ exports.usage = `
8
+ 当群员加入时发送欢迎消息
9
+ `;
10
+ exports.Config = koishi_1.Schema.object({
11
+ isAt: koishi_1.Schema.boolean().default(true).description("at新进群的")
12
+ });
13
+ function apply(ctx, config) {
14
+ ctx.model.extend("receptionist-data", {
15
+ id: "string",
16
+ words: "text"
17
+ });
18
+ const timeInterval = {};
19
+ const cmd = ctx.command("欢迎词", { authority: 1 })
20
+ .action(async ({ session }) => {
21
+ const group = session?.guildId || session?.channelId;
22
+ if (!session?.guildId || !session?.userId)
23
+ return '该指令只能在群组中使用。';
24
+ const dbResult = await ctx.database.get("receptionist-data", { id: group });
25
+ if (dbResult.length === 0)
26
+ return;
27
+ const words = dbResult[0].words;
28
+ const result = words.split(/\r?\n/).map(word => (0, koishi_1.h)('p', word));
29
+ return (0, koishi_1.h)('message', result);
30
+ });
31
+ cmd.subcommand(".设定", { authority: 1 })
32
+ .action(async ({ session }) => {
33
+ const group = session?.guildId || session?.channelId;
34
+ if (!session?.guildId || !session?.userId)
35
+ return '该指令只能在群组中使用。';
36
+ if (!await isUserAdmin(session, session.userId))
37
+ return '权限不足';
38
+ await session.send(`正在为 ${group} 设定欢迎词, 请发送`);
39
+ const words = await session.prompt();
40
+ if (!words)
41
+ return "设定已取消或超时。";
42
+ const existing = await ctx.database.get("receptionist-data", { id: group });
43
+ if (existing.length > 0) {
44
+ await ctx.database.set("receptionist-data", { id: group }, { words });
45
+ }
46
+ else {
47
+ await ctx.database.create("receptionist-data", { id: group, words });
48
+ }
49
+ return (0, koishi_1.h)('message', [
50
+ (0, koishi_1.h)('p', '已设定欢迎词'),
51
+ (0, koishi_1.h)('p', words)
52
+ ]);
53
+ });
54
+ cmd.subcommand(".删除", { authority: 1 })
55
+ .action(async ({ session }) => {
56
+ const group = session?.guildId || session?.channelId;
57
+ if (!session?.guildId || !session?.userId)
58
+ return '该指令只能在群组中使用。';
59
+ if (!await isUserAdmin(session, session.userId))
60
+ return '权限不足';
61
+ await ctx.database.remove("receptionist-data", { id: group });
62
+ return (0, koishi_1.h)('message', [(0, koishi_1.h)('p', '已删除本群欢迎词')]);
63
+ });
64
+ ctx.on("guild-member-added", async (session) => {
65
+ const cacheKey = session.platform + session.guildId;
66
+ // 简单的防抖/节流逻辑
67
+ if (timeInterval[cacheKey] === null)
68
+ return;
69
+ timeInterval[cacheKey] = null;
70
+ setTimeout(() => {
71
+ delete timeInterval[cacheKey];
72
+ }, 1000 * 30);
73
+ const group = session.guildId || session.channelId;
74
+ if (!group)
75
+ return;
76
+ const dbResult = await ctx.database.get("receptionist-data", { id: group });
77
+ if (dbResult.length === 0)
78
+ return;
79
+ const words = dbResult[0].words;
80
+ const result = [];
81
+ if (config.isAt) {
82
+ result.push(koishi_1.h.at(session.userId));
83
+ }
84
+ for (const word of words.split(/\r?\n/)) {
85
+ result.push((0, koishi_1.h)('p', word));
86
+ }
87
+ await session.send((0, koishi_1.h)('message', result));
88
+ });
89
+ }
90
+ async function isUserAdmin(session, userId) {
91
+ if (!session.guildId)
92
+ return false;
93
+ // 使用 (session.user as any) 来规避类型检查,同时保留可选链以防 user 为空
94
+ if (session.user?.authority >= 3)
95
+ return true;
96
+ try {
97
+ const memberInfo = await session.bot.getGuildMember(session.guildId, userId);
98
+ if (!memberInfo)
99
+ return false;
100
+ const adminRoles = ["owner", "admin", "administrator"];
101
+ const memberRoles = [...(memberInfo.roles || [])].flat().filter(Boolean);
102
+ for (const role of memberRoles) {
103
+ if (adminRoles.includes(role.toLowerCase()))
104
+ return true;
105
+ }
106
+ return false;
107
+ }
108
+ catch (error) {
109
+ return false;
110
+ }
111
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "koishi-plugin-receptionist",
3
+ "description": "自用插件",
4
+ "license": "MIT",
5
+ "version": "0.0.1",
6
+ "main": "lib/index.js",
7
+ "typings": "lib/index.d.ts",
8
+ "files": [
9
+ "lib",
10
+ "dist"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/furryaxw/koishi-plugin-receptionist.git"
15
+ },
16
+ "homepage": "https://github.com/furryaxw/koishi-plugin-receptionist",
17
+ "bugs": {
18
+ "url": "https://github.com/furryaxw/koishi-plugin-receptionist/issues"
19
+ },
20
+ "keywords": [
21
+ "chatbot",
22
+ "koishi",
23
+ "plugin"
24
+ ],
25
+ "peerDependencies": {
26
+ "koishi": "4.18.9"
27
+ }
28
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-receptionist
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-receptionist?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-receptionist)
4
+
5
+