Skip to content

Commit

Permalink
细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed Jun 3, 2024
1 parent 4c2f82b commit 5cbff35
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 91 deletions.
110 changes: 55 additions & 55 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ export default class Yunzai extends EventEmitter {
return map
}

pickFriend(user_id) {
pickFriend(user_id, strict) {
user_id = Number(user_id) || user_id
let user = this.fl.get(user_id)
if (!user) for (const [id, ml] of this.gml) {
Expand All @@ -599,15 +599,17 @@ export default class Yunzai extends EventEmitter {
}
}
if (user) return this.bots[user.bot_id].pickFriend(user_id)
if (strict) return false
this.makeLog("debug", ["因不存在用户", user_id, "而随机选择Bot", this.uin.toJSON()])
return this.bots[this.uin].pickFriend(user_id)
}
get pickUser() { return this.pickFriend }

pickGroup(group_id) {
pickGroup(group_id, strict) {
group_id = Number(group_id) || group_id
const group = this.gl.get(group_id)
if (group) return this.bots[group.bot_id].pickGroup(group_id)
if (strict) return false
this.makeLog("debug", ["因不存在群", group_id, "而随机选择Bot", this.uin.toJSON()])
return this.bots[this.uin].pickGroup(group_id)
}
Expand All @@ -616,55 +618,55 @@ export default class Yunzai extends EventEmitter {
return this.pickGroup(group_id).pickMember(user_id)
}

sendFriendMsg(bot_id, user_id, msg) {
try {
if (!bot_id)
return this.pickFriend(user_id).sendMsg(msg)
sendFriendMsg(bot_id, user_id, ...args) { try {
if (!bot_id)
return this.pickFriend(user_id).sendMsg(...args)

if (this.uin.includes(bot_id) && this.bots[bot_id])
return this.bots[bot_id].pickFriend(user_id).sendMsg(msg)
if (this.uin.includes(bot_id) && this.bots[bot_id])
return this.bots[bot_id].pickFriend(user_id).sendMsg(...args)

return new Promise((resolve, reject) => {
const listener = data => {
resolve(data.bot.pickFriend(user_id).sendMsg(msg))
clearTimeout(timeout)
}
const timeout = setTimeout(() => {
reject(Object.assign(Error("等待 Bot 上线超时"), { bot_id, user_id, msg }))
this.off(`connect.${bot_id}`, listener)
}, 300000)
this.once(`connect.${bot_id}`, listener)
})
} catch (err) {
this.makeLog("error", ["发送好友消息错误", msg, err], `${bot_id} => ${user_id}`)
}
return false
}
if (this.pickFriend(bot_id, true))
return this.pickFriend(bot_id).sendMsg(user_id, ...args)

sendGroupMsg(bot_id, group_id, msg) {
try {
if (!bot_id)
return this.pickGroup(group_id).sendMsg(msg)
return new Promise((resolve, reject) => {
const listener = data => {
resolve(data.bot.pickFriend(user_id).sendMsg(...args))
clearTimeout(timeout)
}
const timeout = setTimeout(() => {
reject(Object.assign(Error("等待 Bot 上线超时"), { bot_id, user_id, args }))
this.off(`connect.${bot_id}`, listener)
}, 300000)
this.once(`connect.${bot_id}`, listener)
})
} catch (err) {
this.makeLog("error", ["发送好友消息错误", args, err], `${bot_id} => ${user_id}`)
}}

if (this.uin.includes(bot_id) && this.bots[bot_id])
return this.bots[bot_id].pickGroup(group_id).sendMsg(msg)
sendGroupMsg(bot_id, group_id, ...args) { try {
if (!bot_id)
return this.pickGroup(group_id).sendMsg(...args)

return new Promise((resolve, reject) => {
const listener = data => {
resolve(data.bot.pickGroup(group_id).sendMsg(msg))
clearTimeout(timeout)
}
const timeout = setTimeout(() => {
reject(Object.assign(Error("等待 Bot 上线超时"), { bot_id, group_id, msg }))
this.off(`connect.${bot_id}`, listener)
}, 300000)
this.once(`connect.${bot_id}`, listener)
})
} catch (err) {
this.makeLog("error", ["发送群消息错误", msg, err], `${bot_id} => ${group_id}`)
}
return false
}
if (this.uin.includes(bot_id) && this.bots[bot_id])
return this.bots[bot_id].pickGroup(group_id).sendMsg(...args)

if (this.pickGroup(bot_id, true))
return this.pickGroup(bot_id).sendMsg(group_id, ...args)

return new Promise((resolve, reject) => {
const listener = data => {
resolve(data.bot.pickGroup(group_id).sendMsg(...args))
clearTimeout(timeout)
}
const timeout = setTimeout(() => {
reject(Object.assign(Error("等待 Bot 上线超时"), { bot_id, group_id, args }))
this.off(`connect.${bot_id}`, listener)
}, 300000)
this.once(`connect.${bot_id}`, listener)
})
} catch (err) {
this.makeLog("error", ["发送群消息错误", args, err], `${bot_id} => ${group_id}`)
}}

getTextMsg(fnc = () => true) {
if (typeof fnc !== "function") {
Expand Down Expand Up @@ -697,15 +699,13 @@ export default class Yunzai extends EventEmitter {

async sendMasterMsg(msg, bot_array = Object.keys(cfg.master), sleep = 5000) {
const ret = {}
const promises = []
for (const bot_id of Array.isArray(bot_array) ? bot_array : [bot_array])
promises.push((async () => {
ret[bot_id] = {}
for (const user_id of cfg.master[bot_id] || []) {
ret[bot_id][user_id] = this.sendFriendMsg(bot_id, user_id, msg)
if (sleep) await this.sleep(sleep, ret[bot_id][user_id])
}})())
await Promise.allSettled(promises)
await Promise.allSettled((Array.isArray(bot_array) ? bot_array : [bot_array]).map(async bot_id => {
ret[bot_id] = {}
for (const user_id of cfg.master[bot_id] || []) {
ret[bot_id][user_id] = this.sendFriendMsg(bot_id, user_id, msg)
if (sleep) await this.sleep(sleep, ret[bot_id][user_id])
}
}))
return ret
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"strip-ansi": "^7.1.0",
"ulid": "^2.3.0",
"ws": "^8.17.0",
"yaml": "^2.4.2"
"yaml": "^2.4.3"
},
"devDependencies": {
"eslint": "^8.57.0",
Expand Down
10 changes: 5 additions & 5 deletions plugins/adapter/ComWeChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
msg = [msg]
const msgs = []
for (let i of msg) {
if (typeof i != "object")
if (typeof i !== "object")
i = { type: "text", data: { text: i }}
else if (!i.data)
i = { type: i.type, data: { ...i, type: undefined }}
Expand All @@ -74,7 +74,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
i.type = "file"
break
case "at":
if (i.data.qq == "all")
if (i.data.qq === "all")
i = { type: "mention_all", data: {}}
else
i = { type: "mention", data: { user_id: i.data.qq }}
Expand All @@ -89,7 +89,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
i = i.data
break
default:
i = { type: "text", data: { text: JSON.stringify(i) }}
i = { type: "text", data: { text: Bot.String(i) }}
}
msgs.push(i)
}
Expand Down Expand Up @@ -121,7 +121,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
for (const i of (await data.bot.sendApi("get_friend_list")).data)
array.push({
...i,
nickname: i.user_remark == "null" ? i.user_displayname || i.user_name : i.user_remark,
nickname: i.user_remark === "null" ? i.user_displayname || i.user_name : i.user_remark,
})
return array
}
Expand Down Expand Up @@ -442,7 +442,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
}

if (data.type) {
if (data.type != "meta" && !Bot.uin.includes(data.self_id)) {
if (data.type !== "meta" && !Bot.uin.includes(data.self_id)) {
Bot.makeLog("warn", `找不到对应Bot,忽略消息:${logger.magenta(data.raw)}`, data.self_id)
return false
}
Expand Down
12 changes: 6 additions & 6 deletions plugins/adapter/GSUIDCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Bot.adapter.push(new class GSUIDCoreAdapter {
} else return false

if (button.permission) {
if (button.permission == "admin") {
if (button.permission === "admin") {
msg.permission = 1
} else {
msg.permission = 0
Expand Down Expand Up @@ -58,7 +58,7 @@ Bot.adapter.push(new class GSUIDCoreAdapter {
msg = [msg]
const msgs = []
for (let i of msg) {
if (typeof i != "object")
if (typeof i !== "object")
i = { type: "text", text: i }

if (i.file) {
Expand Down Expand Up @@ -104,7 +104,7 @@ Bot.adapter.push(new class GSUIDCoreAdapter {
i = i.data
break
default:
i = { type: "text", data: JSON.stringify(i) }
i = { type: "text", data: Bot.String(i) }
}
msgs.push(i)
}
Expand Down Expand Up @@ -263,15 +263,15 @@ Bot.adapter.push(new class GSUIDCoreAdapter {
break
case "node":
data.message.push({ type: "node", data: i.data })
data.raw_message += `[合并转发:${JSON.stringify(i.data)}]`
data.raw_message += `[合并转发:${Bot.String(i.data)}]`
break
default:
data.message.push(i)
data.raw_message += JSON.stringify(i)
data.raw_message += Bot.String(i)
}
}

if (raw.user_type == "direct") {
if (raw.user_type === "direct") {
data.message_type = "private"
Bot.makeLog("info", `好友消息:${data.raw_message}`, `${data.self_id} <= ${data.user_id}`)
} else {
Expand Down
4 changes: 2 additions & 2 deletions plugins/adapter/OPQBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Bot.adapter.push(new class OPQBotAdapter {
}

for (let i of msg) {
if (typeof i != "object")
if (typeof i !== "object")
i = { type: "text", text: i }

switch (i.type) {
Expand Down Expand Up @@ -88,7 +88,7 @@ Bot.adapter.push(new class OPQBotAdapter {
message[i] = i.data[i]
continue
default:
message.Content += JSON.stringify(i)
message.Content += Bot.String(i)
}
}

Expand Down
Loading

0 comments on commit 5cbff35

Please sign in to comment.