Skip to content

Commit

Permalink
细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed Apr 11, 2024
1 parent c2af249 commit aa3b931
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
54 changes: 29 additions & 25 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export default class Yunzai extends EventEmitter {
if (!i.sendForwardMsg)
i.sendForwardMsg = msg => this.sendForwardMsg(msg => i.sendMsg(msg), msg)
if (!i.getInfo)
i.getInfo = () => i
i.getInfo = () => i.info || i
}
}

Expand Down Expand Up @@ -541,7 +541,9 @@ export default class Yunzai extends EventEmitter {

return new Promise(resolve =>
this.once(`connect.${bot_id}`, data =>
resolve(data.bot.pickFriend(user_id).sendMsg(msg))))
resolve(data.bot.pickFriend(user_id).sendMsg(msg))
)
)
} catch (err) {
this.makeLog("error", [`发送好友消息错误:[${user_id}]`, err], bot_id)
}
Expand All @@ -558,48 +560,50 @@ export default class Yunzai extends EventEmitter {

return new Promise(resolve =>
this.once(`connect.${bot_id}`, data =>
resolve(data.bot.pickGroup(group_id).sendMsg(msg))))
resolve(data.bot.pickGroup(group_id).sendMsg(msg))
)
)
} catch (err) {
this.makeLog("error", [`发送群消息错误:[${group_id}]`, err], bot_id)
}
return false
}

async getTextMsg(fnc = () => true) {
getTextMsg(fnc = () => true) {
if (typeof fnc != "function") {
const { self_id, user_id } = fnc
fnc = data => data.self_id == self_id && data.user_id == user_id
}

while (true) { try {
const msg = await new Promise(resolve => {
this.once("message", data => {
if (data.message && fnc(data)) {
let msg = ""
for (const i of data.message)
if (i.type == "text" && i.text)
msg += i.text.trim()
resolve(msg)
} else {
resolve(false)
}
})
})
if (msg) return msg
} catch (err) {
this.makeLog("error", err)
}}
return new Promise(resolve => {
const listener = data => { try {
if (!fnc(data)) return

let msg = ""
for (const i of data.message)
if (i.type == "text" && i.text)
msg += i.text.trim()
if (!msg) return

resolve(msg)
this.off("message", listener)
} catch (err) {
this.makeLog("error", err, data.self_id)
}}
this.on("message", listener)
})
}

getMasterMsg() {
return this.getTextMsg(data =>
cfg.master[data.self_id]?.includes(String(data.user_id)))
return this.getTextMsg(data => cfg.master[data.self_id]?.includes(String(data.user_id)))
}

sendMasterMsg(msg) {
const ret = []
for (const bot_id in cfg.master)
for (const user_id of cfg.master[bot_id])
this.sendFriendMsg(bot_id, user_id, msg)
ret.push(this.sendFriendMsg(bot_id, user_id, msg))
return Promise.allSettled(ret)
}

makeForwardMsg(msg) { return { type: "node", data: msg } }
Expand Down
7 changes: 7 additions & 0 deletions lib/plugins/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Bot.adapter.push(new class stdinAdapter {
}

message(msg) {
fs.appendFile(`${this.path}history`, `${Date.now()/1000}:${msg}\n`, "utf-8")
const data = {
bot: Bot[this.id],
self_id: this.id,
Expand Down Expand Up @@ -138,6 +139,12 @@ Bot.adapter.push(new class stdinAdapter {
}
Bot[this.id].gml.set(this.id, Bot[this.id].fl)

try {
Bot[this.id].sdk.history = (await fs.readFile(`${this.path}history`, "utf-8")).split("\n").slice(-Bot[this.id].sdk.historySize-1, -1).map(i => i.replace(/^[\d.]+:/, "")).reverse()
} catch (err) {
Bot.makeLog("trace", err, this.id)
}

Bot.makeLog("mark", `${this.name}(${this.id}) 已连接`, this.id)
Bot.em(`connect.${this.id}`, { self_id: this.id })
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"eslint": "^9.0.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-n": "^17.2.0",
"eslint-plugin-promise": "^6.1.1"
},
"imports": {
Expand Down
4 changes: 2 additions & 2 deletions plugins/adapter/OPQBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Bot.adapter.push(new class OPQBotAdapter {
return {
...i,
sendMsg: msg => this.sendFriendMsg(i, msg),
getAvatarUrl: () => `https://q1.qlogo.cn/g?b=qq&s=0&nk=${user_id}`,
getAvatarUrl: () => `https://q.qlogo.cn/g?b=qq&s=0&nk=${user_id}`,
}
}

Expand Down Expand Up @@ -267,7 +267,7 @@ Bot.adapter.push(new class OPQBotAdapter {
uin: id,
info: { id },
get nickname() { return this.info.nickname },
get avatar() { return `https://q1.qlogo.cn/g?b=qq&s=0&nk=${this.uin}` },
get avatar() { return `https://q.qlogo.cn/g?b=qq&s=0&nk=${this.uin}` },

version: {
id: this.id,
Expand Down
6 changes: 3 additions & 3 deletions plugins/adapter/OneBotv11.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ Bot.adapter.push(new class OneBotv11Adapter {
sendForwardMsg: msg => this.sendFriendForwardMsg(i, msg),
sendFile: (file, name) => this.sendFriendFile(i, file, name),
getInfo: () => this.getFriendInfo(i),
getAvatarUrl: () => i.avatar || `https://q1.qlogo.cn/g?b=qq&s=0&nk=${user_id}`,
getAvatarUrl: () => i.avatar || `https://q.qlogo.cn/g?b=qq&s=0&nk=${user_id}`,
thumbUp: times => this.sendLike(i, times),
}
}
Expand Down Expand Up @@ -607,7 +607,7 @@ Bot.adapter.push(new class OneBotv11Adapter {
...this.pickFriend(i, user_id),
...i,
getInfo: () => this.getMemberInfo(i),
getAvatarUrl: () => i.avatar || `https://q1.qlogo.cn/g?b=qq&s=0&nk=${user_id}`,
getAvatarUrl: () => i.avatar || `https://q.qlogo.cn/g?b=qq&s=0&nk=${user_id}`,
poke: () => this.sendGroupMsg(i, { type: "poke", qq: user_id }),
mute: duration => this.setGroupBan(i, i.user_id, duration),
kick: reject_add_request => this.setGroupKick(i, i.user_id, reject_add_request),
Expand Down Expand Up @@ -700,7 +700,7 @@ Bot.adapter.push(new class OneBotv11Adapter {
info: {},
get uin() { return this.info.user_id },
get nickname() { return this.info.nickname },
get avatar() { return `https://q1.qlogo.cn/g?b=qq&s=0&nk=${this.uin}` },
get avatar() { return `https://q.qlogo.cn/g?b=qq&s=0&nk=${this.uin}` },

setProfile: profile => this.setProfile(data, profile),
setNickname: nickname => this.setProfile(data, { nickname }),
Expand Down

0 comments on commit aa3b931

Please sign in to comment.