🥅 handleException

This commit is contained in:
yeyang
2023-08-10 14:54:50 +08:00
parent 4bdbe87912
commit 8117258f6b
15 changed files with 92 additions and 65 deletions

View File

@@ -363,7 +363,7 @@ export class Assistant extends plugin {
await this.Bot.pickFriend(qq).sendMsg(e.message)
.then(() => e.reply('✅ 私聊消息已送达'))
.catch(err => e.reply(`❎ 发送失败\n错误信息为:${err.stack}`))
.catch(err => common.handleException(e, err, { MsgTemplate: '❎ 发送失败\n错误信息为:{error}' }))
}
/** 发群聊 */
@@ -384,7 +384,7 @@ export class Assistant extends plugin {
await this.Bot.pickGroup(gpid).sendMsg(e.message)
.then(() => e.reply('✅ 群聊消息已送达'))
.catch((err) => e.reply(`❎ 发送失败\n错误信息为:${err.stack}`))
.catch((err) => common.handleException(e, err, { MsgTemplate: '❎ 发送失败\n错误信息为:{error}' }))
}
// 发送群列表
@@ -421,13 +421,16 @@ export class Assistant extends plugin {
if (sendList.length === 1) {
await this.Bot.pickGroup(sendList[0]).sendMsg(e.message)
.then(() => e.reply('✅ ' + sendList[0] + ' 群聊消息已送达'))
.catch((err) => e.reply(`${sendList[0]} 发送失败\n错误信息为:${err.stack}`))
.catch((err) =>
common.handleException(e, err, { MsgTemplate: `${sendList[0]} 发送失败\n错误信息为:{error}` })
)
} else {
e.reply('发送多个群聊将每5秒发送一条消息')
for (let i of sendList) {
await this.Bot.pickGroup(i).sendMsg(e.message)
.then(() => e.reply('✅ ' + i + ' 群聊消息已送达'))
.catch((err) => e.reply(`${i} 发送失败\n错误信息为:${err.stack}`))
.catch((err) =>
common.handleException(e, err, { MsgTemplate: `${i} 发送失败\n错误信息为:{error}` }))
await common.sleep(5000)
}
}

View File

@@ -64,7 +64,7 @@ export class NewBika extends plugin {
let page = common.translateChinaNum(regRet[5])
await Bika.search(regRet[3], page, regRet[2])
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => { e.reply(err.stack) })
.catch(err => common.handleException(e, err))
}
/** 漫画页面 */
@@ -76,7 +76,7 @@ export class NewBika extends plugin {
let order = common.translateChinaNum(regRet[6])
await Bika.comicPage(regRet[2], page, order)
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => { e.reply(err.stack) })
.catch(err => common.handleException(e, err))
}
/** 快速查看 */
@@ -85,7 +85,7 @@ export class NewBika extends plugin {
let number = e.msg.match(/\d+/) - 1
await Bika.viewComicPage(number)
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => { e.reply(err.stack) })
.catch(err => common.handleException(e, err))
}
/** 下一页 */
@@ -93,7 +93,7 @@ export class NewBika extends plugin {
if (!await this._Authentication(e)) return
await Bika.next()
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
/** 下一话 */
@@ -101,7 +101,7 @@ export class NewBika extends plugin {
if (!await this._Authentication(e)) return
await Bika.next('chapter')
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
/** 类别列表 */
@@ -110,7 +110,7 @@ export class NewBika extends plugin {
e.reply(Pixiv.startMsg)
await Bika.categories()
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => { e.reply(err.stack) })
.catch(err => common.handleException(e, err))
}
/** 漫画细节 */
@@ -120,7 +120,7 @@ export class NewBika extends plugin {
let id = e.msg.match(new RegExp(`#?${Prefix}(详情|细节)(.*)`))[3]
await Bika.comicDetail(id)
.then(res => common.recallSendForwardMsg(e, res, { oneMsg: true }))
.catch(err => { e.reply(err.stack) })
.catch(err => common.handleException(e, err))
}
/** 图片质量 */

View File

@@ -132,7 +132,7 @@ export class Fun extends plugin {
/** 判断是赞自己还是赞别人 */
if (e.at) {
/** 读名字 */
let member = await Bot.getGroupMemberInfo(e.group_id, e.at);
let member = await Bot.getGroupMemberInfo(e.group_id, e.at)
let name = member.card ? member.card : member.nickname ? member.nickname : member.user_id
/** 判断是否为好友 */
let isFriend = await (e.bot ?? Bot).fl.get(e.at)
@@ -147,7 +147,7 @@ export class Fun extends plugin {
res = await new QQApi(e).thumbUp(e.at, 10)
} catch (error) {
logger.error(error)
return e.reply(error.stack)
return common.handleException(e, error)
}
logger.debug(`${e.logFnc}${e.at}点赞`, res)
if (res.code != 0) {
@@ -171,12 +171,12 @@ export class Fun extends plugin {
`\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, false, { at: true })
@@ -197,7 +197,7 @@ export class Fun extends plugin {
res = await new QQApi(e).thumbUp(e.user_id, 10)
} catch (error) {
logger.error(error)
return e.reply(error.stack)
return common.handleException(e, error)
}
logger.debug(`${e.logFnc}${e.user_id}点赞`, res)
if (res.code != 0) {
@@ -221,12 +221,12 @@ export class Fun extends plugin {
`\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, false, { at: true })
@@ -257,7 +257,7 @@ export class Fun extends plugin {
e.reply(START_EXECUTION)
await funApi.coser()
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// cos/acg搜索
@@ -268,7 +268,7 @@ export class Fun extends plugin {
const type = e.msg.match(reg)
await funApi.pandadiu(type[1], type[2])
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 黑丝
@@ -280,7 +280,7 @@ export class Fun extends plugin {
const { type, page } = heisiType[e.msg.match(/#?来点(.*)/)[1]]
await funApi.heisiwu(type, page)
.then(res => common.recallSendForwardMsg(e, _.take(res, 20)))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 萌堆
@@ -291,7 +291,7 @@ export class Fun extends plugin {
let regRet = e.msg.match(/#?来点神秘图(s)?(.*)/)
await funApi.mengdui(regRet[2], regRet[1])
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
async xiuren (e) {
@@ -300,7 +300,7 @@ export class Fun extends plugin {
e.reply(START_EXECUTION)
await funApi.xiuren(e.msg.replace(/#?来点/, ''))
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 铃声多多

View File

@@ -169,7 +169,8 @@ export class GroupAdmin extends plugin {
const time = common.translateChinaNum(regRet[3])
new Ga(e).muteMember(
e.group_id, qq ?? regRet[2], e.user_id, time, regRet[4]
).then(res => e.reply(res)).catch(err => e.reply(err.stack))
).then(res => e.reply(res))
.catch(err => common.handleException(e, err))
}
/** 解禁 */
@@ -181,7 +182,7 @@ export class GroupAdmin extends plugin {
new Ga(e).muteMember(
e.group_id, qq ?? regRet[1], e.user_id, 0
).then(res => e.reply(res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
/** 全体禁言 */
@@ -202,7 +203,7 @@ export class GroupAdmin extends plugin {
if (!qq) qq = e.msg.replace(/#|踢/g, '').trim()
new Ga(e).kickMember(e.group_id, qq, e.user_id)
.then(res => e.reply(res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 我要自闭
@@ -387,7 +388,7 @@ export class GroupAdmin extends plugin {
isxml: true,
xmlTitle: '禁言列表'
}))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 解除全部禁言
@@ -395,7 +396,7 @@ export class GroupAdmin extends plugin {
if (!common.checkPermission(e, 'admin', 'admin')) return
new Ga(e).releaseAllMute()
.then(() => e.reply('已经把全部的禁言解除辣╮( •́ω•̀)╭'))
.catch(err => err.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 查看和清理多久没发言的人
@@ -415,7 +416,7 @@ export class GroupAdmin extends plugin {
)
)
} catch (error) {
return e.reply(error.stack)
return common.handleException(e, error)
}
}
// 查看和清理都会发送列表
@@ -426,7 +427,7 @@ export class GroupAdmin extends plugin {
e.group_id, regRet[2], regRet[3], page
)
} catch (err) {
return e.reply(err.stack)
return common.handleException(e, err)
}
// 清理
if (regRet[1] == '清理') {
@@ -449,7 +450,7 @@ export class GroupAdmin extends plugin {
try {
list = await new Ga(e).getNeverSpeak(e.group_id)
} catch (error) {
return e.reply(error.stack)
return common.handleException(e, error)
}
// 确认清理直接执行
@@ -474,7 +475,7 @@ export class GroupAdmin extends plugin {
isxml: true,
xmlTitle: e.msg.replace(/#|查看|清理/g, '')
}))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 查看不活跃排行榜和入群记录

View File

@@ -154,7 +154,7 @@ export class NewGroupBannedWords extends plugin {
`处理方式:${res.penaltyType}`
])
} catch (error) {
e.reply(error.stack)
common.handleException(e, error)
}
}
@@ -167,7 +167,7 @@ export class NewGroupBannedWords extends plugin {
let msg = await GroupBannedWords.delBannedWords(e.group_id, word)
e.reply(['✅ 成功删除:', msg])
} catch (error) {
e.reply(error.stack)
common.handleException(e, error)
}
}
@@ -187,7 +187,7 @@ export class NewGroupBannedWords extends plugin {
`添加时间:${date ?? '未知'}`
])
} catch (error) {
e.reply(error.stack)
common.handleException(e, error)
}
}

View File

@@ -88,7 +88,7 @@ export class NewGroupVerify extends plugin {
try {
list = await new Ga(e).getNeverSpeak(e.group_id)
} catch (error) {
return e.reply(error.stack)
return common.handleException(e, error)
}
for (let item of list) {
await verify(item.user_id, e.group_id, e)

View File

@@ -235,7 +235,7 @@ export class NewHandle extends plugin {
logger.mark(`${e.logFnc}回复临时消息`)
return (e.bot ?? Bot).sendTempMsg(group, qq, e.message)
.then(() => { e.reply('✅ 已把消息发给它了哦~') })
.catch((err) => e.reply(`❎ 发送失败\n错误信息为:${err.stack}`))
.catch((err) => common.handleException(e, err, { MsgTemplate: '❎ 发送失败\n错误信息为:{error}' }))
}
if (!/^\d+$/.test(qq)) return e.reply('❎ QQ号不正确人家做不到的啦>_<~')
@@ -247,7 +247,7 @@ export class NewHandle extends plugin {
(e.bot ?? Bot).pickFriend(qq)
.sendMsg(e.message)
.then(() => { e.reply('✅ 已把消息发给它了哦~') })
.catch((err) => e.reply(`❎ 发送失败\n错误信息为:${err.stack}`))
.catch((err) => common.handleException(e, err, { MsgTemplate: '❎ 发送失败\n错误信息为:{error}' }))
}
// 加群员为好友

View File

@@ -43,7 +43,7 @@ export class NewPicSearch extends plugin {
: common.recallSendForwardMsg(e, res, { xmlTitle: false })
})
.catch(async err => {
await e.reply(err.stack)
await common.handleException(e, err)
if (Config.picSearch.useAscii2dWhenFailed) {
await e.reply('SauceNAO搜图出错自动使用Ascii2D进行搜索')
await this.Ascii2D(e)
@@ -56,7 +56,7 @@ export class NewPicSearch extends plugin {
if (!await this.handelImg(e, 'Ascii2D')) return
await PicSearch.Ascii2D(e.img[0])
.then(res => common.recallSendForwardMsg(e, [...res.color, ...res.bovw], { xmlTitle: false }))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
async WhatAnime (e) {
@@ -68,7 +68,7 @@ export class NewPicSearch extends plugin {
await e.reply(i)
}
})
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
async UploadSauceNAOKey (e) {

View File

@@ -93,7 +93,7 @@ export class NewPixiv extends plugin {
await e.reply(res.msg)
res.img.length == 1 ? common.recallsendMsg(e, res.img) : common.recallSendForwardMsg(e, res.img, false)
})
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// p站排行榜
@@ -109,7 +109,7 @@ export class NewPixiv extends plugin {
let page = common.translateChinaNum(regRet[6])
await Pixiv.Rank(page, regRet[2], regRet[3], regRet[4])
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
/** 关键词搜图 */
@@ -123,7 +123,7 @@ export class NewPixiv extends plugin {
let page = common.translateChinaNum(regRet[4])
await Pixiv[`${regRet[1] ? 's' : 'vilipixS'}earchTags`](regRet[2], page, !setu.getR18(e.group_id))
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
/** 获取热门tag */
@@ -132,7 +132,7 @@ export class NewPixiv extends plugin {
e.reply(Pixiv.startMsg)
await Pixiv.PopularTags()
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
/** 以uid搜图**/
@@ -146,7 +146,7 @@ export class NewPixiv extends plugin {
await Pixiv.userIllust(regRet[1], page, !setu.getR18(e.group_id))
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 随机原创插画
@@ -163,7 +163,7 @@ export class NewPixiv extends plugin {
num = common.translateChinaNum(num)
await Pixiv.vilipixRandomImg(num)
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// 相关作品
@@ -175,7 +175,7 @@ export class NewPixiv extends plugin {
let regRet = e.msg.match(/\d+/)
await Pixiv.relatedIllust(regRet[0], !setu.getR18(e.group_id))
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// p站单图
@@ -186,7 +186,7 @@ export class NewPixiv extends plugin {
await Pixiv.pximg(ispro)
.then(res => ispro ? common.recallSendForwardMsg(e, [res]) : common.recallsendMsg(e, res, false, { anony: true }))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
/** 搜索用户 */
@@ -198,7 +198,7 @@ export class NewPixiv extends plugin {
let page = common.translateChinaNum(regRet[3])
await Pixiv.searchUser(regRet[1], page, !setu.getR18(e.group_id))
.then(res => common.recallSendForwardMsg(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
/** 推荐作品 */
@@ -210,7 +210,7 @@ export class NewPixiv extends plugin {
res.length == 1
? common.recallsendMsg(e, res[0], true)
: common.recallSendForwardMsg(e, res)
}).catch(err => e.reply(err.stack))
}).catch(err => common.handleException(e, err))
}
// 更换代理
@@ -239,7 +239,7 @@ export class NewPixiv extends plugin {
async loginInfo (e) {
await Pixiv.loginInfo()
.then(res => e.reply(res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
async _Authentication (e, type = 'sese', limit = true) {

View File

@@ -2,6 +2,7 @@ import plugin from '../../../lib/plugins/plugin.js'
import _ from 'lodash'
import { puppeteer, funApi } from '../model/index.js'
import { SEARCH_MAP } from '../constants/search.js'
import common from '../lib/common/common.js'
const searchReg = new RegExp(`^#?(${_.keys(SEARCH_MAP).join('|')})搜索(.*)`)
export class NewSearch extends plugin {
@@ -50,7 +51,7 @@ export class NewSearch extends plugin {
let keyword = e.msg.replace(/#?bgg搜索/, '')
funApi.bgg(keyword)
.then(res => e.reply(res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
async bggRank (e) {

View File

@@ -60,7 +60,7 @@ export class SeSe extends plugin {
await setu.setuApi(setu.getR18(e.group_id), num)
.then(res => setu.sendMsgOrSetCd(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
// tag搜图
@@ -93,7 +93,7 @@ export class SeSe extends plugin {
await setu.setuApi(setu.getR18(e.group_id), num, tag)
.then(res => setu.sendMsgOrSetCd(e, res))
.catch(err => e.reply(err.stack))
.catch(err => common.handleException(e, err))
}
async _Authentication (e) {

View File

@@ -466,4 +466,26 @@ export default new class {
return _.every(filteredData, (value) =>
_.isPlainObject(value) ? this.checkIfEmpty(value) : _.isEmpty(value))
}
/**
* 处理异常并返回错误消息。
*
* @param {object} e - 事件对象。
* @param {Error} ErrorObj - 要检查的错误对象。
* @param {Object} options - 可选参数。
* @param {string} options.MsgTemplate - 错误消息的模板。
* @return {Porimse<import('icqq').MessageRet>|false} 如果 ErrorObj 不是 Error 的实例,则返回 false否则返回oicq消息返回值。
*/
handleException (e, ErrorObj, { MsgTemplate } = {}) {
if (!(ErrorObj instanceof Error)) return false
let ErrMsg = ''
if (ErrorObj.name == 'Error') {
ErrMsg = ErrorObj.message
} else {
ErrMsg = ErrorObj.stack
logger.error(ErrorObj)
}
ErrMsg = MsgTemplate ? MsgTemplate.replace('{error}', ErrMsg) : ErrMsg
return e.reply(ErrMsg)
}
}()

View File

@@ -66,7 +66,7 @@ export default new class {
} catch (err) {
logger.error(err)
throw Error(
`Request Get Error${err.stack.match(/reason:(.*)/i) || err.stack}`
`Request Get Error${err.message.match(/reason:(.*)/i) || err.message}`
)
}
}
@@ -121,7 +121,7 @@ export default new class {
} catch (err) {
logger.error(err)
throw Error(
`Request Post Errorreason${err.stack.match(/reason:(.*)/)[1]}`
`Request Post Errorreason${err.message.match(/reason:(.*)/)[1]}`
)
}
}

View File

@@ -47,7 +47,7 @@ export default new (class {
.then(res => res.json())
.catch(err => {
logger.error(err)
throw Error(`bika search Error${err.stack.match(/reason:(.*)/i) || err.stack}`)
throw Error(`bika search Error${err.message.match(/reason:(.*)/i) || err.message}`)
})
let { docs, total, page: pg, pages } = res.data.comics
if (total == 0) throw Error(`未找到作品,换个${type.alias[0]}试试吧`)
@@ -87,7 +87,7 @@ export default new (class {
.then((res) => res.json())
.catch(err => {
logger.error(err)
throw Error(`bika comicPage Error${err.stack.match(/reason:(.*)/i) || err.stack}`)
throw Error(`bika comicPage Error${err.message.match(/reason:(.*)/i) || err.message}`)
})
if (res.error) throw Error(res.message)
this.idNext = {
@@ -140,7 +140,7 @@ export default new (class {
.then((res) => res.json())
.catch(err => {
logger.error(err)
throw Error(`bika categories Error${err.stack.match(/reason:(.*)/i) || err.stack}`)
throw Error(`bika categories Error${err.message.match(/reason:(.*)/i) || err.message}`)
})
if (res.error) throw Error(res.message)
res = res.data.categories.filter(item => !item.isWeb)
@@ -167,7 +167,7 @@ export default new (class {
.then((res) => res.json())
.catch(err => {
logger.error(err)
throw Error(`bika comicDetail Error${err.stack.match(/reason:(.*)/i) || err.stack}`)
throw Error(`bika comicDetail Error${err.message.match(/reason:(.*)/i) || err.message}`)
})
if (res.error) throw Error(res.message)
let {

View File

@@ -134,7 +134,7 @@ export default new class Pixiv {
} else {
res = await request.get(`${this.domain}/rank`, { params }).then(res => res.json())
}
if (res.error) throw Error(res.error.stack)
if (res.error) throw Error(res.error.message)
if (_.isEmpty(res.illusts)) throw Error('暂无数据,请等待榜单更新哦(。-ω-)zzz')
let illusts = await Promise.all(res.illusts.map(async (item, index) => {
@@ -228,7 +228,7 @@ export default new class Pixiv {
} else {
res = await request.get(`${this.domain}/search`, { params }).then(res => res.json())
}
if (res.error) throw Error(res.error.stack)
if (res.error) throw Error(res.error.message)
if (_.isEmpty(res.illusts)) throw Error('宝~没有数据了哦(๑>︶<)و')
let sortIllusts = _.orderBy(res.illusts, 'total_bookmarks', 'desc')
let illusts = []
@@ -322,7 +322,7 @@ export default new class Pixiv {
res = await request.get(`${this.domain}/member_illust`, { params }).then(res => res.json())
}
if (res.error) throw Error(res.error.stack)
if (res.error) throw Error(res.error.message)
// 没有作品直接返回信息
if (_.isEmpty(res.illusts)) throw Error(page >= 2 ? '这一页没有作品辣(>人<;)' : 'Σ(っ °Д °;)っ这个淫居然没有作品')
@@ -376,7 +376,7 @@ export default new class Pixiv {
} else {
user = await request.get(`${this.domain}/search_user`, { params }).then(res => res.json())
}
if (user.error) throw Error(user.error.stack)
if (user.error) throw Error(user.error.message)
if (_.isEmpty(user.user_previews)) throw Error('呜呜呜人家没有找到这个淫d(ŐдŐ๑)')
let msg = await Promise.all(user.user_previews.slice(0, 10).map(async (item, index) => {