diff --git a/apps/assistant.js b/apps/assistant.js index 2aba356..2bf772b 100644 --- a/apps/assistant.js +++ b/apps/assistant.js @@ -111,13 +111,16 @@ export class Assistant extends plugin { */ async SetAvatar(e) { if (!common.checkPermission(e, "master")) return - if (!e.img) { + + const sourceImg = await common.takeSourceMsg(e, { img: true }) + + if (!e.img && !sourceImg) { this.setContext("_avatarContext") e.reply("⚠ 请发送图片") return } - await this.Bot.setAvatar(e.img[0]) + await this.Bot.setAvatar(sourceImg?.[0] || e.img[0]) .then(() => e.reply("✅ 头像修改成功")) .catch((err) => { e.reply("❎ 头像修改失败") @@ -204,29 +207,27 @@ export class Assistant extends plugin { */ async SetGroupAvatar(e) { if (e.isPrivate) { - if (!common.checkPermission(e, "admin", "admin")) return e.group_id = e.msg.replace(/#(改|换)群头像/g, "").trim() if (!e.group_id) return e.reply("❎ 群号不能为空") if (!(/^\d+$/.test(e.group_id))) return e.reply("❎ 您的群号不合法") - if (!this.Bot.gl.get(Number(e.group_id))) return e.reply("❎ 群聊列表查无此群") e.group_id = Number(e.group_id) - } else if (!e.member.is_admin && !e.member.is_owner && !e.isMaster) { - return logger.mark(`${e.logFnc}该群员权限不足`) + + if (!this.Bot.gl.get(e.group_id)) return e.reply("❎ 群聊列表查无此群") } - let groupObj = this.Bot.pickGroup(e.group_id) - if (!groupObj.is_admin && !groupObj.is_owner) { - return e.reply("❎ 没有管理员人家做不到啦~>_<") - } - if (!e.img) { + if (!common.checkPermission(e, "admin", "admin")) return + + const sourceImg = await common.takeSourceMsg(e, { img: true }) + + if (!sourceImg && !e.img) { this.setContext("_GroupAvatarContext") e.reply("⚠ 请发送图片") return } - this.Bot.pickGroup(e.group_id).setAvatar(e.img[0]) + this.Bot.pickGroup(e.group_id).setAvatar(sourceImg?.[0] || e.img[0]) .then(() => e.reply("✅ 群头像修改成功")) .catch((err) => { e.reply("✅ 群头像修改失败") @@ -415,23 +416,8 @@ export class Assistant extends plugin { * @param e */ async ImageLink(e) { - let img = [] - if (e.source) { - let source - if (e.isGroup) { - source = (await e.group.getChatHistory(e.source.seq, 1)).pop() - } else { - source = (await e.friend.getChatHistory(e.source.time, 1)).pop() - } - for (let i of source.message) { - if (i.type == "image") { - img.push(i.url) - } - } - } else { - img = e.img - } - + const sourceImg = await common.takeSourceMsg(e, { img: true }) + const img = sourceImg || e.img if (_.isEmpty(img)) { this.setContext("_ImageLinkContext") await this.reply("⚠ 请发送图片") @@ -527,12 +513,7 @@ export class Assistant extends plugin { // 引用撤回 async RecallMsgown(e) { if (!e.source) return false - let source - if (e.isGroup) { - source = (await e.group.getChatHistory(e.source.seq, 1)).pop() - } else { - source = (await e.friend.getChatHistory(e.source.time, 1)).pop() - } + const source = await common.takeSourceMsg(e) let target = e.isGroup ? e.group : e.friend if (e.isGroup) { diff --git a/apps/picSearch.js b/apps/picSearch.js index dd38dcd..49f4706 100644 --- a/apps/picSearch.js +++ b/apps/picSearch.js @@ -99,15 +99,7 @@ export class NewPicSearch extends plugin { } async handelImg(e, funName) { - if (e.source) { - let source - if (e.isGroup) { - source = (await e.group.getChatHistory(e.source.seq, 1)).pop() - } else { - source = (await e.friend.getChatHistory(e.source.time, 1)).pop() - } - e.img = [ source.message.find(item => item.type == "image")?.url ] - } + e.img = e.img || await common.takeSourceMsg(e, { img: true }) if (!_.isEmpty(e.img)) return true e.sourceFunName = funName this.setContext("MonitorImg") @@ -115,11 +107,11 @@ export class NewPicSearch extends plugin { return false } - async MonitorImg() { + async MonitorImg(e) { if (!this.e.img) { this.e.reply("❎ 未检测到图片操作已取消") } else { - this[this.getContext().MonitorImg.sourceFunName](this.e) + this[e.sourceFunName](this.e) } this.finish("MonitorImg") } diff --git a/lib/common/common.js b/lib/common/common.js index 8268822..8fbd0d6 100644 --- a/lib/common/common.js +++ b/lib/common/common.js @@ -142,7 +142,7 @@ export default new class extends sendMsgMod { * @param {Array} omits - 需要忽略的属性列表。默认为空数组,表示不忽略任何属性。 * @returns {boolean} - 如果对象或数组中的所有值都是空值,则返回 true;否则返回 false。 */ - checkIfEmpty(data, omits) { + checkIfEmpty(data, omits = []) { const filteredData = _.omit(data, omits) return _.every(filteredData, (value) => _.isPlainObject(value) ? this.checkIfEmpty(value) : _.isEmpty(value)) @@ -168,6 +168,30 @@ export default new class extends sendMsgMod { ErrMsg = MsgTemplate ? MsgTemplate.replace(/{error}/g, ErrMsg) : ErrMsg return e.reply(ErrMsg) } + + async takeSourceMsg(e, { img } = {}) { + let source = "" + if (e.getReply) { + source = await e.getReply() + } else if (e.source) { + if (e.group?.getChatHistory) { + source = (await e.group.getChatHistory(e.source.seq, 1)).pop() + } else if (e.friend?.getChatHistory) { + source = (await e.friend.getChatHistory(e.source.time, 1)).pop() + } + } + if (!source) return false + if (img) { + let imgArr = [] + for (let i of source.message) { + if (i.type == "image") { + imgArr.push(i.url) + } + } + return !_.isEmpty(imgArr) && imgArr + } + return source + } }() let a = []