🐞 fix: 修复各种小问题

This commit is contained in:
zhiyu1998 2022-11-27 21:18:05 +08:00
parent 22415c7546
commit 46a1e209b7
3 changed files with 61 additions and 30 deletions

View File

@ -79,7 +79,7 @@ export class mystery extends plugin {
result = await oldReply(msgs, quote, data)
} else {
let MsgList = [ {
message: msgs, nickname: Bot.nickname, user_id: Bot.uin
message: msgs, nickname: Bot.nickname, user_id: Bot.user_id
} ]
let forwardMsg = await Bot.makeForwardMsg(MsgList)
@ -113,25 +113,20 @@ export class mystery extends plugin {
// 回复
this.reply('确实是吧, 正在探索...')
// 请求
let images = []
let imgData = []
let url = `https://www.cos6.net/wp-json/wp/v2/posts?page=${ page }`
let images = []
await fetch(url)
.then((resp) => {
return resp.json()
})
.then((json) => {
if (!json.length) {
this.e.reply('探索失败,你再我去一次吧')
return false
const template = {
nickname: this.e.sender.card || this.e.user_id,
user_id: this.e.user_id
}
const content = json[randomIndex].content
images = this.getImages(content.rendered)
// 如果图片为空直接返回
if (images.length === 0) {
this.e.reply('探索失败,你再我去一次吧')
return false
}
images = this.getCos6Img(content.rendered)
// 洗牌
images = _.shuffle(images)
// 限制长度
@ -139,16 +134,19 @@ export class mystery extends plugin {
images = images.slice(1, imageCountLimit + 1)
}
// 循环队列
images.forEach((item) => {
imgData.push({
message: segment.image(item),
nickname: this.e.sender.card || this.e.user_id,
user_id: this.e.user_id
})
})
for (let i = 0; i < images.length; i++) {
images[i] = {
message: segment.image(images[i]),
...template
}
}
})
.catch((err) => logger.error(err))
return !!(await this.reply(await Bot.makeForwardMsg(imgData)))
.catch((err) => {
this.e.reply('探索失败,你再我去一次吧')
logger.error(err)
return false
})
return !!(await this.reply(await Bot.makeForwardMsg(images)))
}
async cospro (e) {
@ -187,15 +185,14 @@ export class mystery extends plugin {
})
.then((json) => {
if (!json.length) {
this.e.reply('探索失败,你再我去一次吧')
e.reply('探索失败,你再我去一次吧')
return false
}
const content = json[randomIndex].content
images = this.getImages2(content.rendered)
console.log(images)
// 如果图片为空直接返回
if (images.length === 0) {
this.e.reply('探索失败,你再我去一次吧')
e.reply('探索失败,你再我去一次吧')
return false
}
// 洗牌
@ -208,8 +205,8 @@ export class mystery extends plugin {
images.forEach((item) => {
imgData.push({
message: segment.image(item),
nickname: this.e.sender.card || this.e.user_id,
user_id: this.e.user_id
nickname: e.sender.card || e.user_id,
user_id: e.user_id
})
})
})
@ -256,12 +253,12 @@ export class mystery extends plugin {
}
// 正则:获取图片
getImages (string) {
const imgRex = /(http|https):\/\/([\w.]+\/?)\S*.(jpg|JPG|png|PNG|gif|GIF|jpeg|JPEG)/g
getCos6Img (string) {
const imgRex = /\/([\w].*?).(jpg|JPG|png|PNG|gif|GIF|jpeg|JPEG|svg)/g
const images = []
let img
while ((img = imgRex.exec(string))) {
images.push(encodeURI(img[0]))
images.push(`https://www.cos6.net/${img[1]}.jpg`)
}
return images
}
@ -272,7 +269,7 @@ export class mystery extends plugin {
const images = []
let img
while ((img = imgRex.exec(string))) {
images.push(encodeURI(img[1]))
images.push(img[1])
}
return images
}

View File

@ -8,6 +8,7 @@ import axios from "axios";
import _ from 'lodash'
import { mkdirsSync } from '../utils/file.js'
import { downloadBFile, getDownloadUrl, mergeFileToMp4 } from '../utils/bilibili.js'
import { get, remove, add} from "../utils/redisu.js";
export class tools extends plugin {
constructor () {
@ -39,7 +40,10 @@ export class tools extends plugin {
},
],
});
// 视频保存路径
this.defaultPath = `./data/rcmp4/`
// redis的key
this.redisKey = `Yz:tools:cache:${this.e.group_id}`
}
// 翻译插件
@ -145,6 +149,7 @@ export class tools extends plugin {
return true
}
// 百科
async wiki (e) {
const key = e.msg.replace(/#|百科|wiki/g, "").trim();
const url = `https://xiaoapi.cn/API/bk.php?m=json&type=bd&msg=${ encodeURI(key) }`
@ -193,6 +198,7 @@ export class tools extends plugin {
if (!fs.existsSync(this.defaultPath)) {
mkdirsSync(this.defaultPath);
}
const redisObj = get(this.redisKey)
const target = this.defaultPath + `${ this.e.group_id || this.e.user_id }/temp.mp4`
// 待优化
if (fs.existsSync(target)) {

28
utils/redisu.js Normal file
View File

@ -0,0 +1,28 @@
// 批量/单个添加
function add (key, values) {
if (typeof values instanceof Array) {
values.forEach(async (value) => {
await redis.set(key, value)
})
} else {
redis.set(key, values)
}
}
// 批量/单个删除
function remove (key, values) {
if (typeof values instanceof Array) {
values.forEach(async (values) => {
await redis.del(key, values)
})
} else {
redis.del(key, values)
}
}
// 批量/单个查询
function get (key) {
return redis.get(key)
}
export { get, remove, add }