koishi-plugin-cocoyyy-console 1.0.0
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 +11 -0
- package/lib/index.js +471 -0
- package/lib/infra/mysql_init.d.ts +5 -0
- package/lib/models/imgs.d.ts +17 -0
- package/lib/models/tags.d.ts +17 -0
- package/lib/services/img_service.d.ts +6 -0
- package/lib/services/tag_service.d.ts +7 -0
- package/lib/utils/common.d.ts +1 -0
- package/lib/utils/config.d.ts +17 -0
- package/package.json +29 -0
- package/readme.md +5 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Context, Logger, Schema } from 'koishi';
|
|
2
|
+
import { MysqlConfig, SaveConfig } from './utils/config';
|
|
3
|
+
export declare const name = "cocoyyy-console";
|
|
4
|
+
export interface Config {
|
|
5
|
+
mysql?: MysqlConfig;
|
|
6
|
+
save?: SaveConfig;
|
|
7
|
+
}
|
|
8
|
+
export declare const Config: Schema<Config>;
|
|
9
|
+
export declare let logger: Logger;
|
|
10
|
+
export declare let savePath: any;
|
|
11
|
+
export declare function apply(ctx: Context, config: Config): Promise<void>;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name2 in all)
|
|
8
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Config: () => Config,
|
|
24
|
+
apply: () => apply,
|
|
25
|
+
logger: () => logger,
|
|
26
|
+
name: () => name,
|
|
27
|
+
savePath: () => savePath
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
var import_koishi3 = require("koishi");
|
|
31
|
+
|
|
32
|
+
// src/utils/config.ts
|
|
33
|
+
var import_koishi = require("koishi");
|
|
34
|
+
var MysqlConfigSchema = import_koishi.Schema.object({
|
|
35
|
+
host: import_koishi.Schema.string().default("127.0.0.1").description("MySQL 主机地址"),
|
|
36
|
+
port: import_koishi.Schema.number().default(3306).description("MySQL 端口"),
|
|
37
|
+
user: import_koishi.Schema.string().default("root").description("用户名"),
|
|
38
|
+
password: import_koishi.Schema.string().role("secret").default("").description("密码"),
|
|
39
|
+
database: import_koishi.Schema.string().default("test").description("数据库名")
|
|
40
|
+
});
|
|
41
|
+
function loadMysqlConfigFromEnv(env = process.env) {
|
|
42
|
+
return {
|
|
43
|
+
host: env.MYSQL_HOST || "127.0.0.1",
|
|
44
|
+
port: Number(env.MYSQL_PORT || 3306),
|
|
45
|
+
user: env.MYSQL_USER || "root",
|
|
46
|
+
password: env.MYSQL_PASSWORD || "",
|
|
47
|
+
database: env.MYSQL_DATABASE || "test"
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
__name(loadMysqlConfigFromEnv, "loadMysqlConfigFromEnv");
|
|
51
|
+
var SaveConfigSchema = import_koishi.Schema.object({
|
|
52
|
+
save_path: import_koishi.Schema.string().default("").description("保存图片路径(绝对路径)")
|
|
53
|
+
});
|
|
54
|
+
function resolveTagBaseDir(config) {
|
|
55
|
+
if (config?.save_path && config.save_path.trim().length > 0) return config.save_path;
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
__name(resolveTagBaseDir, "resolveTagBaseDir");
|
|
59
|
+
|
|
60
|
+
// src/infra/mysql_init.ts
|
|
61
|
+
var import_sequelize = require("sequelize");
|
|
62
|
+
var sequelize = null;
|
|
63
|
+
function initSequelize(cfg) {
|
|
64
|
+
const conf = cfg ?? loadMysqlConfigFromEnv();
|
|
65
|
+
sequelize = new import_sequelize.Sequelize(conf.database, conf.user, conf.password, {
|
|
66
|
+
host: conf.host,
|
|
67
|
+
port: conf.port,
|
|
68
|
+
dialect: "mysql",
|
|
69
|
+
logging: false
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
__name(initSequelize, "initSequelize");
|
|
73
|
+
function getSequelize() {
|
|
74
|
+
if (!sequelize) {
|
|
75
|
+
initSequelize();
|
|
76
|
+
}
|
|
77
|
+
return sequelize;
|
|
78
|
+
}
|
|
79
|
+
__name(getSequelize, "getSequelize");
|
|
80
|
+
async function testConnect() {
|
|
81
|
+
try {
|
|
82
|
+
const db = getSequelize();
|
|
83
|
+
await db.authenticate();
|
|
84
|
+
logger.info("数据库连接成功");
|
|
85
|
+
return true;
|
|
86
|
+
} catch (e) {
|
|
87
|
+
logger.info("数据库连接失败:", e?.message || e);
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
__name(testConnect, "testConnect");
|
|
92
|
+
|
|
93
|
+
// src/models/tags.ts
|
|
94
|
+
var import_sequelize2 = require("sequelize");
|
|
95
|
+
var TagsModel = class extends import_sequelize2.Model {
|
|
96
|
+
static {
|
|
97
|
+
__name(this, "TagsModel");
|
|
98
|
+
}
|
|
99
|
+
id;
|
|
100
|
+
tags;
|
|
101
|
+
alias;
|
|
102
|
+
status;
|
|
103
|
+
createtime;
|
|
104
|
+
};
|
|
105
|
+
var inited = false;
|
|
106
|
+
function getTagsModel() {
|
|
107
|
+
const sequelize2 = getSequelize();
|
|
108
|
+
if (!inited) {
|
|
109
|
+
TagsModel.init(
|
|
110
|
+
{
|
|
111
|
+
id: {
|
|
112
|
+
type: import_sequelize2.DataTypes.INTEGER.UNSIGNED,
|
|
113
|
+
allowNull: false,
|
|
114
|
+
autoIncrement: true,
|
|
115
|
+
primaryKey: true
|
|
116
|
+
},
|
|
117
|
+
tags: {
|
|
118
|
+
type: import_sequelize2.DataTypes.STRING(191),
|
|
119
|
+
allowNull: false,
|
|
120
|
+
unique: true
|
|
121
|
+
},
|
|
122
|
+
alias: {
|
|
123
|
+
type: import_sequelize2.DataTypes.STRING(191),
|
|
124
|
+
allowNull: true
|
|
125
|
+
},
|
|
126
|
+
status: {
|
|
127
|
+
type: import_sequelize2.DataTypes.TINYINT,
|
|
128
|
+
allowNull: false,
|
|
129
|
+
defaultValue: 0
|
|
130
|
+
// 0: 正常, 1: 软删除
|
|
131
|
+
},
|
|
132
|
+
createtime: {
|
|
133
|
+
type: import_sequelize2.DataTypes.DATE,
|
|
134
|
+
allowNull: false,
|
|
135
|
+
defaultValue: import_sequelize2.DataTypes.NOW,
|
|
136
|
+
field: "createtime",
|
|
137
|
+
set(value) {
|
|
138
|
+
if (typeof value === "number") {
|
|
139
|
+
const ts = value > 1e12 ? value : value * 1e3;
|
|
140
|
+
this.setDataValue("createtime", new Date(ts));
|
|
141
|
+
} else {
|
|
142
|
+
this.setDataValue("createtime", value);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
sequelize: sequelize2,
|
|
149
|
+
tableName: "tags",
|
|
150
|
+
modelName: "Tags",
|
|
151
|
+
timestamps: false,
|
|
152
|
+
underscored: false
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
inited = true;
|
|
156
|
+
}
|
|
157
|
+
return TagsModel;
|
|
158
|
+
}
|
|
159
|
+
__name(getTagsModel, "getTagsModel");
|
|
160
|
+
|
|
161
|
+
// src/models/imgs.ts
|
|
162
|
+
var import_sequelize3 = require("sequelize");
|
|
163
|
+
var ImgsModel = class extends import_sequelize3.Model {
|
|
164
|
+
static {
|
|
165
|
+
__name(this, "ImgsModel");
|
|
166
|
+
}
|
|
167
|
+
id;
|
|
168
|
+
tag_id;
|
|
169
|
+
img_url;
|
|
170
|
+
status;
|
|
171
|
+
createtime;
|
|
172
|
+
};
|
|
173
|
+
var inited2 = false;
|
|
174
|
+
function getImgsModel() {
|
|
175
|
+
const sequelize2 = getSequelize();
|
|
176
|
+
if (!inited2) {
|
|
177
|
+
ImgsModel.init(
|
|
178
|
+
{
|
|
179
|
+
id: {
|
|
180
|
+
type: import_sequelize3.DataTypes.INTEGER.UNSIGNED,
|
|
181
|
+
allowNull: false,
|
|
182
|
+
autoIncrement: true,
|
|
183
|
+
primaryKey: true
|
|
184
|
+
},
|
|
185
|
+
tag_id: {
|
|
186
|
+
type: import_sequelize3.DataTypes.INTEGER.UNSIGNED,
|
|
187
|
+
allowNull: false
|
|
188
|
+
},
|
|
189
|
+
img_url: {
|
|
190
|
+
type: import_sequelize3.DataTypes.STRING(512),
|
|
191
|
+
allowNull: false
|
|
192
|
+
},
|
|
193
|
+
status: {
|
|
194
|
+
type: import_sequelize3.DataTypes.TINYINT,
|
|
195
|
+
allowNull: false,
|
|
196
|
+
defaultValue: 0
|
|
197
|
+
// 0: 正常, 1: 软删除
|
|
198
|
+
},
|
|
199
|
+
createtime: {
|
|
200
|
+
type: import_sequelize3.DataTypes.DATE,
|
|
201
|
+
allowNull: false,
|
|
202
|
+
defaultValue: import_sequelize3.DataTypes.NOW,
|
|
203
|
+
field: "createtime",
|
|
204
|
+
set(value) {
|
|
205
|
+
if (typeof value === "number") {
|
|
206
|
+
const ts = value > 1e12 ? value : value * 1e3;
|
|
207
|
+
this.setDataValue("createtime", new Date(ts));
|
|
208
|
+
} else {
|
|
209
|
+
this.setDataValue("createtime", value);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
sequelize: sequelize2,
|
|
216
|
+
tableName: "imgs",
|
|
217
|
+
modelName: "Imgs",
|
|
218
|
+
timestamps: false,
|
|
219
|
+
underscored: false
|
|
220
|
+
}
|
|
221
|
+
);
|
|
222
|
+
inited2 = true;
|
|
223
|
+
}
|
|
224
|
+
return ImgsModel;
|
|
225
|
+
}
|
|
226
|
+
__name(getImgsModel, "getImgsModel");
|
|
227
|
+
|
|
228
|
+
// src/utils/common.ts
|
|
229
|
+
function is_at_bot(session) {
|
|
230
|
+
const elements = Array.isArray(session?.elements) ? session.elements : [];
|
|
231
|
+
const atElement = elements.find((element) => element?.type === "at");
|
|
232
|
+
const atId = atElement?.attrs?.id;
|
|
233
|
+
const selfId = session?.bot?.selfId ?? session?.selfId;
|
|
234
|
+
if (atId == null || selfId == null) return false;
|
|
235
|
+
return String(atId) === String(selfId);
|
|
236
|
+
}
|
|
237
|
+
__name(is_at_bot, "is_at_bot");
|
|
238
|
+
|
|
239
|
+
// src/services/tag_service.ts
|
|
240
|
+
var import_sequelize4 = require("sequelize");
|
|
241
|
+
var import_fs = require("fs");
|
|
242
|
+
var import_path = require("path");
|
|
243
|
+
async function findTagByName(name2) {
|
|
244
|
+
const Tag = getTagsModel();
|
|
245
|
+
return await Tag.findOne({
|
|
246
|
+
where: {
|
|
247
|
+
status: 0,
|
|
248
|
+
[import_sequelize4.Op.or]: [
|
|
249
|
+
{ tags: name2 },
|
|
250
|
+
{ alias: { [import_sequelize4.Op.like]: `%${name2}%` } }
|
|
251
|
+
]
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
__name(findTagByName, "findTagByName");
|
|
256
|
+
async function findDeletedTagByName(name2) {
|
|
257
|
+
const Tag = getTagsModel();
|
|
258
|
+
return await Tag.findOne({
|
|
259
|
+
where: {
|
|
260
|
+
status: 1,
|
|
261
|
+
[import_sequelize4.Op.or]: [
|
|
262
|
+
{ tags: name2 },
|
|
263
|
+
{ alias: { [import_sequelize4.Op.like]: `%${name2}%` } }
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
__name(findDeletedTagByName, "findDeletedTagByName");
|
|
269
|
+
async function createorUpdateTag(name2, alias, savePath2) {
|
|
270
|
+
try {
|
|
271
|
+
const Tag = getTagsModel();
|
|
272
|
+
let existed = await findTagByName(name2);
|
|
273
|
+
if (existed) {
|
|
274
|
+
if (alias == null) {
|
|
275
|
+
throw new Error("already existed!");
|
|
276
|
+
}
|
|
277
|
+
let now_alias = existed.get("alias");
|
|
278
|
+
if (now_alias != null) {
|
|
279
|
+
const list = now_alias.split(";");
|
|
280
|
+
if (!list.includes(alias)) list.push(alias);
|
|
281
|
+
now_alias = list.join(";");
|
|
282
|
+
} else {
|
|
283
|
+
now_alias = alias;
|
|
284
|
+
}
|
|
285
|
+
existed.alias = now_alias;
|
|
286
|
+
await existed.save();
|
|
287
|
+
return { result: true, error: null };
|
|
288
|
+
}
|
|
289
|
+
existed = await findDeletedTagByName(name2);
|
|
290
|
+
if (existed) {
|
|
291
|
+
if (existed.get("status") != 0) {
|
|
292
|
+
existed.set("status", 0);
|
|
293
|
+
await existed.save();
|
|
294
|
+
return { result: true, error: null };
|
|
295
|
+
}
|
|
296
|
+
throw new Error("already existed!");
|
|
297
|
+
}
|
|
298
|
+
if (savePath2 == null) {
|
|
299
|
+
throw new Error("saving path config wrong!");
|
|
300
|
+
}
|
|
301
|
+
const now = new Date(Date.now() + 8 * 60 * 60 * 1e3);
|
|
302
|
+
const tag = await Tag.create({ tags: name2, alias: alias ?? null, createtime: now });
|
|
303
|
+
const dir = (0, import_path.join)(savePath2, String(tag.id));
|
|
304
|
+
(0, import_fs.mkdirSync)(dir, { recursive: true });
|
|
305
|
+
return { result: true, error: null };
|
|
306
|
+
} catch (e) {
|
|
307
|
+
logger.error("[createorUpdateTag Error]: " + e?.message || String(e));
|
|
308
|
+
return { result: false, error: e?.message || String(e) };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
__name(createorUpdateTag, "createorUpdateTag");
|
|
312
|
+
async function getTag(name2) {
|
|
313
|
+
try {
|
|
314
|
+
let tag = await findTagByName(name2);
|
|
315
|
+
return tag;
|
|
316
|
+
} catch (e) {
|
|
317
|
+
logger.error("[getTag Error]: " + e?.message || String(e));
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
__name(getTag, "getTag");
|
|
322
|
+
|
|
323
|
+
// src/services/img_service.ts
|
|
324
|
+
var import_koishi2 = require("koishi");
|
|
325
|
+
var import_path2 = require("path");
|
|
326
|
+
var import_fs2 = require("fs");
|
|
327
|
+
async function saveImg(name2, img, savePath2) {
|
|
328
|
+
try {
|
|
329
|
+
const Img = getImgsModel();
|
|
330
|
+
logger.info("[saveImg Info]: " + img);
|
|
331
|
+
let tag = await getTag(name2);
|
|
332
|
+
if (tag == null) {
|
|
333
|
+
throw new Error(`not found tag '${name2}'!`);
|
|
334
|
+
}
|
|
335
|
+
if (tag.get("status") !== 0) {
|
|
336
|
+
logger.info(`[saveImg Info]:tag '${name2}' is deleted`);
|
|
337
|
+
throw new Error(`tag '${name2}' is deleted!`);
|
|
338
|
+
}
|
|
339
|
+
if (savePath2 == null) {
|
|
340
|
+
throw new Error("saving path config wrong!");
|
|
341
|
+
}
|
|
342
|
+
let dir = (0, import_path2.join)(savePath2, String(tag.get("id")));
|
|
343
|
+
const now = new Date(Date.now() + 8 * 60 * 60 * 1e3);
|
|
344
|
+
const filename = now.getFullYear().toString() + String(now.getMonth() + 1).padStart(2, "0") + String(now.getDate()).padStart(2, "0") + String(now.getHours()).padStart(2, "0") + String(now.getMinutes()).padStart(2, "0") + String(now.getSeconds()).padStart(2, "0") + ".txt";
|
|
345
|
+
let filePath = (0, import_path2.join)(dir, filename);
|
|
346
|
+
await import_fs2.promises.writeFile(filePath, img);
|
|
347
|
+
const saveImg2 = await Img.create({ tag_id: tag.get("id"), img_url: filePath, createtime: now });
|
|
348
|
+
logger.info("[saveImg Info]: save image to '" + name2 + "' success");
|
|
349
|
+
return { result: true, error: null };
|
|
350
|
+
} catch (e) {
|
|
351
|
+
logger.error("[saveImg Error]: " + e?.message || String(e));
|
|
352
|
+
return { result: false, error: e?.message || String(e) };
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
__name(saveImg, "saveImg");
|
|
356
|
+
async function randomImgByTag(name2, savePath2) {
|
|
357
|
+
try {
|
|
358
|
+
const Img = getImgsModel();
|
|
359
|
+
const existed = await getTag(name2);
|
|
360
|
+
if (!existed) {
|
|
361
|
+
throw new Error(`not found tag '${name2}'!`);
|
|
362
|
+
}
|
|
363
|
+
if (existed.status == 0) {
|
|
364
|
+
throw new Error(`not found tag '${name2}'!`);
|
|
365
|
+
}
|
|
366
|
+
let ImgList = await Img.findAll({
|
|
367
|
+
where: {
|
|
368
|
+
status: 0,
|
|
369
|
+
tag_id: existed.get("id")
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
if (ImgList.length <= 0) {
|
|
373
|
+
throw new Error("Don't have image!");
|
|
374
|
+
}
|
|
375
|
+
const random = new import_koishi2.Random(() => Math.random());
|
|
376
|
+
let result = ImgList[random.int(0, ImgList.length)];
|
|
377
|
+
const filePath = result.get("img_url");
|
|
378
|
+
const fileContent = await import_fs2.promises.readFile(filePath, { encoding: "utf-8" });
|
|
379
|
+
return fileContent;
|
|
380
|
+
} catch (e) {
|
|
381
|
+
logger.error("[randomImgByTag Error]: " + e?.message || String(e));
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
__name(randomImgByTag, "randomImgByTag");
|
|
386
|
+
|
|
387
|
+
// src/index.ts
|
|
388
|
+
var name = "cocoyyy-console";
|
|
389
|
+
var Config = import_koishi3.Schema.object({
|
|
390
|
+
mysql: MysqlConfigSchema.description("MySQL 数据库配置"),
|
|
391
|
+
save: SaveConfigSchema.description("图片保存配置")
|
|
392
|
+
});
|
|
393
|
+
var menuList = [
|
|
394
|
+
{
|
|
395
|
+
name: "maketag",
|
|
396
|
+
description: "创建一个标签",
|
|
397
|
+
command: "maketag [标签]"
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
name: "add",
|
|
401
|
+
description: "给标签添加图片",
|
|
402
|
+
command: "add [标签] [图片]"
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
name: "get",
|
|
406
|
+
description: "给标签添加图片",
|
|
407
|
+
command: "get [标签]"
|
|
408
|
+
}
|
|
409
|
+
];
|
|
410
|
+
var logger = new import_koishi3.Logger(name);
|
|
411
|
+
var savePath = null;
|
|
412
|
+
async function apply(ctx, config) {
|
|
413
|
+
const mysqlConf = config?.mysql ?? loadMysqlConfigFromEnv();
|
|
414
|
+
initSequelize(mysqlConf);
|
|
415
|
+
const connected = await testConnect();
|
|
416
|
+
if (connected) {
|
|
417
|
+
await getTagsModel().sync();
|
|
418
|
+
await getImgsModel().sync();
|
|
419
|
+
}
|
|
420
|
+
savePath = resolveTagBaseDir(config?.save);
|
|
421
|
+
ctx.command("fullhelp", "帮助菜单").check(({ session }) => {
|
|
422
|
+
if (!connected) return;
|
|
423
|
+
if (!is_at_bot(session)) return;
|
|
424
|
+
}).action(async ({ session }) => {
|
|
425
|
+
return `帮助菜单[所有命令都需要@bot]:
|
|
426
|
+
${menuList.map((item) => item.command + ": " + item.description).join("\n ")}`;
|
|
427
|
+
});
|
|
428
|
+
ctx.command("maketag <参数>", "创建一个标签").check(({ session }) => {
|
|
429
|
+
if (!connected) return;
|
|
430
|
+
if (!is_at_bot(session)) return;
|
|
431
|
+
}).action(async ({ session }, ...args) => {
|
|
432
|
+
const tag = args?.[0];
|
|
433
|
+
if (!tag) return "请提供正确格式,如:@bot maketag [标签]";
|
|
434
|
+
let exec = await createorUpdateTag(tag, null, savePath);
|
|
435
|
+
if (!exec.result)
|
|
436
|
+
return `创建标签失败:${exec.error}`;
|
|
437
|
+
return `已创建标签:${tag}`;
|
|
438
|
+
});
|
|
439
|
+
ctx.command("add <参数>", "给标签添加图片").check(({ session }) => {
|
|
440
|
+
if (!connected) return;
|
|
441
|
+
if (!is_at_bot(session)) return;
|
|
442
|
+
}).action(async ({ session }, ...args) => {
|
|
443
|
+
const tag = args?.[0];
|
|
444
|
+
const img = args?.[1];
|
|
445
|
+
if (!tag || !img) return "请提供正确格式,如:@bot add [标签] [图片]";
|
|
446
|
+
let exec = await saveImg(tag, img, savePath);
|
|
447
|
+
if (!exec.result)
|
|
448
|
+
return `保存图片失败:${exec.error}`;
|
|
449
|
+
return "保存图片成功";
|
|
450
|
+
});
|
|
451
|
+
ctx.command("get <参数>", "获取标签对应图片").check(({ session }) => {
|
|
452
|
+
if (!connected) return;
|
|
453
|
+
if (!is_at_bot(session)) return;
|
|
454
|
+
}).action(async ({ session }, ...args) => {
|
|
455
|
+
const tag = args?.[0];
|
|
456
|
+
if (!tag) return "请提供正确格式,如:@bot get [标签]";
|
|
457
|
+
let exec = await randomImgByTag(tag, savePath);
|
|
458
|
+
if (!exec)
|
|
459
|
+
return `获取图片失败`;
|
|
460
|
+
return exec;
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
__name(apply, "apply");
|
|
464
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
465
|
+
0 && (module.exports = {
|
|
466
|
+
Config,
|
|
467
|
+
apply,
|
|
468
|
+
logger,
|
|
469
|
+
name,
|
|
470
|
+
savePath
|
|
471
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Model, Optional } from 'sequelize';
|
|
2
|
+
export interface ImgAttributes {
|
|
3
|
+
id: number;
|
|
4
|
+
tag_id: number;
|
|
5
|
+
img_url: string;
|
|
6
|
+
status: number | null;
|
|
7
|
+
createtime: Date;
|
|
8
|
+
}
|
|
9
|
+
export type ImgCreationAttributes = Optional<ImgAttributes, 'id' | 'status' | 'createtime'>;
|
|
10
|
+
export declare class ImgsModel extends Model<ImgAttributes, ImgCreationAttributes> implements ImgAttributes {
|
|
11
|
+
id: number;
|
|
12
|
+
tag_id: number;
|
|
13
|
+
img_url: string;
|
|
14
|
+
status: number | null;
|
|
15
|
+
createtime: Date;
|
|
16
|
+
}
|
|
17
|
+
export declare function getImgsModel(): typeof ImgsModel;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Model, Optional } from 'sequelize';
|
|
2
|
+
export interface TagAttributes {
|
|
3
|
+
id: number;
|
|
4
|
+
tags: string;
|
|
5
|
+
alias: string | null;
|
|
6
|
+
status: number | null;
|
|
7
|
+
createtime: Date;
|
|
8
|
+
}
|
|
9
|
+
export type TagCreationAttributes = Optional<TagAttributes, 'id' | 'alias' | 'status' | 'createtime'>;
|
|
10
|
+
export declare class TagsModel extends Model<TagAttributes, TagCreationAttributes> implements TagAttributes {
|
|
11
|
+
id: number;
|
|
12
|
+
tags: string;
|
|
13
|
+
alias: string | null;
|
|
14
|
+
status: number | null;
|
|
15
|
+
createtime: Date;
|
|
16
|
+
}
|
|
17
|
+
export declare function getTagsModel(): typeof TagsModel;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TagsModel } from '../models/tags';
|
|
2
|
+
declare function createorUpdateTag(name: string, alias?: string | null, savePath?: string | null): Promise<{
|
|
3
|
+
result: boolean;
|
|
4
|
+
error: string | null;
|
|
5
|
+
}>;
|
|
6
|
+
declare function getTag(name: string): Promise<TagsModel | null>;
|
|
7
|
+
export { createorUpdateTag, getTag };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function is_at_bot(session: any): boolean;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Schema } from 'koishi';
|
|
2
|
+
interface MysqlConfig {
|
|
3
|
+
host: string;
|
|
4
|
+
port: number;
|
|
5
|
+
user: string;
|
|
6
|
+
password: string;
|
|
7
|
+
database: string;
|
|
8
|
+
}
|
|
9
|
+
declare const MysqlConfigSchema: Schema<MysqlConfig>;
|
|
10
|
+
declare function loadMysqlConfigFromEnv(env?: NodeJS.ProcessEnv): MysqlConfig;
|
|
11
|
+
declare function toMysqlUri(cfg: MysqlConfig): string;
|
|
12
|
+
interface SaveConfig {
|
|
13
|
+
save_path: string;
|
|
14
|
+
}
|
|
15
|
+
declare const SaveConfigSchema: Schema<SaveConfig>;
|
|
16
|
+
declare function resolveTagBaseDir(config?: SaveConfig): string;
|
|
17
|
+
export { MysqlConfigSchema, SaveConfigSchema, MysqlConfig, SaveConfig, loadMysqlConfigFromEnv, toMysqlUri, resolveTagBaseDir };
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-cocoyyy-console",
|
|
3
|
+
"description": "for self using, function console",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"contributors": [
|
|
8
|
+
"黑椰Coco"
|
|
9
|
+
],
|
|
10
|
+
"files": [
|
|
11
|
+
"lib",
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"scripts": {},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"chatbot",
|
|
18
|
+
"koishi",
|
|
19
|
+
"plugin"
|
|
20
|
+
],
|
|
21
|
+
"devDependencies": {},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"koishi": "^4.18.7"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"mysql2": "^3.11.0",
|
|
27
|
+
"sequelize": "^6.37.3"
|
|
28
|
+
}
|
|
29
|
+
}
|