Miao-Yunzai/lib/common/common.js

123 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-03-04 14:30:13 +08:00
import { pipeline } from 'stream'
import { promisify } from 'util'
import fetch from 'node-fetch'
import fs from 'node:fs'
import path from 'node:path'
/**
* 发送私聊消息仅给好友发送
* @param user_id qq号
* @param msg 消息
*/
async function relpyPrivate(userId, msg) {
2023-03-04 14:30:13 +08:00
userId = Number(userId)
let friend = Bot.fl.get(userId)
if (friend) {
logger.mark(`发送好友消息[${friend.nickname}](${userId})`)
return await Bot.pickUser(userId).sendMsg(msg).catch((err) => {
logger.mark(err)
})
}
}
/**
* 休眠函数
* @param ms 毫秒
*/
function sleep(ms) {
2023-03-04 14:30:13 +08:00
return new Promise((resolve) => setTimeout(resolve, ms))
}
/**
* 下载保存文件
* @param fileUrl 下载地址
* @param savePath 保存路径
*/
async function downFile(fileUrl, savePath, param = {}) {
2023-03-04 14:30:13 +08:00
try {
mkdirs(path.dirname(savePath))
logger.debug(`[下载文件] ${fileUrl}`)
2023-06-07 05:45:41 +08:00
const response = await fetch(fileUrl, param)
2023-03-04 14:30:13 +08:00
const streamPipeline = promisify(pipeline)
await streamPipeline(response.body, fs.createWriteStream(savePath))
return true
} catch (err) {
logger.error(`下载文件错误:${err}`)
return false
}
}
function mkdirs(dirname) {
2023-03-04 14:30:13 +08:00
if (fs.existsSync(dirname)) {
return true
} else {
if (mkdirs(path.dirname(dirname))) {
fs.mkdirSync(dirname)
return true
}
}
}
/**
* 制作转发消息
* @param e 消息事件
2023-03-04 14:30:13 +08:00
* @param msg 消息数组
* @param dec 转发描述
2023-07-30 15:15:58 +08:00
* @param msgsscr 转发信息是否伪装
2023-03-04 14:30:13 +08:00
*/
async function makeForwardMsg(e, msg = [], dec = '', msgsscr = false) {
2023-07-27 04:19:02 +08:00
if (!Array.isArray(msg)) msg = [msg]
2023-07-30 15:15:58 +08:00
let name = msgsscr ? e.sender.card || e.user_id : Bot.nickname
let id = msgsscr ? e.user_id : Bot.uin
2023-03-04 14:30:13 +08:00
if (e.isGroup) {
let info = await e.bot.getGroupMemberInfo(e.group_id, id)
name = info.card || info.nickname
2023-03-04 14:30:13 +08:00
}
2023-03-04 14:30:13 +08:00
let userInfo = {
2023-06-07 05:45:41 +08:00
user_id: id,
nickname: name
2023-03-04 14:30:13 +08:00
}
let forwardMsg = []
2023-07-27 04:19:02 +08:00
for (const message of msg){
if(!message) continue
2023-03-04 14:30:13 +08:00
forwardMsg.push({
...userInfo,
2023-07-27 04:19:02 +08:00
message: message
2023-03-04 14:30:13 +08:00
})
2023-07-27 04:19:02 +08:00
}
2023-03-04 14:30:13 +08:00
/** 制作转发内容 */
if (e?.group?.makeForwardMsg) {
2023-03-04 14:30:13 +08:00
forwardMsg = await e.group.makeForwardMsg(forwardMsg)
} else if (e?.friend?.makeForwardMsg) {
2023-03-04 14:30:13 +08:00
forwardMsg = await e.friend.makeForwardMsg(forwardMsg)
} else {
return msg.join('\n')
2023-03-04 14:30:13 +08:00
}
if (dec) {
/** 处理描述 */
if (typeof (forwardMsg.data) === 'object') {
let detail = forwardMsg.data?.meta?.detail
if (detail) {
detail.news = [{ text: dec }]
}
} else {
forwardMsg.data = forwardMsg.data
.replace(/\n/g, '')
.replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
.replace(/___+/, `<title color="#777777" size="26">${dec}</title>`)
}
2023-03-04 14:30:13 +08:00
}
return forwardMsg
}
export default { sleep, relpyPrivate, downFile, makeForwardMsg }