From 828c1b1e88dac349b0f34ffcde38b2fb586c39fe Mon Sep 17 00:00:00 2001 From: yeyang <746659424@qq.com> Date: Mon, 11 Mar 2024 20:18:43 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E5=88=86=E7=A6=BBfun.js?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/fun.js | 193 +++------------- model/api/QQApi.js | 22 +- model/api/funApi.js | 363 +----------------------------- model/api/funApi/bgg.js | 115 ++++++++++ model/api/funApi/pageCrawling.js | 116 ++++++++++ model/api/funApi/randomSinging.js | 22 ++ model/api/funApi/thumbUp.js | 116 ++++++++++ model/api/funApi/utils.js | 12 + model/api/funApi/youdao.js | 60 +++++ 9 files changed, 494 insertions(+), 525 deletions(-) create mode 100644 model/api/funApi/bgg.js create mode 100644 model/api/funApi/pageCrawling.js create mode 100644 model/api/funApi/randomSinging.js create mode 100644 model/api/funApi/thumbUp.js create mode 100644 model/api/funApi/utils.js create mode 100644 model/api/funApi/youdao.js diff --git a/apps/fun.js b/apps/fun.js index 8821786..36a7fbc 100644 --- a/apps/fun.js +++ b/apps/fun.js @@ -2,11 +2,8 @@ import _ from 'lodash' import fetch from 'node-fetch' import plugin from '../../../lib/plugins/plugin.js' import { Config } from '../components/index.js' -import { faildsImgs, heisiType, pandadiuType, successImgs } from '../constants/fun.js' -import { QQApi, common, funApi, memes, uploadRecord } from '../model/index.js' - -/** API请求错误文案 */ -const API_ERROR = '❎ 出错辣,请稍后重试' +import { heisiType, pandadiuType, xiurenTypeId } from '../constants/fun.js' +import { common, funApi, uploadRecord } from '../model/index.js' /** 开始执行文案 */ const START_EXECUTION = '椰奶产出中......' @@ -24,7 +21,7 @@ _.forIn(picApis, (values, key) => { const apiReg = new RegExp(`(${picApiKeys.join('|')}|^jktj$|^接口统计$)`) export class Fun extends plugin { - constructor() { + constructor () { super({ name: '椰奶娱乐', event: 'message', @@ -54,10 +51,6 @@ export class Fun extends plugin { reg: '^#?coser$', fnc: 'coser' }, - // { - // reg: `^#?来点(${Object.keys(heisiType).join('|')})$`, - // fnc: 'heisiwu' - // }, { reg: '^#?铃声搜索', fnc: 'lingsheng' @@ -79,7 +72,7 @@ export class Fun extends plugin { fnc: 'acg' }, { - reg: `^#来点(${Object.keys(funApi.xiurenTypeId).join('|')})$`, + reg: `^#来点(${Object.keys(xiurenTypeId).join('|')})$`, fnc: 'xiuren' } @@ -88,7 +81,7 @@ export class Fun extends plugin { } /** 随机唱鸭 */ - async Sing(e) { + async Sing (e) { let data = await funApi.randomSinging() if (data.error) return e.reply(data.error) await e.reply(await uploadRecord(data.audioUrl, 0, false)) @@ -96,7 +89,7 @@ export class Fun extends plugin { } /** 支付宝语音 */ - async ZFB(e) { + async ZFB (e) { let amount = parseFloat(e.msg.replace(/#|支付宝到账|元|圆/g, '').trim()) if (!/^\d+(\.\d{1,2})?$/.test(amount)) return e.reply('你觉得这河里吗!!', true) @@ -108,7 +101,7 @@ export class Fun extends plugin { } /** 有道翻译 */ - async youdao(e) { + async youdao (e) { const msg = e.msg.match(/#(([\u4e00-\u9fa5]{2,6})-)?([\u4e00-\u9fa5]{2,6})?翻译(.*)/) // 如果是在群聊中回复,则获取上一条消息作为翻译内容 if (e.source) { @@ -125,121 +118,12 @@ export class Fun extends plugin { } /** 点赞 */ - async thumbUp(e) { - if (e.msg.includes(`超`, `操`, `草`, `抄`, `吵`, `炒`)) { - this.do = `超` - } else { - this.do = `赞` - } - /** 判断是赞自己还是赞别人 */ - if (e.at && e.msg.includes(`他`, `她`, `它`, `TA`, `ta`, `Ta`)) { - /** 判断是否为好友 */ - let isFriend = await (e.bot ?? Bot).fl.get(e.at) - let allowLikeByStrangers = Config.whole.Strangers_love - if (!isFriend && !allowLikeByStrangers) return e.reply(`不加好友不${this.do}🙄`, true) - /** 执行点赞 */ - let n = 0 - let failsMsg = `今天已经${this.do}过了,还搁这讨${this.do}呢!!!` - for (let i = 0; i < 10; i++) { - let res = null - try { - res = await new QQApi(e).thumbUp(e.at, 10) - } catch (error) { - logger.error(error) - return common.handleException(e, error) - } - logger.debug(`${e.logFnc}给${e.at}点赞`, res) - if (res.code) { - if (res.code == 1) { - failsMsg = `${this.do}失败,请检查是否开启陌生人点赞或添加好友` - } else { - if (this.do == `超`) { - failsMsg = res.msg.replace(/点赞/g, '超').replace('给', '超').replace('点', '').replace('个赞', '下') - } else { - failsMsg = res.msg - } - } - break - } else { - n += 10 - } - } - let successMsg = `给${e.at}${this.do}了${n}下哦,记得回我~ ${isFriend ? `` : `(如${this.do}失败请添加好友)`}` - const avatar = `https://q1.qlogo.cn/g?b=qq&s=100&nk=${e.at}` - const successFn = _.sample(['ganyu', 'zan']) - - /** 判断点赞是否成功 */ - let msg = n > 0 - ? [ - `\n${successMsg}`, - segment.image((await memes[successFn](avatar)) || - _.sample(successImgs) + e.user_id) - ] - : [ - `\n${failsMsg}`, - segment.image((await memes.crawl(avatar)) || - _.sample(faildsImgs) + e.user_id) - ] - - /** 回复 */ - e.reply(msg, true, { at: e.at }) - } else if (!e.msg.includes(`他`, `她`, `它`, `TA`, `ta`, `Ta`)) { - /** 判断是否为好友 */ - let isFriend = await (e.bot ?? Bot).fl.get(e.user_id) - let allowLikeByStrangers = Config.whole.Strangers_love - if (!isFriend && !allowLikeByStrangers) return e.reply(`不加好友不${this.do}🙄`, true) - - /** 执行点赞 */ - let n = 0 - let failsMsg = `今天已经${this.do}过了,还搁这讨${this.do}呢!!!` - for (let i = 0; i < 10; i++) { - let res = null - try { - res = await new QQApi(e).thumbUp(e.user_id, 10) - } catch (error) { - logger.error(error) - return common.handleException(e, error) - } - logger.debug(`${e.logFnc}给${e.user_id}点赞`, res) - if (res.code) { - if (res.code == 1) { - failsMsg = `${this.do}失败,请检查是否开启陌生人点赞或添加好友` - } else { - if (this.do == `超`) { - failsMsg = res.msg.replace(/点赞/g, '超').replace('给', '超').replace('点', '').replace('个赞', '下') - } else { - failsMsg = res.msg - } - } - break - } else { - n += 10 - } - } - let successMsg = `给你${this.do}了${n}下哦,记得回我~ ${isFriend ? `` : `(如${this.do}失败请添加好友)`}` - const avatar = `https://q1.qlogo.cn/g?b=qq&s=100&nk=${e.user_id}` - const successFn = _.sample(['ganyu', 'zan']) - - /** 判断点赞是否成功 */ - let msg = n > 0 - ? [ - `\n${successMsg}`, - segment.image((await memes[successFn](avatar)) || - _.sample(successImgs) + e.user_id) - ] - : [ - `\n${failsMsg}`, - segment.image((await memes.crawl(avatar)) || - _.sample(faildsImgs) + e.user_id) - ] - - /** 回复 */ - e.reply(msg, true, { at: true }) - } + async thumbUp (e) { + await funApi.thumbUp(e) } // github - async GH(e) { + async GH (e) { const api = 'https://opengraph.githubassets.com' let reg = /github.com\/[a-zA-Z0-9-]{1,39}\/[a-zA-Z0-9_-]{1,100}(?:\/(?:pull|issues)\/\d+)?/ @@ -256,7 +140,7 @@ export class Fun extends plugin { } // coser - async coser(e) { + async coser (e) { if (!common.checkSeSePermission(e)) return false e.reply(START_EXECUTION) @@ -266,7 +150,7 @@ export class Fun extends plugin { } // cos/acg搜索 - async acg(e) { + async acg (e) { if (!common.checkSeSePermission(e)) return false e.reply(START_EXECUTION) const reg = new RegExp(`^#(${Object.keys(pandadiuType).join('|')})?acg(.*)$`) @@ -277,7 +161,7 @@ export class Fun extends plugin { } // 黑丝 - async heisiwu(e) { + async heisiwu (e) { if (!common.checkSeSePermission(e, 'sesepro')) return false e.reply(START_EXECUTION) @@ -289,7 +173,7 @@ export class Fun extends plugin { } // 萌堆 - async mengdui(e) { + async mengdui (e) { if (!common.checkSeSePermission(e, 'sesepro')) return false // 开始执行 e.reply(START_EXECUTION) @@ -299,7 +183,7 @@ export class Fun extends plugin { .catch(err => common.handleException(e, err)) } - async xiuren(e) { + async xiuren (e) { if (!common.checkSeSePermission(e, 'pro')) return false // 开始执行 e.reply(START_EXECUTION) @@ -309,41 +193,24 @@ export class Fun extends plugin { } // 铃声多多 - async lingsheng(e) { - let msg = e.msg.replace(/#|铃声搜索/g, '') - let num = Math.ceil(Math.random() * 15) - if (num == 0) num = 1 - let api = `http://xiaobai.klizi.cn/API/music/lingsheng.php?msg=${msg}&n=${num}` - let res = await fetch(api).then(res => res.json()).catch(err => logger.error(err)) - if (!res) return e.reply(API_ERROR) - if (res.title == null && res.author == null) return e.reply('❎ 没有找到相关的歌曲哦~', true) + // async lingsheng (e) { + // let msg = e.msg.replace(/#|铃声搜索/g, '') + // let num = Math.ceil(Math.random() * 15) + // if (num == 0) num = 1 + // let api = `http://xiaobai.klizi.cn/API/music/lingsheng.php?msg=${msg}&n=${num}` + // let res = await fetch(api).then(res => res.json()).catch(err => logger.error(err)) + // if (!res) return e.reply(API_ERROR) + // if (res.title == null && res.author == null) return e.reply('❎ 没有找到相关的歌曲哦~', true) - await e.reply([ - `标题:${res.title}\n`, - `作者:${res.author}` - ]) - await e.reply(await uploadRecord(res.aac, 0, false)) - } - - /** 半次元话题 */ - async bcyTopic(e) { - let api = 'https://xiaobai.klizi.cn/API/other/bcy_topic.php' - let res = await fetch(api).then(res => res.json()).catch(err => logger.error(err)) - if (!res) return e.reply(API_ERROR) - if (res.code != 200) return e.reply('❎ 出错辣' + JSON.stringify(res)) - if (_.isEmpty(res.data)) return e.reply('请求错误!无数据,请稍后再试') - let msg = [] - for (let i of res.data) { - if (!i.title || _.isEmpty(i.image)) continue - msg.push(i.title) - msg.push(i.image.map(item => segment.image(item))) - } - if (_.isEmpty(msg)) return this.bcyTopic(e) - common.getforwardMsg(e, msg) - } + // await e.reply([ + // `标题:${res.title}\n`, + // `作者:${res.author}` + // ]) + // await e.reply(await uploadRecord(res.aac, 0, false)) + // } // api大集合 - async picture(e) { + async picture (e) { let { sese, sesepro } = Config.getGroup(e.group_id) if (!sese && !sesepro && !e.isMaster) return false let key = 'yenai:apiAggregate:CD' diff --git a/model/api/QQApi.js b/model/api/QQApi.js index f4e03ec..add3d08 100644 --- a/model/api/QQApi.js +++ b/model/api/QQApi.js @@ -428,17 +428,17 @@ export default class { */ async thumbUp (uid, times = 1) { let core = this.Bot.icqq?.core - if (!core) try { - core = (await import('icqq')).core - } catch (error) { - const thumbUp = this.Bot.pickFriend(uid).thumbUp - if (!thumbUp) throw Error('当前协议端不支持点赞,详情查看\nhttps://gitee.com/TimeRainStarSky/Yunzai') - const res = { ...await thumbUp(times) } - if (res.retcode && !res.code) - res.code = res.retcode - if (res.message && !res.msg) - res.msg = res.message - return res + if (!core) { + try { + core = (await import('icqq')).core + } catch (error) { + const thumbUp = this.Bot.pickFriend(uid).thumbUp + if (!thumbUp) throw Error('当前协议端不支持点赞,详情查看\nhttps://gitee.com/TimeRainStarSky/Yunzai') + const res = { ...await thumbUp(times) } + if (res.retcode && !res.code) { res.code = res.retcode } + if (res.message && !res.msg) { res.msg = res.message } + return res + } } if (times > 20) { times = 20 } let ReqFavorite diff --git a/model/api/funApi.js b/model/api/funApi.js index 19e679a..903e040 100644 --- a/model/api/funApi.js +++ b/model/api/funApi.js @@ -1,351 +1,12 @@ -import _ from 'lodash' -import md5 from 'md5' -import fetch from 'node-fetch' -import request from '../../lib/request/request.js' -import { puppeteer } from '../index.js' -import { xiurenTypeId, youDaoLangType, pandadiuType } from '../../constants/fun.js' - -const API_ERROR = '出了点小问题,待会再试试吧' - -let cheerio = null -export default new class { - constructor () { - this.xiurenTypeId = xiurenTypeId - } - - async _importDependency () { - if (cheerio) return cheerio - cheerio = await import('cheerio') - .catch(() => { - throw Error('未检测到依赖cheerio,请安装后再使用该功能,安装命令:pnpm add cheerio -w 或 pnpm install -P') - }) - } - - /** 有道翻译 */ - async youdao (msg, to = 'auto', from = 'auto') { - if (to != 'auto') to = youDaoLangType.find(item => item.label == to)?.code - if (from != 'auto') from = youDaoLangType.find(item => item.label == from)?.code - if (!to || !from) return `未找到翻译的语种,支持的语言为:\n${youDaoLangType.map(item => item.label).join(',')}\n示例:#翻译你好 - 自动翻译\n#日语翻译你好 - 指定翻译为语种\n#中文-日语翻译你好 - 指定原语言翻译为指定语言` - // 翻译结果为空的提示 - const RESULT_ERROR = '找不到翻译结果' - // API 请求错误提示 - const qs = (obj) => { - let res = '' - for (const [k, v] of Object.entries(obj)) { res += `${k}=${encodeURIComponent(v)}&` } - return res.slice(0, res.length - 1) - } - const appVersion = '5.0 (Windows NT 10.0; Win64; x64) Chrome/98.0.4750.0' - const payload = { - from, - to, - bv: md5(appVersion), - client: 'fanyideskweb', - doctype: 'json', - version: '2.1', - keyfrom: 'fanyi.web', - action: 'FY_BY_DEFAULT', - smartresult: 'dict' - } - const headers = { - 'Host': 'fanyi.youdao.com', - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/98.0.4758.102', - 'Referer': 'https://fanyi.youdao.com/', - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'Cookie': 'OUTFOX_SEARCH_USER_ID_NCOO=133190305.98519628; OUTFOX_SEARCH_USER_ID="2081065877@10.169.0.102";' - } - const api = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' - const key = 'Ygy_4c=r#e#4EX^NUGUc5' - - const i = msg // 翻译的内容 - const lts = '' + new Date().getTime() - const salt = lts + parseInt(String(10 * Math.random()), 10) - const sign = md5(payload.client + i + salt + key) - const postData = qs(Object.assign({ i, lts, sign, salt }, payload)) - try { - let { errorCode, translateResult } = await fetch(api, { - method: 'POST', - body: postData, - headers - }).then(res => res.json()).catch(err => console.error(err)) - if (errorCode != 0) return API_ERROR - translateResult = _.flattenDeep(translateResult)?.map(item => item.tgt).join('\n') - if (!translateResult) return RESULT_ERROR - return translateResult - } catch (err) { - logger.error(err) - return API_ERROR - } - } - - /** 随机唱歌/唱鸭 */ - async randomSinging () { - try { - const api = 'https://m.api.singduck.cn/user-piece/SoQJ9cKu61FJ1Vwc7' - let res = await fetch(api).then(res => res.text()) - let JSONdara = JSON.parse(res.match(/