细节优化,更新 ComWeChat v0.0.8
This commit is contained in:
parent
15444b93e9
commit
cef97bcc0c
|
@ -22,7 +22,7 @@ export default class ComWeChatAdapter {
|
|||
return this.toStr(msg).replace(/(base64:\/\/|"type":"data","data":").*?(,|]|")/g, "$1...$2")
|
||||
}
|
||||
|
||||
sendApi(ws, action, params) {
|
||||
sendApi(ws, action, params = {}) {
|
||||
const echo = randomUUID()
|
||||
const msg = JSON.stringify({ action, params, echo })
|
||||
logger.debug(`发送 API 请求:${logger.cyan(this.makeLog(msg))}`)
|
||||
|
@ -32,7 +32,7 @@ export default class ComWeChatAdapter {
|
|||
resolve({ ...data, ...data.data })))
|
||||
}
|
||||
|
||||
uploadFile(data, file, name) {
|
||||
uploadFile(data, file, name = randomUUID()) {
|
||||
const opts = { name }
|
||||
|
||||
if (file.match(/^https?:\/\//)) {
|
||||
|
@ -62,7 +62,7 @@ export default class ComWeChatAdapter {
|
|||
else if (!i.data)
|
||||
i = { type: i.type, data: { ...i, type: undefined }}
|
||||
if (i.data.file)
|
||||
i.data = { file_id: (await this.uploadFile(data, i.data.file, randomUUID())).file_id }
|
||||
i.data = { file_id: (await this.uploadFile(data, i.data.file)).file_id }
|
||||
switch (i.type) {
|
||||
case "at":
|
||||
if (i.data.qq == "all")
|
||||
|
@ -107,13 +107,12 @@ export default class ComWeChatAdapter {
|
|||
const messages = []
|
||||
for (const i of msg)
|
||||
messages.push(await send(i.message))
|
||||
messages.data = "消息"
|
||||
return messages
|
||||
}
|
||||
|
||||
async getFriendArray(data) {
|
||||
const array = []
|
||||
for (const i of (await data.sendApi("get_friend_list", {})).data.filter(item => !item.user_id?.endsWith("@chatroom")))
|
||||
for (const i of (await data.sendApi("get_friend_list")).data)
|
||||
array.push({
|
||||
...i,
|
||||
nickname: i.user_remark == "null" ? i.user_displayname || i.user_name : i.user_remark,
|
||||
|
@ -142,13 +141,7 @@ export default class ComWeChatAdapter {
|
|||
}
|
||||
|
||||
async getGroupArray(data) {
|
||||
const array = (await data.sendApi("get_group_list", {})).data
|
||||
for (const i of (await data.sendApi("get_friend_list", {})).data.filter(item => item.user_id?.endsWith("@chatroom")))
|
||||
array.push({
|
||||
group_id: i.user_id,
|
||||
group_name: i.user_remark == "null" ? i.user_displayname || i.user_name : i.user_remark,
|
||||
})
|
||||
return array
|
||||
return (await data.sendApi("get_group_list")).data
|
||||
}
|
||||
|
||||
async getGroupList(data) {
|
||||
|
@ -244,14 +237,12 @@ export default class ComWeChatAdapter {
|
|||
}
|
||||
|
||||
async connect(data) {
|
||||
const status = await data.sendApi("get_status", {})
|
||||
for (const bot of status.bots)
|
||||
for (const bot of data.status.bots)
|
||||
data.self_id = bot.self.user_id
|
||||
|
||||
Bot[data.self_id] = {
|
||||
sendApi: data.sendApi,
|
||||
version: data.version,
|
||||
stat: { start_time: data.time },
|
||||
stat: { ...data.status, start_time: data.time },
|
||||
|
||||
pickFriend: user_id => this.pickFriend(data, user_id),
|
||||
|
||||
|
@ -268,11 +259,13 @@ export default class ComWeChatAdapter {
|
|||
}
|
||||
Bot[data.self_id].pickUser = Bot[data.self_id].pickFriend
|
||||
|
||||
Bot[data.self_id].info = (await data.sendApi("get_self_info", {})).data
|
||||
Bot[data.self_id].info = (await data.sendApi("get_self_info")).data
|
||||
Bot[data.self_id].uin = Bot[data.self_id].info.user_id
|
||||
Bot[data.self_id].nickname = Bot[data.self_id].info.user_name
|
||||
Bot[data.self_id].avatar = Bot[data.self_id].info["wx.avatar"]
|
||||
|
||||
Bot[data.self_id].version = (await data.sendApi("get_version")).data
|
||||
|
||||
Bot[data.self_id].fl = await this.getFriendMap(data)
|
||||
Bot[data.self_id].gl = await this.getGroupMap(data)
|
||||
|
||||
|
@ -331,7 +324,11 @@ export default class ComWeChatAdapter {
|
|||
|
||||
makeMeta(data) {
|
||||
switch (data.detail_type) {
|
||||
case "heartbeat":
|
||||
break
|
||||
case "connect":
|
||||
break
|
||||
case "status_update":
|
||||
this.connect(data)
|
||||
break
|
||||
default:
|
||||
|
@ -346,8 +343,6 @@ export default class ComWeChatAdapter {
|
|||
return logger.error(err)
|
||||
}
|
||||
|
||||
if (data.detail_type == "heartbeat") return
|
||||
|
||||
data.sendApi = (action, params) => this.sendApi(ws, action, params)
|
||||
if (data.self?.user_id) {
|
||||
data.self_id = data.self.user_id
|
||||
|
@ -358,6 +353,9 @@ export default class ComWeChatAdapter {
|
|||
|
||||
if (data.type) {
|
||||
switch (data.type) {
|
||||
case "meta":
|
||||
this.makeMeta(data)
|
||||
break
|
||||
case "message":
|
||||
this.makeMessage(data)
|
||||
break
|
||||
|
@ -369,9 +367,6 @@ export default class ComWeChatAdapter {
|
|||
this.makeRequest(data)
|
||||
break
|
||||
*/
|
||||
case "meta":
|
||||
this.makeMeta(data)
|
||||
break
|
||||
default:
|
||||
logger.warn(`${logger.blue(`[${data.self_id}]`)} 未知消息:${logger.red(JSON.stringify(data))}`)
|
||||
}
|
||||
|
|
|
@ -115,7 +115,6 @@ export default class gocqhttpAdapter {
|
|||
user_id: data.user_id,
|
||||
messages: this.makeForwardMsg(msg),
|
||||
})
|
||||
msg.data = "好友转发消息"
|
||||
return msg
|
||||
}
|
||||
|
||||
|
@ -125,7 +124,6 @@ export default class gocqhttpAdapter {
|
|||
group_id: data.group_id,
|
||||
messages: this.makeForwardMsg(msg),
|
||||
})
|
||||
msg.data = "群转发消息"
|
||||
return msg
|
||||
}
|
||||
|
||||
|
@ -133,7 +131,6 @@ export default class gocqhttpAdapter {
|
|||
const messages = []
|
||||
for (const i of msg)
|
||||
messages.push(await this.sendGuildMsg(data, i.message))
|
||||
messages.data = "频道消息"
|
||||
return messages
|
||||
}
|
||||
|
||||
|
|
|
@ -65,7 +65,6 @@ export default class stdinAdapter {
|
|||
const messages = []
|
||||
for (const i of msg)
|
||||
messages.push(this.sendMsg(i.message))
|
||||
messages.data = "系统消息"
|
||||
return messages
|
||||
}
|
||||
|
||||
|
@ -98,14 +97,14 @@ export default class stdinAdapter {
|
|||
message(msg) {
|
||||
const data = {
|
||||
bot: Bot.stdin,
|
||||
self_id: Bot.stdin.uin,
|
||||
user_id: Bot.stdin.uin,
|
||||
self_id: "stdin",
|
||||
user_id: "stdin",
|
||||
post_type: "message",
|
||||
message_type: "private",
|
||||
sender: { nickname: Bot.stdin.nickname },
|
||||
sender: { nickname: "标准输入" },
|
||||
message: [{ type: "text", text: msg }],
|
||||
raw_message: msg,
|
||||
friend: Bot.stdin.pickFriend(),
|
||||
friend: this.pickFriend(),
|
||||
}
|
||||
logger.info(`${logger.blue(`[${data.self_id}]`)} 系统消息:[${data.sender.nickname}(${data.user_id})] ${data.raw_message}`)
|
||||
|
||||
|
@ -120,12 +119,19 @@ export default class stdinAdapter {
|
|||
stat: { start_time: Date.now()/1000 },
|
||||
version: { impl: "stdin" },
|
||||
pickFriend: () => this.pickFriend(),
|
||||
fl: new Map().set("stdin", {}),
|
||||
pickUser: () => this.pickFriend(),
|
||||
pickGroup: () => this.pickFriend(),
|
||||
pickMember: () => this.pickFriend(),
|
||||
|
||||
fl: new Map().set("stdin", {
|
||||
user_id: "stdin",
|
||||
nickname: "标准输入",
|
||||
}),
|
||||
gl: new Map().set("stdin", {
|
||||
group_id: "stdin",
|
||||
group_name: "标准输入",
|
||||
}),
|
||||
}
|
||||
Bot.stdin.pickUser = Bot.stdin.pickFriend
|
||||
Bot.stdin.pickGroup = Bot.stdin.pickFriend
|
||||
Bot.stdin.pickMember = Bot.stdin.pickFriend
|
||||
Bot.stdin.gl = Bot.stdin.fl
|
||||
|
||||
if (Array.isArray(Bot.uin)) {
|
||||
if (!Bot.uin.includes("stdin"))
|
||||
|
@ -134,7 +140,7 @@ export default class stdinAdapter {
|
|||
Bot.uin = ["stdin"]
|
||||
}
|
||||
|
||||
process.stdin.on("data", data => this.message(data.toString().trim()))
|
||||
process.stdin.on("data", data => this.message(data.toString()))
|
||||
|
||||
logger.mark(`${logger.blue(`[stdin]`)} 标准输入 已连接`)
|
||||
Bot.emit(`connect.stdin`, Bot.stdin)
|
||||
|
|
Loading…
Reference in New Issue