细节优化

This commit is contained in:
🌌 2023-06-29 20:25:21 +08:00
parent a38dd325e0
commit 76cf749182
5 changed files with 40 additions and 12 deletions

View File

@ -1,7 +1,7 @@
# 日志等级:trace,debug,info,warn,fatal,mark,error,off # 日志等级:trace,debug,info,warn,fatal,mark,error,off
# mark时只显示执行命令不显示聊天记录 # mark时只显示执行命令不显示聊天记录
log_level: info log_level: info
# WebSocket 端口 # HTTP 端口
port: 2536 port: 2536
# chromium其他路径 # chromium其他路径

View File

@ -1,5 +1,7 @@
import fetch from "node-fetch"
import fs from "node:fs" import fs from "node:fs"
import path from "node:path" import path from "node:path"
import { fileTypeFromBuffer } from "file-type"
Bot.adapter.push(new class stdinAdapter { Bot.adapter.push(new class stdinAdapter {
constructor() { constructor() {
@ -18,6 +20,19 @@ Bot.adapter.push(new class stdinAdapter {
return file return file
} }
async fileType(data) {
const file = {}
try {
file.url = data.replace(/^base64:\/\/.*/, "base64://...")
file.buffer = await this.makeBuffer(data)
file.type = await fileTypeFromBuffer(file.buffer)
file.path = `${Bot[this.id].data_dir}${Date.now()}.${file.type.ext}`
} catch (err) {
logger.error(`文件类型检测错误:${logger.red(err)}`)
}
return file
}
async sendMsg(msg) { async sendMsg(msg) {
if (!Array.isArray(msg)) if (!Array.isArray(msg))
msg = [msg] msg = [msg]
@ -26,6 +41,11 @@ Bot.adapter.push(new class stdinAdapter {
i = { type: "text", data: { text: i }} i = { type: "text", data: { text: i }}
else if (!i.data) else if (!i.data)
i = { type: i.type, data: { ...i, type: undefined }} i = { type: i.type, data: { ...i, type: undefined }}
let file
if (i.data.file)
file = await this.fileType(i.data.file)
switch (i.type) { switch (i.type) {
case "text": case "text":
if (i.data.text.match("\n")) if (i.data.text.match("\n"))
@ -33,19 +53,16 @@ Bot.adapter.push(new class stdinAdapter {
logger.info(`${logger.blue(`[${this.id}]`)} 发送文本:${i.data.text}`) logger.info(`${logger.blue(`[${this.id}]`)} 发送文本:${i.data.text}`)
break break
case "image": case "image":
i.file = `${Bot[this.id].data_dir}${Date.now()}.png` logger.info(`${logger.blue(`[${this.id}]`)} 发送图片:${file.url}\n文件已保存到:${logger.cyan(file.path)}`)
logger.info(`${logger.blue(`[${this.id}]`)} 发送图片:${i.data.file.replace(/^base64:\/\/.*/, "base64://...")}\n文件已保存到:${logger.cyan(i.file)}`) fs.writeFileSync(file.path, file.buffer)
fs.writeFileSync(i.file, await this.makeBuffer(i.data.file))
break break
case "record": case "record":
i.file = `${Bot[this.id].data_dir}${Date.now()}.mp3` logger.info(`${logger.blue(`[${this.id}]`)} 发送音频:${file.url}\n文件已保存到:${logger.cyan(file.path)}`)
logger.info(`${logger.blue(`[${this.id}]`)} 发送音频:${i.data.file.replace(/^base64:\/\/.*/, "base64://...")}\n文件已保存到:${logger.cyan(i.file)}`) fs.writeFileSync(file.path, file.buffer)
fs.writeFileSync(i.file, await this.makeBuffer(i.data.file))
break break
case "video": case "video":
i.file = `${Bot[this.id].data_dir}${Date.now()}.mp4` logger.info(`${logger.blue(`[${this.id}]`)} 发送视频:${file.url}\n文件已保存到:${logger.cyan(file.path)}`)
logger.info(`${logger.blue(`[${this.id}]`)} 发送视频:${i.data.file.replace(/^base64:\/\/.*/, "base64://...")}\n文件已保存到:${logger.cyan(i.file)}`) fs.writeFileSync(file.path, file.buffer)
fs.writeFileSync(i.file, await this.makeBuffer(i.data.file))
break break
case "reply": case "reply":
break break

View File

@ -17,9 +17,10 @@
}, },
"dependencies": { "dependencies": {
"art-template": "^4.13.2", "art-template": "^4.13.2",
"chalk": "^5.2.0", "chalk": "^5.3.0",
"chokidar": "^3.5.3", "chokidar": "^3.5.3",
"express": "^4.18.2", "express": "^4.18.2",
"file-type": "^18.5.0",
"https-proxy-agent": "7.0.0", "https-proxy-agent": "7.0.0",
"image-size": "^1.0.2", "image-size": "^1.0.2",
"lodash": "^4.17.21", "lodash": "^4.17.21",

View File

@ -197,6 +197,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
} }
async sendFile(data, send, file, name = path.basename(file)) { async sendFile(data, send, file, name = path.basename(file)) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 发送文件:${name}(${file})`)
return send(segment.custom("file", { return send(segment.custom("file", {
file_id: (await this.uploadFile(data, file, name)).file_id file_id: (await this.uploadFile(data, file, name)).file_id
})) }))

View File

@ -291,6 +291,7 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
setGroupName(data, group_name) { setGroupName(data, group_name) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 设置群名:[${data.group_id}] ${group_name}`)
return data.sendApi("set_group_name", { return data.sendApi("set_group_name", {
group_id: data.group_id, group_id: data.group_id,
group_name, group_name,
@ -298,13 +299,15 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
setGroupAvatar(data, file) { setGroupAvatar(data, file) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 设置群头像:[${data.group_id}] ${file}`)
return data.sendApi("set_group_portrait", { return data.sendApi("set_group_portrait", {
group_id: data.group_id, group_id: data.group_id,
file: segment.image(file).data.file, file: segment.image(file).file,
}) })
} }
setGroupAdmin(data, user_id, enable) { setGroupAdmin(data, user_id, enable) {
logger.info(`${logger.blue(`[${data.self_id}]`)} ${enable ? "设置" : "取消"}群管理员:[${data.group_id}] ${user_id}`)
return data.sendApi("set_group_admin", { return data.sendApi("set_group_admin", {
group_id: data.group_id, group_id: data.group_id,
user_id, user_id,
@ -313,6 +316,7 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
setGroupCard(data, user_id, card) { setGroupCard(data, user_id, card) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 设置群名片:[${data.group_id}] ${user_id} ${card}`)
return data.sendApi("set_group_card", { return data.sendApi("set_group_card", {
group_id: data.group_id, group_id: data.group_id,
user_id, user_id,
@ -321,6 +325,7 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
setGroupTitle(data, user_id, special_title, duration) { setGroupTitle(data, user_id, special_title, duration) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 设置群头衔:[${data.group_id}] ${user_id} ${special_title} ${duration}`)
return data.sendApi("set_group_special_title", { return data.sendApi("set_group_special_title", {
group_id: data.group_id, group_id: data.group_id,
user_id, user_id,
@ -346,6 +351,7 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
async sendFriendFile(data, file, name) { async sendFriendFile(data, file, name) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 发送好友文件:[${data.user_id}] ${name}(${file})`)
return data.sendApi("upload_private_file", { return data.sendApi("upload_private_file", {
user_id: data.user_id, user_id: data.user_id,
...await this.makeFile(data, file, name), ...await this.makeFile(data, file, name),
@ -353,6 +359,7 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
async sendGroupFile(data, file, folder, name) { async sendGroupFile(data, file, folder, name) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 发送群文件:[${data.group_id}] ${folder||""}/${name}(${file})`)
return data.sendApi("upload_group_file", { return data.sendApi("upload_group_file", {
group_id: data.group_id, group_id: data.group_id,
folder, folder,
@ -361,6 +368,7 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
deleteGroupFile(data, file_id, busid) { deleteGroupFile(data, file_id, busid) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 删除群文件:[${data.group_id}] ${file_id}(${busid})`)
return data.sendApi("delete_group_file", { return data.sendApi("delete_group_file", {
group_id: data.group_id, group_id: data.group_id,
file_id, file_id,
@ -369,6 +377,7 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
createGroupFileFolder(data, name) { createGroupFileFolder(data, name) {
logger.info(`${logger.blue(`[${data.self_id}]`)} 创建群文件夹:[${data.group_id}] ${name}`)
return data.sendApi("create_group_file_folder", { return data.sendApi("create_group_file_folder", {
group_id: data.group_id, group_id: data.group_id,
name, name,