2023-06-29 20:25:21 +08:00
|
|
|
import fetch from "node-fetch"
|
2023-05-11 16:03:18 +08:00
|
|
|
import fs from "node:fs"
|
2023-06-18 11:57:31 +08:00
|
|
|
import path from "node:path"
|
2023-07-27 18:46:26 +08:00
|
|
|
import common from "../common/common.js"
|
2023-06-29 20:25:21 +08:00
|
|
|
import { fileTypeFromBuffer } from "file-type"
|
2023-05-11 16:03:18 +08:00
|
|
|
|
2023-06-26 23:32:52 +08:00
|
|
|
Bot.adapter.push(new class stdinAdapter {
|
|
|
|
constructor() {
|
|
|
|
this.id = "stdin"
|
|
|
|
this.name = "标准输入"
|
2023-07-19 09:34:22 +08:00
|
|
|
this.path = "data/stdin/"
|
2023-07-27 18:46:26 +08:00
|
|
|
common.mkdirs(this.path)
|
2023-06-26 23:32:52 +08:00
|
|
|
}
|
|
|
|
|
2023-05-11 16:03:18 +08:00
|
|
|
async makeBuffer(file) {
|
|
|
|
if (file.match(/^base64:\/\//))
|
|
|
|
return Buffer.from(file.replace(/^base64:\/\//, ""), "base64")
|
|
|
|
else if (file.match(/^https?:\/\//))
|
|
|
|
return Buffer.from(await (await fetch(file)).arrayBuffer())
|
2023-06-23 20:50:45 +08:00
|
|
|
else if (fs.existsSync(file))
|
|
|
|
return Buffer.from(fs.readFileSync(file))
|
2023-07-05 17:02:48 +08:00
|
|
|
return file
|
2023-05-11 16:03:18 +08:00
|
|
|
}
|
|
|
|
|
2023-06-29 20:25:21 +08:00
|
|
|
async fileType(data) {
|
|
|
|
const file = {}
|
|
|
|
try {
|
|
|
|
file.url = data.replace(/^base64:\/\/.*/, "base64://...")
|
|
|
|
file.buffer = await this.makeBuffer(data)
|
|
|
|
file.type = await fileTypeFromBuffer(file.buffer)
|
2023-07-19 09:34:22 +08:00
|
|
|
file.path = `${this.path}${Date.now()}.${file.type.ext}`
|
2023-06-29 20:25:21 +08:00
|
|
|
} catch (err) {
|
|
|
|
logger.error(`文件类型检测错误:${logger.red(err)}`)
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2023-05-11 16:03:18 +08:00
|
|
|
async sendMsg(msg) {
|
|
|
|
if (!Array.isArray(msg))
|
|
|
|
msg = [msg]
|
|
|
|
for (let i of msg) {
|
|
|
|
if (typeof i != "object")
|
|
|
|
i = { type: "text", data: { text: i }}
|
|
|
|
else if (!i.data)
|
|
|
|
i = { type: i.type, data: { ...i, type: undefined }}
|
2023-06-29 20:25:21 +08:00
|
|
|
|
|
|
|
let file
|
|
|
|
if (i.data.file)
|
|
|
|
file = await this.fileType(i.data.file)
|
|
|
|
|
2023-05-11 16:03:18 +08:00
|
|
|
switch (i.type) {
|
|
|
|
case "text":
|
|
|
|
if (i.data.text.match("\n"))
|
|
|
|
i.data.text = `\n${i.data.text}`
|
2023-06-26 23:32:52 +08:00
|
|
|
logger.info(`${logger.blue(`[${this.id}]`)} 发送文本:${i.data.text}`)
|
2023-05-11 16:03:18 +08:00
|
|
|
break
|
|
|
|
case "image":
|
2023-06-29 20:25:21 +08:00
|
|
|
logger.info(`${logger.blue(`[${this.id}]`)} 发送图片:${file.url}\n文件已保存到:${logger.cyan(file.path)}`)
|
|
|
|
fs.writeFileSync(file.path, file.buffer)
|
2023-05-11 16:03:18 +08:00
|
|
|
break
|
|
|
|
case "record":
|
2023-06-29 20:25:21 +08:00
|
|
|
logger.info(`${logger.blue(`[${this.id}]`)} 发送音频:${file.url}\n文件已保存到:${logger.cyan(file.path)}`)
|
|
|
|
fs.writeFileSync(file.path, file.buffer)
|
2023-05-11 16:03:18 +08:00
|
|
|
break
|
|
|
|
case "video":
|
2023-06-29 20:25:21 +08:00
|
|
|
logger.info(`${logger.blue(`[${this.id}]`)} 发送视频:${file.url}\n文件已保存到:${logger.cyan(file.path)}`)
|
|
|
|
fs.writeFileSync(file.path, file.buffer)
|
2023-05-11 16:03:18 +08:00
|
|
|
break
|
|
|
|
case "reply":
|
|
|
|
break
|
|
|
|
case "at":
|
|
|
|
break
|
2023-06-18 11:57:31 +08:00
|
|
|
case "node":
|
2023-07-05 17:02:48 +08:00
|
|
|
Bot.sendForwardMsg(msg => this.sendMsg(msg), i.data)
|
2023-06-18 11:57:31 +08:00
|
|
|
break
|
2023-05-11 16:03:18 +08:00
|
|
|
default:
|
|
|
|
i = JSON.stringify(i)
|
|
|
|
if (i.match("\n"))
|
|
|
|
i = `\n${i}`
|
2023-06-26 23:32:52 +08:00
|
|
|
logger.info(`${logger.blue(`[${this.id}]`)} 发送消息:${i}`)
|
2023-05-11 16:03:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return { message_id: Date.now() }
|
|
|
|
}
|
|
|
|
|
|
|
|
recallMsg(message_id) {
|
2023-06-26 23:32:52 +08:00
|
|
|
logger.info(`${logger.blue(`[${this.id}]`)} 撤回消息:${message_id}`)
|
2023-05-11 16:03:18 +08:00
|
|
|
}
|
|
|
|
|
2023-06-23 20:50:45 +08:00
|
|
|
async sendFile(file, name = path.basename(file)) {
|
|
|
|
const buffer = await this.makeBuffer(file)
|
|
|
|
if (!Buffer.isBuffer(buffer)) {
|
2023-06-26 23:32:52 +08:00
|
|
|
logger.error(`${logger.blue(`[${this.id}]`)} 发送文件错误:找不到文件 ${logger.red(file)}`)
|
2023-06-18 11:57:31 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-19 09:34:22 +08:00
|
|
|
const files = `${this.path}${Date.now()}-${name}`
|
2023-06-26 23:32:52 +08:00
|
|
|
logger.info(`${logger.blue(`[${this.id}]`)} 发送文件:${file}\n文件已保存到:${logger.cyan(files)}`)
|
2023-06-23 20:50:45 +08:00
|
|
|
return fs.writeFileSync(files, buffer)
|
2023-06-18 11:57:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pickFriend() {
|
|
|
|
return {
|
2023-07-29 13:47:45 +08:00
|
|
|
user_id: this.id,
|
|
|
|
nickname: this.name,
|
|
|
|
group_id: this.id,
|
|
|
|
group_name: this.name,
|
2023-06-18 11:57:31 +08:00
|
|
|
sendMsg: msg => this.sendMsg(msg),
|
|
|
|
recallMsg: message_id => this.recallMsg(message_id),
|
|
|
|
sendFile: (file, name) => this.sendFile(file, name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 16:03:18 +08:00
|
|
|
message(msg) {
|
|
|
|
const data = {
|
2023-06-26 23:32:52 +08:00
|
|
|
bot: Bot[this.id],
|
|
|
|
self_id: this.id,
|
|
|
|
user_id: this.id,
|
2023-05-11 16:03:18 +08:00
|
|
|
post_type: "message",
|
|
|
|
message_type: "private",
|
2023-07-29 13:47:45 +08:00
|
|
|
sender: { user_id: this.id, nickname: this.name },
|
2023-05-11 16:03:18 +08:00
|
|
|
message: [{ type: "text", text: msg }],
|
|
|
|
raw_message: msg,
|
2023-06-19 22:59:24 +08:00
|
|
|
friend: this.pickFriend(),
|
2023-05-11 16:03:18 +08:00
|
|
|
}
|
|
|
|
logger.info(`${logger.blue(`[${data.self_id}]`)} 系统消息:[${data.sender.nickname}(${data.user_id})] ${data.raw_message}`)
|
|
|
|
|
2023-08-20 08:35:47 +08:00
|
|
|
Bot.em(`${data.post_type}.${data.message_type}`, data)
|
2023-05-11 16:03:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
load() {
|
2023-06-26 23:32:52 +08:00
|
|
|
Bot[this.id] = {
|
2023-07-21 20:58:26 +08:00
|
|
|
adapter: this,
|
2023-06-26 23:32:52 +08:00
|
|
|
uin: this.id,
|
|
|
|
nickname: this.name,
|
2023-05-11 16:03:18 +08:00
|
|
|
stat: { start_time: Date.now()/1000 },
|
2023-06-26 23:32:52 +08:00
|
|
|
version: { id: this.id, name: this.name },
|
2023-06-18 11:57:31 +08:00
|
|
|
pickFriend: () => this.pickFriend(),
|
2023-07-29 13:47:45 +08:00
|
|
|
get pickUser() { return this.pickFriend },
|
|
|
|
get pickMember() { return this.pickFriend },
|
|
|
|
get pickGroup() { return this.pickFriend },
|
2023-06-19 22:59:24 +08:00
|
|
|
|
2023-06-26 23:32:52 +08:00
|
|
|
fl: new Map().set(this.id, {
|
|
|
|
user_id: this.id,
|
|
|
|
nickname: this.name,
|
|
|
|
group_id: this.id,
|
|
|
|
group_name: this.name,
|
2023-06-19 22:59:24 +08:00
|
|
|
}),
|
2023-07-29 13:47:45 +08:00
|
|
|
get gl() { return this.fl },
|
2023-08-20 08:35:47 +08:00
|
|
|
gml: new Map,
|
2023-05-11 16:03:18 +08:00
|
|
|
}
|
2023-08-20 08:35:47 +08:00
|
|
|
Bot[this.id].gml.set(this.id, Bot[this.id].fl)
|
2023-05-11 16:03:18 +08:00
|
|
|
|
2023-06-26 23:32:52 +08:00
|
|
|
process[this.id].on("data", data => this.message(data.toString()))
|
2023-05-11 16:03:18 +08:00
|
|
|
|
2023-06-26 23:32:52 +08:00
|
|
|
logger.mark(`${logger.blue(`[${this.id}]`)} ${this.name}(${this.id}) 已连接`)
|
2023-08-20 08:35:47 +08:00
|
|
|
Bot.em(`connect.${this.id}`, { self_id: this.id })
|
2023-05-11 16:03:18 +08:00
|
|
|
}
|
2023-06-26 23:32:52 +08:00
|
|
|
})
|