diff --git a/package.json b/package.json index 38aa095..11e4ef7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "el-bot", - "version": "0.3.21", + "version": "0.4.0", "description": "A quick qq bot framework for mirai.", "main": "dist/index.js", "files": [ @@ -76,9 +76,10 @@ "html-to-text": "^5.1.1", "inquirer": "^7.3.3", "js-yaml": "^3.14.0", - "mirai-ts": "^0.6.0", + "mirai-ts": "^0.6.1", "mongodb": "^3.6.1", "node-schedule": "^1.3.2", + "ora": "^5.0.0", "progress": "^2.0.3", "rss-parser": "^3.8.0", "shelljs": "^0.8.4" @@ -89,7 +90,7 @@ "@types/inquirer": "^7.3.1", "@types/js-yaml": "^3.12.4", "@types/mongodb": "^3.5.27", - "@types/node": "^14.6.3", + "@types/node": "^14.6.4", "@types/node-schedule": "^1.3.0", "@types/progress": "^2.0.3", "@types/shelljs": "^0.8.8", @@ -111,5 +112,12 @@ }, "directories": { "lib": "https://github.com/YunYouJun/mirai-ts" + }, + "ecosystem": { + "el-bot-api": "https://github.com/ElpsyCN/el-bot-api", + "el-bot-plugins": "https://github.com/ElpsyCN/el-bot-plugins", + "el-bot-docs": "https://github.com/ElpsyCN/el-bot-docs", + "el-bot-template": "https://github.com/ElpsyCN/el-bot-template", + "el-bot-web": "https://github.com/ElpsyCN/el-bot-web" } } diff --git a/src/bot/db.ts b/src/bot/db.ts index ba2e3b5..78e46e1 100644 --- a/src/bot/db.ts +++ b/src/bot/db.ts @@ -1,6 +1,7 @@ import Bot from "."; import { MongoClient } from "mongodb"; import { dbConfig } from "../el"; +import ora from "ora"; /** * db 配置项 @@ -12,7 +13,7 @@ export async function connectDb(bot: Bot, dbConfig: dbConfig): Promise { const uri = dbConfig.uri || "mongodb://localhost:27017"; const name = dbConfig.name || "el-bot"; - bot.logger.info(`连接 MongoDB 数据库「${name}」...`); + const linkDb = ora(`连接 MongoDB 数据库「${name}」`).start(); const client = await MongoClient.connect(uri, { useUnifiedTopology: true, }).catch((err) => { @@ -22,7 +23,8 @@ export async function connectDb(bot: Bot, dbConfig: dbConfig): Promise { if (!client) return; try { - bot.logger.success(`成功连接 MongoDB 数据库「${name}」`); + linkDb.succeed(); + bot.client = client; bot.db = client.db(name); } catch (err) { diff --git a/src/plugins/limit/index.ts b/src/plugins/limit/index.ts index dcc5644..7e9df35 100644 --- a/src/plugins/limit/index.ts +++ b/src/plugins/limit/index.ts @@ -131,6 +131,3 @@ export default function limit(ctx: Bot, options: LimitOptions) { return data; }; } - -limit.version = "0.0.1"; -limit.description = "限制消息频率"; diff --git a/src/plugins/limit/package.json b/src/plugins/limit/package.json new file mode 100644 index 0000000..8e10fc4 --- /dev/null +++ b/src/plugins/limit/package.json @@ -0,0 +1,11 @@ +{ + "name": "limit", + "private": "true", + "version": "0.0.2", + "description": "限制消息频率", + "author": { + "name": "YunYouJun", + "url": "https://www.yunyoujun.cn", + "email": "me@yunyoujun.cn" + } +} diff --git a/src/plugins/rss.ts b/src/plugins/rss/index.ts similarity index 97% rename from src/plugins/rss.ts rename to src/plugins/rss/index.ts index 7dc3064..324b046 100644 --- a/src/plugins/rss.ts +++ b/src/plugins/rss/index.ts @@ -5,8 +5,8 @@ import schedule from "node-schedule"; import Parser, { CustomFields } from "rss-parser"; import { MessageType, log } from "mirai-ts"; -import ElBot from "../bot"; -import Bot from "../bot"; +import ElBot from "../../bot"; +import Bot from "../../bot"; interface RssConfig { name: string; @@ -194,6 +194,3 @@ export default function rss(ctx: ElBot, options: RssConfig[]) { } }); } - -rss.version = "0.0.1"; -rss.description = "订阅 RSS 信息"; diff --git a/src/plugins/rss/package.json b/src/plugins/rss/package.json new file mode 100644 index 0000000..26abda6 --- /dev/null +++ b/src/plugins/rss/package.json @@ -0,0 +1,11 @@ +{ + "name": "rss", + "private": "true", + "version": "0.0.2", + "description": "订阅 RSS 信息", + "author": { + "name": "YunYouJun", + "url": "https://www.yunyoujun.cn", + "email": "me@yunyoujun.cn" + } +} diff --git a/src/plugins/rss/template.ts b/src/plugins/rss/template.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/plugins/teach/index.ts b/src/plugins/teach/index.ts index ff746ef..5c0e6dd 100644 --- a/src/plugins/teach/index.ts +++ b/src/plugins/teach/index.ts @@ -91,6 +91,3 @@ export default async function teach(ctx: Bot, options: TeachOptions) { } }); } - -teach.version = "0.1.0"; -teach.description = "问答学习"; diff --git a/src/plugins/teach/package.json b/src/plugins/teach/package.json new file mode 100644 index 0000000..dc3880d --- /dev/null +++ b/src/plugins/teach/package.json @@ -0,0 +1,11 @@ +{ + "name": "teach", + "private": "true", + "version": "0.1.0", + "description": "问答学习(教小云做事)", + "author": { + "name": "YunYouJun", + "url": "https://www.yunyoujun.cn", + "email": "me@yunyoujun.cn" + } +} diff --git a/src/plugins/workflow/index.ts b/src/plugins/workflow/index.ts index 8e8799a..317ca5b 100644 --- a/src/plugins/workflow/index.ts +++ b/src/plugins/workflow/index.ts @@ -7,6 +7,8 @@ import { EventType, MessageType } from "mirai-ts"; import Bot from "src/bot"; import fs from "fs"; import { parse } from "../../utils/config"; +import { exec } from "shelljs"; +import schedule from "node-schedule"; interface step { name?: string; @@ -28,9 +30,20 @@ type MessageAndEventType = | EventType.EventType | MessageType.ChatMessageType; +/** + * 定时格式 + */ +interface Schedule { + cron: string; +} + +interface On { + schedule: [Schedule]; +} + interface WorkflowConfig { name: string; - on: MessageAndEventType | MessageAndEventType[]; + on: On | MessageAndEventType | MessageAndEventType[]; jobs: Jobs; } @@ -39,14 +52,26 @@ interface WorkflowConfig { */ function createWorkflow(ctx: Bot, workflow: WorkflowConfig) { const mirai = ctx.mirai; + if (!workflow.on) return; + if (Array.isArray(workflow.on)) { workflow.on.forEach((on) => { trigger(on); }); - } else { + } else if (typeof workflow.on === "string") { trigger(workflow.on); + } else if ((workflow.on as On).schedule) { + (workflow.on as On).schedule.forEach((singleSchedule) => { + schedule.scheduleJob(singleSchedule.cron, () => { + doJobs(workflow.jobs); + }); + }); } + /** + * 触发 + * @param type + */ function trigger(type: MessageAndEventType) { mirai.on(type, (msg) => { Object.keys(workflow.jobs).forEach((name) => { @@ -59,6 +84,20 @@ function createWorkflow(ctx: Bot, workflow: WorkflowConfig) { }); }); } + + /** + * 运行 jobs 中终端命令 + */ + function doJobs(jobs: Jobs) { + Object.keys(jobs).forEach((name) => { + const job = jobs[name]; + job.steps.forEach((step) => { + if (step.run) { + exec(step.run); + } + }); + }); + } } export default function workflow(ctx: Bot) { diff --git a/src/plugins/workflow/package.json b/src/plugins/workflow/package.json new file mode 100644 index 0000000..4f9772b --- /dev/null +++ b/src/plugins/workflow/package.json @@ -0,0 +1,11 @@ +{ + "name": "workflow", + "private": "true", + "version": "0.0.1", + "description": "工作流", + "author": { + "name": "YunYouJun", + "url": "https://www.yunyoujun.cn", + "email": "me@yunyoujun.cn" + } +} diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 7ec433d..f25aaa9 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -19,7 +19,7 @@ export function statement(ctx: Bot) { chalk.cyan("el-bot") + " 是一个非盈利的开源项目,仅供交流学习使用。" ); ctx.logger.warning( - "请勿用于商业或非法用途,非法使用而与腾讯公司产生的一切纠纷均与原作者无关。" + "请勿用于商业或非法用途,因使用而与腾讯公司产生的一切纠纷均与原作者无关。" ); console.log("-----------------------------------------------"); }