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