Skip to content

Commit

Permalink
细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed Aug 12, 2024
1 parent 17745fb commit 26ceabf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 111 deletions.
1 change: 1 addition & 0 deletions lib/plugins/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ class PluginsLoader {
let app = await import(`../../${this.dir}/${key}?${moment().format("x")}`)
if (app.apps) app = { ...app.apps }
lodash.forEach(app, p => {
if (!p?.prototype) return
const plugin = new p
if (plugin.rule) for (const i of plugin.rule)
if (!(i.reg instanceof RegExp))
Expand Down
11 changes: 6 additions & 5 deletions lib/tools/docker.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#TRSS Yunzai Docker 安装脚本 作者:时雨🌌星空
NAME=v1.0.0 VERSION=202406200
NAME=v1.0.0 VERSION=202408090
R="" G="" Y="" C="" B="" O=""
echo "$B———————————————————————————
$R TRSS$Y Yunzai$G Docker$C Script$O
Expand Down Expand Up @@ -55,10 +55,11 @@ $R! 下载失败,5秒后切换镜像源$O"
case "$N" in
1)DKURL="docker.m.daocloud.io";;
2)DKURL="mirror.ccs.tencentyun.com";;
3)DKURL="dockerhub.timeweb.cloud";;
4)DKURL="mirror.baidubce.com";;
5)DKURL="docker.nju.edu.cn";;
6)DKURL="dockerproxy.com";;
3)DKURL="dockerpull.com";;
4)DKURL="dockerhub.timeweb.cloud";;
5)DKURL="mirror.baidubce.com";;
6)DKURL="docker.nju.edu.cn";;
7)DKURL="dockerproxy.com";;
*)DKURL="docker.io";N=0
esac
done
Expand Down
169 changes: 63 additions & 106 deletions plugins/system/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "node:fs/promises"
import path from "node:path"
import lodash from "lodash"

let messageMap = {}
export const messageMap = {}

