Skip to content

Commit

Permalink
新增 定时更新/重启/关机/开机,优化 原关机改为停止,现关机与开机配合使用
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed May 30, 2024
1 parent 9423a3e commit 6240306
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 96 deletions.
9 changes: 9 additions & 0 deletions config/default_config/bot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ port: 2536
update_time: 1440
# 自动重启时间
restart_time: 0
# 定时更新cron表达式
update_cron:
# 定时重启cron表达式
restart_cron:
# 定时关机cron表达式
stop_cron:
# 定时开机cron表达式
start_cron:

# 上线推送通知的冷却时间
online_msg_exp: 1440
# 文件保存时间
Expand Down
2 changes: 1 addition & 1 deletion lib/listener/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class EventListener {
* @param data.event 监听的事件
* @param data.once 是否只监听一次
*/
constructor (data) {
constructor(data) {
this.prefix = data.prefix || ""
this.event = data.event
this.once = data.once || false
Expand Down
2 changes: 1 addition & 1 deletion lib/listener/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ListenerLoader {
/**
* 监听事件加载
*/
async load () {
async load() {
Bot.makeLog("info", "-----------", "Listener")
Bot.makeLog("info", "加载监听事件中...", "Listener")
let eventCount = 0
Expand Down
108 changes: 54 additions & 54 deletions lib/plugins/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,27 @@ global.segment = segment
* 加载插件
*/
class PluginsLoader {
constructor() {
this.priority = []
this.handler = {}
this.task = []
this.dir = "plugins"

/** 命令冷却cd */
this.groupCD = {}
this.singleCD = {}

/** 插件监听 */
this.watcher = {}
this.eventMap = {
message: ["post_type", "message_type", "sub_type"],
notice: ["post_type", "notice_type", "sub_type"],
request: ["post_type", "request_type", "sub_type"],
}
priority = []
handler = {}
task = []
dir = "plugins"

/** 命令冷却cd */
groupCD = {}
singleCD = {}

/** 插件监听 */
watcher = {}
eventMap = {
message: ["post_type", "message_type", "sub_type"],
notice: ["post_type", "notice_type", "sub_type"],
request: ["post_type", "request_type", "sub_type"],
}

this.msgThrottle = {}
msgThrottle = {}

/** 星铁命令前缀 */
this.srReg = /^#?(\*|星铁|星轨|穹轨|星穹|崩铁|星穹铁道|崩坏星穹铁道|铁道)+/
}
/** 星铁命令前缀 */
srReg = /^#?(\*|星铁|星轨|穹轨|星穹|崩铁|星穹铁道|崩坏星穹铁道|铁道)+/

async getPlugins() {
const files = await fs.readdir(this.dir, { withFileTypes: true })
Expand Down Expand Up @@ -112,7 +110,7 @@ class PluginsLoader {
pluginArray.push(this.loadPlugin(file, p))
)
for (const i of await Promise.allSettled(pluginArray))
if (i?.status && i.status != "fulfilled") {
if (i?.status && i.status !== "fulfilled") {
Bot.makeLog("error", [`插件加载错误 ${logger.red(file.name)}`, i], "Plugin")
}
} catch (error) {
Expand All @@ -130,9 +128,9 @@ class PluginsLoader {
const plugin = new p
Bot.makeLog("debug", `加载插件 [${file.name}][${plugin.name}]`, "Plugin")
/** 执行初始化,返回 return 则跳过加载 */
if (plugin.init && await plugin.init() == "return") return
if (plugin.init && await plugin.init() === "return") return
/** 初始化定时任务 */
this.collectTask(plugin.task)
this.collectTask(plugin.task, plugin.name)
this.priority.push({
class: p,
key: file.name,
Expand Down Expand Up @@ -203,7 +201,7 @@ class PluginsLoader {
let ret
for (const fnc in context)
ret ||= await plugin[fnc](context[fnc])
if (ret == "continue") continue
if (ret === "continue") continue
return
}
}
Expand All @@ -230,7 +228,7 @@ class PluginsLoader {
for (const plugin of priority)
if (plugin.accept) {
const res = await plugin.accept(e)
if (res == "return") return
if (res === "return") return
if (res) break
}

Expand Down Expand Up @@ -272,19 +270,19 @@ class PluginsLoader {
const eventMap = this.eventMap[e.post_type] || []
const newEvent = []
for (const i in event) {
if (event[i] == "*")
if (event[i] === "*")
newEvent.push(event[i])
else
newEvent.push(e[eventMap[i]])
}
return v.event == newEvent.join(".")
return v.event === newEvent.join(".")
}

/** 判断权限 */
filtPermission(e, v) {
if (v.permission == "all" || !v.permission) return true
if (v.permission === "all" || !v.permission) return true

if (v.permission == "master") {
if (v.permission === "master") {
if (e.isMaster) {
return true
} else {
Expand All @@ -294,13 +292,13 @@ class PluginsLoader {
}

if (e.isGroup) {
if (v.permission == "owner") {
if (v.permission === "owner") {
if (!e.member.is_owner) {
e.reply("暂无权限,只有群主才能操作")
return false
}
}
if (v.permission == "admin") {
if (v.permission === "admin") {
if (!e.member.is_admin) {
e.reply("暂无权限,只有管理员才能操作")
return false
Expand Down Expand Up @@ -346,7 +344,7 @@ class PluginsLoader {
e.img = [i.url]
break
case "at":
if (i.qq == e.self_id)
if (i.qq === e.self_id)
e.atBot = true
else
e.at = i.qq
Expand All @@ -363,14 +361,14 @@ class PluginsLoader {
break
case "xml":
case "json":
e.msg = (e.msg || "") + (typeof i.data == "string" ? i.data : JSON.stringify(i.data))
e.msg = (e.msg || "") + (typeof i.data === "string" ? i.data : JSON.stringify(i.data))
break
}
}

e.logText = ""

if (e.message_type == "private" || e.notice_type == "friend") {
if (e.message_type === "private" || e.notice_type === "friend") {
e.isPrivate = true

if (e.sender) {
Expand All @@ -384,7 +382,7 @@ class PluginsLoader {
}

e.logText = `[${e.sender?.nickname ? `${e.sender.nickname}(${e.user_id})` : e.user_id}]`
} else if (e.message_type == "group" || e.notice_type == "group") {
} else if (e.message_type === "group" || e.notice_type === "group") {
e.isGroup = true

if (e.sender) {
Expand Down Expand Up @@ -523,26 +521,28 @@ class PluginsLoader {
}

/** 收集定时任务 */
collectTask(task) {
collectTask(task, name) {
for (const i of Array.isArray(task) ? task : [task])
if (i.cron && i.name)
if (i.cron && i.fnc) {
i.name ??= name
this.task.push(i)
}
}

/** 创建定时任务 */
createTask() {
for (const i of this.task)
i.job = schedule.scheduleJob(i.cron, async () => {
try {
if (i.log == true)
Bot.makeLog("mark", `开始定时任务 ${i.name}`, "Task")
await i.fnc()
if (i.log == true)
Bot.makeLog("mark", `定时任务完成 ${i.name}`, "Task")
} catch (err) {
Bot.makeLog("error", [`定时任务报错 ${i.name}`, err], "Task")
}
})
for (const i of this.task) {
const name = `${logger.blue(`[${i.name}(${i.cron})]`)}`
Bot.makeLog("debug", `加载定时任务 ${name}`, "Task")
i.job = schedule.scheduleJob(i.cron, async () => { try {
const start_time = Date.now()
Bot.makeLog(i.log === false ? "debug" : "mark", `${name}${logger.yellow("[开始处理]")}`, false)
await i.fnc()
Bot.makeLog(i.log === false ? "debug" : "mark", `${name}${logger.green(`[完成${Bot.getTimeDiff(start_time)}]`)}`, false)
} catch (err) {
Bot.makeLog("error", [name, err], false)
}})
}
}

/** 检查命令冷却cd */
Expand Down Expand Up @@ -591,10 +591,10 @@ class PluginsLoader {
let groupCfg = cfg.getGroup(e.self_id, e.group_id)

/** 模式0,未开启前缀 */
if (groupCfg.onlyReplyAt == 0 || !groupCfg.botAlias) return true
if (groupCfg.onlyReplyAt === 0 || !groupCfg.botAlias) return true

/** 模式2,非主人开启 */
if (groupCfg.onlyReplyAt == 2 && e.isMaster) return true
if (groupCfg.onlyReplyAt === 2 && e.isMaster) return true

/** at机器人 */
if (e.atBot) return true
Expand Down Expand Up @@ -645,7 +645,7 @@ class PluginsLoader {
lodash.forEach(app, p => {
const plugin = new p
for (const i in this.priority)
if (this.priority[i].key == key && this.priority[i].name == plugin.name) {
if (this.priority[i].key === key && this.priority[i].name === plugin.name) {
this.priority[i].class = p
this.priority[i].priority = plugin.priority
}
Expand Down Expand Up @@ -676,7 +676,7 @@ class PluginsLoader {
Bot.makeLog("mark", `[卸载插件][${dirName}][${appName}]`, "Plugin")
/** 停止更新监听 */
this.watcher[`${dirName}.${appName}`].removeAllListeners("change")
this.priority = this.priority.filter(i => i.key != key)
this.priority = this.priority.filter(i => i.key !== key)
})
this.watcher[`${dirName}.${appName}`] = watcher
}
Expand Down
11 changes: 2 additions & 9 deletions lib/plugins/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class plugin {
namespace,
event = "message",
priority = 5000,
task = { fnc: "", cron: "" },
task = { name: "", fnc: "", cron: "" },
rule = []
}) {
/** 插件名称 */
Expand All @@ -48,14 +48,7 @@ export default class plugin {
/** 优先级 */
this.priority = priority
/** 定时任务,可以是数组 */
this.task = {
/** 任务名 */
name: "",
/** 任务方法名 */
fnc: task.fnc || "",
/** 任务cron表达式 */
cron: task.cron || ""
}
this.task = task
/** 命令规则 */
this.rule = rule

Expand Down
Loading

0 comments on commit 6240306

Please sign in to comment.