细节优化

This commit is contained in:
🌌 2023-06-23 20:50:45 +08:00
parent cef97bcc0c
commit 8088a85aae
6 changed files with 35 additions and 20 deletions

View File

@ -4,6 +4,11 @@ import express from "express"
import http from "http"
export default class WebSocketAdapter {
request(req) {
logger.info(`${logger.blue(`[${req.ip}]`)} HTTP ${req.method} 请求:${req.url} ${JSON.stringify(req.rawHeaders)}`)
req.res.redirect("https://github.com/TimeRainStarSky/Yunzai")
}
async load() {
const wss = {}
for (const file of fs.readdirSync('./lib/adapter/WebSocket').filter(file => file.endsWith('.js'))) {
@ -19,7 +24,8 @@ export default class WebSocketAdapter {
}
const app = express()
app.get("*", (req, res) => res.redirect("https://github.com/TimeRainStarSky/Yunzai"))
app.get("*", this.request)
app.post("*", this.request)
const server = http.createServer(app)
server.on("upgrade", (req, socket, head) => {

View File

@ -191,9 +191,9 @@ export default class ComWeChatAdapter {
})
}
async sendFile(data, send, file, name) {
async sendFile(data, send, file, name = path.basename(file)) {
return send(segment.custom("file", {
file_id: (await this.uploadFile(data, file, name || path.basename(file))).file_id
file_id: (await this.uploadFile(data, file, name)).file_id
}))
}

View File

@ -332,9 +332,7 @@ export default class gocqhttpAdapter {
})
}
async makeFile(data, file, name) {
if (!name)
name = path.basename(file)
async makeFile(data, file, name = path.basename(file)) {
if (file.match(/^https?:\/\//))
file = (await this.downloadFile(data, file)).file
else if (fs.existsSync(file))
@ -667,6 +665,11 @@ export default class gocqhttpAdapter {
logger.info(`${logger.blue(`[${data.self_id}]`)} 子频道创建:[${data.guild_id}-${data.channel_id}, ${data.user_id}] ${JSON.stringify(data.channel_info)}`)
Bot[data.self_id].gl = await this.getGroupMap(data)
break
case "channel_destroyed":
data.notice_type = "guild_channel_destroyed"
logger.info(`${logger.blue(`[${data.self_id}]`)} 子频道删除:[${data.guild_id}-${data.channel_id}, ${data.user_id}] ${JSON.stringify(data.channel_info)}`)
Bot[data.self_id].gl = await this.getGroupMap(data)
break
default:
logger.warn(`${logger.blue(`[${data.self_id}]`)} 未知通知:${logger.red(JSON.stringify(data))}`)
}

View File

@ -7,6 +7,8 @@ export default class stdinAdapter {
return Buffer.from(file.replace(/^base64:\/\//, ""), "base64")
else if (file.match(/^https?:\/\//))
return Buffer.from(await (await fetch(file)).arrayBuffer())
else if (fs.existsSync(file))
return Buffer.from(fs.readFileSync(file))
else
return file
}
@ -26,17 +28,17 @@ export default class stdinAdapter {
logger.info(`${logger.blue(`[stdin]`)} 发送文本:${i.data.text}`)
break
case "image":
i.file = `${process.cwd()}/data/${Date.now()}.png`
i.file = `${Bot.stdin.data_dir}${Date.now()}.png`
logger.info(`${logger.blue(`[stdin]`)} 发送图片:${i.data.file.replace(/^base64:\/\/.*/, "base64://...")}\n文件已保存到:${logger.cyan(i.file)}`)
fs.writeFileSync(i.file, await this.makeBuffer(i.data.file))
break
case "record":
i.file = `${process.cwd()}/data/${Date.now()}.mp3`
i.file = `${Bot.stdin.data_dir}${Date.now()}.mp3`
logger.info(`${logger.blue(`[stdin]`)} 发送音频:${i.data.file.replace(/^base64:\/\/.*/, "base64://...")}\n文件已保存到:${logger.cyan(i.file)}`)
fs.writeFileSync(i.file, await this.makeBuffer(i.data.file))
break
case "video":
i.file = `${process.cwd()}/data/${Date.now()}.mp4`
i.file = `${Bot.stdin.data_dir}${Date.now()}.mp4`
logger.info(`${logger.blue(`[stdin]`)} 发送视频:${i.data.file.replace(/^base64:\/\/.*/, "base64://...")}\n文件已保存到:${logger.cyan(i.file)}`)
fs.writeFileSync(i.file, await this.makeBuffer(i.data.file))
break
@ -68,20 +70,16 @@ export default class stdinAdapter {
return messages
}
async sendFile(file, name) {
let buffer
if (Buffer.isBuffer(file)) {
buffer = file
} else if (file.match(/^https?:\/\//)) {
buffer = Buffer.from(await (await fetch(file)).arrayBuffer())
} else if (!fs.existsSync(file)) {
async sendFile(file, name = path.basename(file)) {
const buffer = await this.makeBuffer(file)
if (!Buffer.isBuffer(buffer)) {
logger.error(`${logger.blue(`[stdin]`)} 发送文件错误:找不到文件 ${logger.red(file)}`)
return false
}
const files = `${process.cwd()}/data/${Date.now()}-${name || path.basename(file)}`
const files = `${Bot.stdin.data_dir}${Date.now()}-${name}`
logger.info(`${logger.blue(`[stdin]`)} 发送文件:${file}\n文件已保存到:${logger.cyan(files)}`)
return fs.copyFileSync(buffer || file, files)
return fs.writeFileSync(files, buffer)
}
pickFriend() {
@ -131,8 +129,13 @@ export default class stdinAdapter {
group_id: "stdin",
group_name: "标准输入",
}),
data_dir: `${process.cwd()}/data/stdin/`,
}
if (!fs.existsSync(Bot.stdin.data_dir))
fs.mkdirSync(Bot.stdin.data_dir)
if (Array.isArray(Bot.uin)) {
if (!Bot.uin.includes("stdin"))
Bot.uin.push("stdin")

View File

@ -91,7 +91,10 @@ export default class Yunzai extends EventEmitter {
}
pickMember(group_id, user_id) {
return this.pickGroup(group_id)?.pickMember(user_id)
const group = this.pickGroup(group_id)
if (group) return group.pickMember(user_id)
return false
}
sendFriendMsg(bot_id, user_id, msg) {

View File

@ -30,7 +30,7 @@
"node-schedule": "^2.1.1",
"node-xlsx": "^0.23.0",
"pm2": "^5.3.0",
"puppeteer": "^20.7.2",
"puppeteer": "^20.7.3",
"redis": "^4.6.7",
"sequelize": "^6.32.1",
"sqlite3": "^5.1.6",