export class add extends plugin {
constructor() {
Expand Down Expand Up @@ -50,21 +50,18 @@ export class add extends plugin {
this.isGlobal = Boolean(this.e.msg.match(/^#全局/))
await this.getGroupId()

if (!this.group_id) {
await this.reply("请先在群内触发消息,确定添加的群")
return
}
if (!this.group_id)
return this.reply("请先在群内触发消息,确定添加的群")

await this.initMessageMap()

if (!this.checkAuth()) return false
/** 获取关键词 */
this.getKeyWord()
if (!this.e.keyWord) {
await this.reply("添加错误:没有关键词")
return
}
if (!this.keyWord)
return this.reply("添加错误:没有关键词")

this.e.keyWord = this.keyWord
this.e.message = []
this.setContext("addContext")

Expand All @@ -73,26 +70,18 @@ export class add extends plugin {

/** 获取群号 */
async getGroupId() {
/** 添加全局消息,存入到机器人文件中 */
if (this.isGlobal) {
this.group_id = "global"
return this.group_id
}

if (this.e.isGroup) {
this.group_id = this.e.group_id
redis.setEx(this.grpKey, 3600 * 24 * 30, String(this.group_id))
return this.group_id
}

// redis获取
let groupId = await redis.get(this.grpKey)
if (groupId) {
this.group_id = groupId
redis.setEx(this.grpKey, 2592000, String(this.group_id))
return this.group_id
}

return false
return this.group_id = await redis.get(this.grpKey)
}

checkAuth() {
Expand Down Expand Up @@ -125,8 +114,7 @@ export class add extends plugin {
/** 获取添加关键词 */
getKeyWord() {
this.e.isGlobal = Boolean(this.e.msg.match(/^#全局/))
this.keyWord = this.e.raw_message.replace(/#(全局)?(添加|删除)/, "").trim()
this.e.keyWord = this.trimAlias(this.keyWord)
this.keyWord = this.trimAlias(this.e.raw_message.replace(/#(全局)?(添加|删除)/, "").trim())
}

/** 过滤别名 */
Expand Down Expand Up @@ -162,10 +150,8 @@ export class add extends plugin {
}

this.finish("addContext")
if (!context.message?.length) {
this.reply("添加错误:没有添加内容")
return
}
if (!context.message?.length)
return this.reply("添加错误:没有添加内容")

if (!messageMap[this.group_id])
messageMap[this.group_id] = new Map()
Expand All @@ -177,17 +163,16 @@ export class add extends plugin {
else
message = [context.message]
messageMap[this.group_id].set(this.keyWord, message)

if (message.length > 1)
this.keyWord += String(message.length)
this.keyWord += `(${message.length})`

await this.saveJson()
return this.reply(`添加成功:${this.keyWord}`)
}

async saveJson() {
let obj = {}
for (let [k, v] of messageMap[this.group_id])
const obj = {}
for (const [k, v] of messageMap[this.group_id])
obj[k] = v

await fs.writeFile(`${this.path}${this.group_id}.json`, JSON.stringify(obj, "", "\t"))
Expand All @@ -205,6 +190,13 @@ export class add extends plugin {
return data.url
}

getKeyWordMsg(keyWord) {
return [
...messageMap[this.group_id].get(keyWord) || [],
...messageMap.global.get(keyWord) || [],
]
}

async getMessage() {
if (!this.e.raw_message) return false
this.isGlobal = false
Expand All @@ -216,33 +208,33 @@ export class add extends plugin {
await this.initGlobalMessageMap()

this.keyWord = this.trimAlias(this.e.raw_message.trim())
let keyWord = this.keyWord

let num = 0
if (isNaN(keyWord)) {
num = keyWord.charAt(keyWord.length-1)

if (!isNaN(num) && !messageMap[this.group_id].has(keyWord) && !messageMap.global.has(keyWord)) {
keyWord = lodash.trimEnd(keyWord, num).trim()
num--
let msg = this.getKeyWordMsg(this.keyWord)
if (!msg.length) {
const index = this.keyWord.match(/\d+$/)?.[0]
if (index) for (let i=0; i<index.length; i++) {
const keyWord = this.keyWord.slice(0, this.keyWord.length - index.length+i)
msg = this.getKeyWordMsg(keyWord)
if (msg.length) {
const n = index.slice(-i)
msg = msg[n-1]
if (msg) {
this.keyWord = `${keyWord}(${n})`
break
}
}
}
} else {
msg = msg[lodash.random(0, msg.length-1)]
}

let msg = [
...messageMap[this.group_id].get(keyWord) || [],
...messageMap.global.get(keyWord) || [],
]
if (lodash.isEmpty(msg)) return false

if (!msg[num])
num = lodash.random(0, msg.length-1)

msg = [...msg[num]]
msg = [...msg]
for (const i in msg)
if (msg[i].file && await Bot.fsStat(`${this.path}${msg[i].file}`))
msg[i] = { ...msg[i], file: `${this.path}${msg[i].file}` }

logger.mark(`[发送消息]${this.e.logText} ${this.keyWord}`)
logger.mark(`[发送消息]${this.e.logText}[${this.keyWord}]`)
const groupCfg = cfg.getGroup(this.e.self_id, this.group_id)
return this.reply(msg, Boolean(groupCfg.addReply), {
at: Boolean(groupCfg.addAt),
Expand Down Expand Up @@ -292,65 +284,32 @@ export class add extends plugin {
await this.initMessageMap()

this.getKeyWord()
if (!this.keyWord) {
await this.reply("删除错误:没有关键词")
return false
}

this.keyWord = this.trimAlias(this.keyWord)
let keyWord = this.keyWord

let num = false
let index = 0
if (isNaN(keyWord)) {
num = keyWord.charAt(keyWord.length-1)

if (!isNaN(num) && !messageMap[this.group_id].has(keyWord)) {
keyWord = lodash.trimEnd(keyWord, num).trim()
index = num-1
} else {
num = false
}
}

let arr = messageMap[this.group_id].get(keyWord)
if (!arr) {
// await this.reply(`暂无此消息:${keyWord}`)
return false
}
if (!this.keyWord)
return this.reply("删除错误:没有关键词")

let tmp = []
if (num) {
if (!arr[index]) {
// await this.reply(`暂无此消息:${keyWord}${num}`)
return false
}

tmp = arr[index]
arr.splice(index, 1)

if (arr.length <= 0) {
messageMap[this.group_id].delete(keyWord)
} else {
messageMap[this.group_id].set(keyWord, arr)
}
if (messageMap[this.group_id].has(this.keyWord)) {
messageMap[this.group_id].delete(this.keyWord)
await this.saveJson()
return this.reply(`删除成功:${this.keyWord}`)
} else {
if (this.e.msg.includes("删除全部")) {
tmp = arr
arr = []
} else {
tmp = arr.pop()
}

if (arr.length <= 0) {
messageMap[this.group_id].delete(keyWord)
} else {
messageMap[this.group_id].set(keyWord, arr)
const index = this.keyWord.match(/\d+$/)?.[0]
if (index) for (let i=0; i<index.length; i++) {
const keyWord = this.keyWord.slice(0, this.keyWord.length - index.length+i)
const msg = messageMap[this.group_id].get(keyWord)
console.log(keyWord,msg)
if (msg) {
const n = index.slice(-i)-1
if (msg[n]) {
msg.splice(n, 1)
if (!msg.length)
messageMap[this.group_id].delete(keyWord)
await this.saveJson()
return this.reply(`删除成功:${keyWord}(${n+1})`)
}
}
}
}

await this.saveJson()
return this.reply(`删除成功:${this.keyWord}`)
return this.reply("删除错误:没有添加此关键词")
}

async list() {
Expand All @@ -373,10 +332,8 @@ export class add extends plugin {

let list = messageMap[this.group_id]

if (lodash.isEmpty(list)) {
await this.reply("暂无消息")
return
}
if (lodash.isEmpty(list))
return this.reply("暂无消息")

let arr = []
if (type == "list")
Expand Down

0 comments on commit 26ceabf

Please sign in to comment.