koishi-plugin-chatluna-toolbox 0.0.1 → 0.0.2
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.js +88 -49
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -22,6 +22,7 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
Config: () => ConfigSchema,
|
|
24
24
|
ConfigSchema: () => ConfigSchema,
|
|
25
|
+
DEFAULT_GROUP_INFO_ITEMS: () => DEFAULT_GROUP_INFO_ITEMS,
|
|
25
26
|
DEFAULT_MEMBER_INFO_ITEMS: () => DEFAULT_MEMBER_INFO_ITEMS,
|
|
26
27
|
NativeToolsSchema: () => NativeToolsSchema,
|
|
27
28
|
RAW_INTERCEPTOR_TAG: () => RAW_INTERCEPTOR_TAG,
|
|
@@ -132,6 +133,13 @@ var DEFAULT_MEMBER_INFO_ITEMS = [
|
|
|
132
133
|
"joinTime",
|
|
133
134
|
"lastSentTime"
|
|
134
135
|
];
|
|
136
|
+
var DEFAULT_GROUP_INFO_ITEMS = [
|
|
137
|
+
"groupName",
|
|
138
|
+
"groupId",
|
|
139
|
+
"memberCount",
|
|
140
|
+
"ownerList",
|
|
141
|
+
"adminList"
|
|
142
|
+
];
|
|
135
143
|
var RAW_INTERCEPTOR_TAG = "__chatlunaToolboxRawInterceptor";
|
|
136
144
|
|
|
137
145
|
// src/schema/variables.ts
|
|
@@ -172,9 +180,16 @@ var VariablesSchema = import_koishi3.Schema.object({
|
|
|
172
180
|
}).description("\u673A\u5668\u4EBA\u4FE1\u606F\u53D8\u91CF").collapse(),
|
|
173
181
|
groupInfo: import_koishi3.Schema.object({
|
|
174
182
|
variableName: import_koishi3.Schema.string().default("groupInfo").description("\u53D8\u91CF\u540D\u79F0"),
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
183
|
+
items: import_koishi3.Schema.array(
|
|
184
|
+
import_koishi3.Schema.union([
|
|
185
|
+
import_koishi3.Schema.const("groupName").description("\u7FA4\u540D\u79F0"),
|
|
186
|
+
import_koishi3.Schema.const("groupId").description("\u7FA4\u53F7"),
|
|
187
|
+
import_koishi3.Schema.const("memberCount").description("\u6210\u5458\u6570\u91CF"),
|
|
188
|
+
import_koishi3.Schema.const("createTime").description("\u521B\u5EFA\u65F6\u95F4\uFF08\u4EC5\u90E8\u5206\u5B9E\u73B0\u652F\u6301\uFF09"),
|
|
189
|
+
import_koishi3.Schema.const("ownerList").description("\u7FA4\u4E3B\u540D\u5355"),
|
|
190
|
+
import_koishi3.Schema.const("adminList").description("\u7BA1\u7406\u5458\u540D\u5355")
|
|
191
|
+
])
|
|
192
|
+
).role("checkbox").default([...DEFAULT_GROUP_INFO_ITEMS]).description("\u663E\u793A\u7684\u7FA4\u4FE1\u606F\u9879")
|
|
178
193
|
}).description("\u7FA4\u4FE1\u606F\u53D8\u91CF").collapse(),
|
|
179
194
|
random: import_koishi3.Schema.object({
|
|
180
195
|
variableName: import_koishi3.Schema.string().default("random").description("\u53D8\u91CF\u540D\u79F0"),
|
|
@@ -182,13 +197,14 @@ var VariablesSchema = import_koishi3.Schema.object({
|
|
|
182
197
|
max: import_koishi3.Schema.number().default(100).description("\u9ED8\u8BA4\u968F\u673A\u6570\u4E0A\u9650")
|
|
183
198
|
}).description("\u968F\u673A\u6570\u53D8\u91CF").collapse()
|
|
184
199
|
}).default({
|
|
185
|
-
userInfo: {
|
|
200
|
+
userInfo: {
|
|
201
|
+
variableName: "userInfo",
|
|
202
|
+
items: [...DEFAULT_MEMBER_INFO_ITEMS]
|
|
203
|
+
},
|
|
186
204
|
botInfo: { variableName: "botInfo", items: [...DEFAULT_MEMBER_INFO_ITEMS] },
|
|
187
205
|
groupInfo: {
|
|
188
206
|
variableName: "groupInfo",
|
|
189
|
-
|
|
190
|
-
includeCreateTime: true,
|
|
191
|
-
includeOwnersAndAdmins: true
|
|
207
|
+
items: [...DEFAULT_GROUP_INFO_ITEMS]
|
|
192
208
|
},
|
|
193
209
|
random: { variableName: "random", min: 0, max: 100 }
|
|
194
210
|
}).description("\u5176\u4ED6\u53D8\u91CF");
|
|
@@ -1082,16 +1098,24 @@ async function fetchOwnersAndAdmins(session, log) {
|
|
|
1082
1098
|
const list = await bot.getGuildMemberList(guildId);
|
|
1083
1099
|
members = list?.data || null;
|
|
1084
1100
|
}
|
|
1085
|
-
if (!members || !Array.isArray(members) || members.length === 0)
|
|
1101
|
+
if (!members || !Array.isArray(members) || members.length === 0)
|
|
1102
|
+
return null;
|
|
1086
1103
|
const owners = [];
|
|
1087
1104
|
const admins = [];
|
|
1088
1105
|
for (const member of members) {
|
|
1089
1106
|
const roleRaw = member.role || member.roleName || member.permission || member.identity || (Array.isArray(member.roles) ? member.roles[0] : "") || "";
|
|
1090
1107
|
const role = String(roleRaw || "").toLowerCase();
|
|
1091
|
-
const userId = String(
|
|
1092
|
-
|
|
1093
|
-
(item) => item ? String(item).trim() : ""
|
|
1108
|
+
const userId = String(
|
|
1109
|
+
member.user_id || member.userId || member.id || member.qq || member.uid || ""
|
|
1094
1110
|
);
|
|
1111
|
+
const candidates = [
|
|
1112
|
+
member.card,
|
|
1113
|
+
member.remark,
|
|
1114
|
+
member.displayName,
|
|
1115
|
+
member.nick,
|
|
1116
|
+
member.nickname,
|
|
1117
|
+
member.name
|
|
1118
|
+
].map((item) => item ? String(item).trim() : "");
|
|
1095
1119
|
const name2 = candidates.find((item) => item) || userId;
|
|
1096
1120
|
const label = userId ? `${name2}(${userId})` : name2;
|
|
1097
1121
|
if (role === "owner" || role === "master" || role === "leader") {
|
|
@@ -1106,26 +1130,56 @@ async function fetchOwnersAndAdmins(session, log) {
|
|
|
1106
1130
|
return null;
|
|
1107
1131
|
}
|
|
1108
1132
|
}
|
|
1109
|
-
function
|
|
1110
|
-
|
|
1111
|
-
const
|
|
1112
|
-
|
|
1133
|
+
function formatDateOnly2(value) {
|
|
1134
|
+
if (value == null || value === "") return "";
|
|
1135
|
+
const timestamp = Number(value);
|
|
1136
|
+
if (!Number.isFinite(timestamp)) return "";
|
|
1137
|
+
const date = new Date(timestamp < 1e11 ? timestamp * 1e3 : timestamp);
|
|
1138
|
+
if (Number.isNaN(date.valueOf())) return "";
|
|
1139
|
+
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
|
|
1140
|
+
}
|
|
1141
|
+
function renderGroupInfo(group, items, roles) {
|
|
1142
|
+
const values = [];
|
|
1143
|
+
const id = String(group.group_id ?? group.groupId ?? group.id ?? "").trim();
|
|
1144
|
+
const name2 = String(
|
|
1145
|
+
group.group_name ?? group.groupName ?? group.name ?? ""
|
|
1146
|
+
).trim();
|
|
1113
1147
|
const memberCount = group.member_count ?? group.memberCount ?? group.max_member_count;
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
if (
|
|
1122
|
-
|
|
1123
|
-
|
|
1148
|
+
const createTime = formatDateOnly2(group.create_time ?? group.createTime);
|
|
1149
|
+
for (const item of items) {
|
|
1150
|
+
switch (item) {
|
|
1151
|
+
case "groupName":
|
|
1152
|
+
if (name2) values.push(`\u7FA4\u540D\uFF1A${name2}`);
|
|
1153
|
+
break;
|
|
1154
|
+
case "groupId":
|
|
1155
|
+
if (id) values.push(`\u7FA4\u53F7\uFF1A${id}`);
|
|
1156
|
+
break;
|
|
1157
|
+
case "memberCount":
|
|
1158
|
+
if (memberCount != null) values.push(`\u6210\u5458\u6570\u91CF\uFF1A${memberCount}`);
|
|
1159
|
+
break;
|
|
1160
|
+
case "createTime":
|
|
1161
|
+
if (createTime) values.push(`\u521B\u5EFA\u65F6\u95F4\uFF1A${createTime}`);
|
|
1162
|
+
break;
|
|
1163
|
+
case "ownerList": {
|
|
1164
|
+
if (!roles) break;
|
|
1165
|
+
values.push(
|
|
1166
|
+
`\u7FA4\u4E3B\uFF1A${roles.owners.length ? roles.owners.join("\u3001") : "\u65E0"}`
|
|
1167
|
+
);
|
|
1168
|
+
break;
|
|
1169
|
+
}
|
|
1170
|
+
case "adminList": {
|
|
1171
|
+
if (!roles) break;
|
|
1172
|
+
values.push(
|
|
1173
|
+
`\u7BA1\u7406\u5458\uFF1A${roles.admins.length ? roles.admins.join("\u3001") : "\u65E0"}`
|
|
1174
|
+
);
|
|
1175
|
+
break;
|
|
1124
1176
|
}
|
|
1177
|
+
default:
|
|
1178
|
+
break;
|
|
1125
1179
|
}
|
|
1126
1180
|
}
|
|
1127
|
-
|
|
1128
|
-
return
|
|
1181
|
+
if (values.length === 0) return id || name2 || "\u672A\u80FD\u83B7\u53D6\u5F53\u524D\u7FA4\u4FE1\u606F\u3002";
|
|
1182
|
+
return values.join("\n");
|
|
1129
1183
|
}
|
|
1130
1184
|
function createGroupInfoProvider(deps) {
|
|
1131
1185
|
const { config, log } = deps;
|
|
@@ -1134,29 +1188,13 @@ function createGroupInfoProvider(deps) {
|
|
|
1134
1188
|
if (!session) return "\u6682\u65E0\u7FA4\u4FE1\u606F\u3002";
|
|
1135
1189
|
if (!session.guildId) return "";
|
|
1136
1190
|
if (session.platform !== "onebot") return "\u5F53\u524D\u5E73\u53F0\u6682\u4E0D\u652F\u6301\u67E5\u8BE2\u7FA4\u4FE1\u606F\u3002";
|
|
1137
|
-
const
|
|
1138
|
-
includeMemberCount: true,
|
|
1139
|
-
includeCreateTime: true,
|
|
1140
|
-
includeOwnersAndAdmins: true
|
|
1141
|
-
};
|
|
1191
|
+
const items = Array.isArray(config.groupInfo?.items) && config.groupInfo.items.length ? config.groupInfo.items : DEFAULT_GROUP_INFO_ITEMS;
|
|
1142
1192
|
try {
|
|
1143
1193
|
const groupInfo = await fetchGroupInfo(session);
|
|
1144
1194
|
if (!groupInfo) return "\u672A\u80FD\u83B7\u53D6\u5F53\u524D\u7FA4\u4FE1\u606F\u3002";
|
|
1145
|
-
const
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
});
|
|
1149
|
-
if (groupInfoCfg.includeOwnersAndAdmins) {
|
|
1150
|
-
const roles = await fetchOwnersAndAdmins(session, log);
|
|
1151
|
-
if (roles) {
|
|
1152
|
-
const ownersText = roles.owners.length ? roles.owners.join("\u3001") : "\u65E0";
|
|
1153
|
-
const adminsText = roles.admins.length ? roles.admins.join("\u3001") : "\u65E0";
|
|
1154
|
-
return `${baseText}
|
|
1155
|
-
\u7FA4\u4E3B\uFF1A${ownersText}
|
|
1156
|
-
\u7BA1\u7406\u5458\uFF1A${adminsText}`;
|
|
1157
|
-
}
|
|
1158
|
-
}
|
|
1159
|
-
return baseText;
|
|
1195
|
+
const needRoles = items.includes("ownerList") || items.includes("adminList");
|
|
1196
|
+
const roles = needRoles ? await fetchOwnersAndAdmins(session, log) : null;
|
|
1197
|
+
return renderGroupInfo(groupInfo, items, roles);
|
|
1160
1198
|
} catch (error) {
|
|
1161
1199
|
log?.("debug", "\u7FA4\u4FE1\u606F\u53D8\u91CF\u89E3\u6790\u5931\u8D25", error);
|
|
1162
1200
|
return "\u83B7\u53D6\u7FA4\u4FE1\u606F\u5931\u8D25\u3002";
|
|
@@ -1194,7 +1232,7 @@ function normalizeTimestamp2(raw) {
|
|
|
1194
1232
|
if (!Number.isFinite(numeric)) return null;
|
|
1195
1233
|
return numeric < 1e11 ? numeric * 1e3 : numeric;
|
|
1196
1234
|
}
|
|
1197
|
-
function
|
|
1235
|
+
function formatDateOnly3(value) {
|
|
1198
1236
|
if (!value) return "";
|
|
1199
1237
|
const date = new Date(value);
|
|
1200
1238
|
if (Number.isNaN(date.valueOf())) return "";
|
|
@@ -1328,7 +1366,7 @@ function renderUserInfo(session, member, items) {
|
|
|
1328
1366
|
break;
|
|
1329
1367
|
}
|
|
1330
1368
|
case "joinTime": {
|
|
1331
|
-
const joinTime =
|
|
1369
|
+
const joinTime = formatDateOnly3(
|
|
1332
1370
|
normalizeTimestamp2(
|
|
1333
1371
|
pickFirst2(
|
|
1334
1372
|
member?.join_time,
|
|
@@ -1447,6 +1485,7 @@ function apply(ctx, config) {
|
|
|
1447
1485
|
0 && (module.exports = {
|
|
1448
1486
|
Config,
|
|
1449
1487
|
ConfigSchema,
|
|
1488
|
+
DEFAULT_GROUP_INFO_ITEMS,
|
|
1450
1489
|
DEFAULT_MEMBER_INFO_ITEMS,
|
|
1451
1490
|
NativeToolsSchema,
|
|
1452
1491
|
RAW_INTERCEPTOR_TAG,
|