新增 go-cqhttp 请求处理,细节优化

This commit is contained in:
🌌 2023-07-27 11:57:24 +08:00
parent f20abbe767
commit cdf02345bd
7 changed files with 76 additions and 73 deletions

View File

@ -161,8 +161,8 @@ class PluginsLoader {
/** /**
* 处理事件 * 处理事件
* *
* 参数文档 https://oicqjs.github.io/oicq/interfaces/GroupMessageEvent.html * 参数文档 https://github.com/TimeRainStarSky/Yunzai/tree/docs
* @param e icqq Events * @param e 事件
*/ */
async deal(e) { async deal(e) {
/** 检查黑白名单 */ /** 检查黑白名单 */

View File

@ -22,7 +22,7 @@ export default class plugin {
this.name = name this.name = name
/** 插件描述 */ /** 插件描述 */
this.dsc = dsc this.dsc = dsc
/** 监听事件默认message https://oicqjs.github.io/oicq/#events */ /** 监听事件默认message */
this.event = event this.event = event
/** 优先级 */ /** 优先级 */
this.priority = priority this.priority = priority

View File

@ -437,6 +437,23 @@ Bot.adapter.push(new class gocqhttpAdapter {
} }
} }
setFriendAddRequest(data, flag, approve, remark) {
return data.sendApi("set_friend_add_request", {
flag,
approve,
remark,
})
}
setGroupAddRequest(data, flag, sub_type, approve, reason) {
return data.sendApi("set_group_add_request", {
flag,
sub_type,
approve,
reason,
})
}
pickFriend(data, user_id) { pickFriend(data, user_id) {
const i = { const i = {
...Bot[data.self_id].fl.get(user_id), ...Bot[data.self_id].fl.get(user_id),
@ -568,6 +585,11 @@ Bot.adapter.push(new class gocqhttpAdapter {
getGroupArray: () => this.getGroupArray(data), getGroupArray: () => this.getGroupArray(data),
getGroupList: () => this.getGroupList(data), getGroupList: () => this.getGroupList(data),
getGroupMap: () => this.getGroupMap(data), getGroupMap: () => this.getGroupMap(data),
request_list: [],
getSystemMsg: () => Bot[data.self_id].request_list,
setFriendAddRequest: (flag, approve, remark) => this.setFriendAddRequest(data, flag, approve, remark),
setGroupAddRequest: (flag, sub_type, approve, reason) => this.setGroupAddRequest(data, flag, sub_type, approve, reason),
} }
Bot[data.self_id].info = (await data.sendApi("get_login_info")).data Bot[data.self_id].info = (await data.sendApi("get_login_info")).data
@ -761,19 +783,23 @@ Bot.adapter.push(new class gocqhttpAdapter {
makeRequest(data) { makeRequest(data) {
switch (data.request_type) { switch (data.request_type) {
case "friend": case "friend":
logger.info(`${logger.blue(`[${data.self_id}]`)} 加好友请求:[${data.user_id}] ${data.comment} ${data.flag}`) logger.info(`${logger.blue(`[${data.self_id}]`)} 加好友请求:[${data.user_id}] ${data.comment}(${data.flag})`)
data.sub_type = "add"
data.friend = data.bot.pickFriend(data.user_id) data.friend = data.bot.pickFriend(data.user_id)
data.approve = approve => data.bot.setFriendAddRequest(data.flag, approve)
break break
case "group": case "group":
logger.info(`${logger.blue(`[${data.self_id}]`)} 加群请求:[${data.group_id}, ${data.user_id}] ${data.sub_type} ${data.comment} ${data.flag}`) logger.info(`${logger.blue(`[${data.self_id}]`)} 加群请求:[${data.group_id}, ${data.user_id}] ${data.sub_type} ${data.comment}(${data.flag})`)
data.friend = data.bot.pickFriend(data.user_id) data.friend = data.bot.pickFriend(data.user_id)
data.group = data.bot.pickGroup(data.group_id) data.group = data.bot.pickGroup(data.group_id)
data.member = data.group.pickMember(data.user_id) data.member = data.group.pickMember(data.user_id)
data.approve = approve => data.bot.setGroupAddRequest(data.flag, data.sub_type, approve)
break break
default: default:
logger.warn(`${logger.blue(`[${data.self_id}]`)} 未知请求:${logger.magenta(JSON.stringify(data))}`) logger.warn(`${logger.blue(`[${data.self_id}]`)} 未知请求:${logger.magenta(JSON.stringify(data))}`)
} }
data.bot.request_list.push(data)
if (data.sub_type) if (data.sub_type)
Bot.emit(`${data.post_type}.${data.request_type}.${data.sub_type}`, data) Bot.emit(`${data.post_type}.${data.request_type}.${data.sub_type}`, data)
Bot.emit(`${data.post_type}.${data.request_type}`, data) Bot.emit(`${data.post_type}.${data.request_type}`, data)

View File

@ -124,4 +124,4 @@ export class install extends plugin {
restart() { restart() {
new Restart(this.e).restart() new Restart(this.e).restart()
} }
} }

View File

@ -19,11 +19,11 @@ export class update extends plugin {
priority: 4000, priority: 4000,
rule: [ rule: [
{ {
reg: '^#更新日志$', reg: '^#更新日志',
fnc: 'updateLog' fnc: 'updateLog'
}, },
{ {
reg: '^#(强制)*更新(.*)', reg: '^#(强制)?更新',
fnc: 'update' fnc: 'update'
}, },
{ {
@ -39,21 +39,14 @@ export class update extends plugin {
async update() { async update() {
if (!this.e.isMaster) return false if (!this.e.isMaster) return false
if (uping) { if (uping) return this.reply('已有命令更新中..请勿重复操作')
await this.reply('已有命令更新中..请勿重复操作')
return
}
if (/详细|详情|面板|面版/.test(this.e.msg)) return false if (/详细|详情|面板|面版/.test(this.e.msg)) return false
/** 获取插件 */ /** 获取插件 */
let plugin = this.getPlugin() const plugin = this.getPlugin()
if (plugin === false) return false if (plugin === false) return false
/** 检查git安装 */
if (!await this.checkGit()) return
/** 执行更新 */ /** 执行更新 */
await this.runUpdate(plugin) await this.runUpdate(plugin)
@ -64,19 +57,9 @@ export class update extends plugin {
} }
} }
async checkGit() {
let ret = await execSync('git --version', { encoding: 'utf-8' })
if (!ret || !ret.includes('git version')) {
await this.reply('请先安装git')
return false
}
return true
}
getPlugin(plugin = '') { getPlugin(plugin = '') {
if (!plugin) { if (!plugin) {
plugin = this.e.msg.replace(/#|更新|强制/g, '') plugin = this.e.msg.replace(/#(强制)?更新(日志)?/, '')
if (!plugin) return '' if (!plugin) return ''
} }
@ -108,14 +91,13 @@ export class update extends plugin {
if (plugin) if (plugin)
cm = `cd "plugins/${plugin}" && ${cm}` cm = `cd "plugins/${plugin}" && ${cm}`
this.oldCommitId = await this.getcommitId(plugin) this.oldCommitId = await this.getcommitId(plugin)
logger.mark(`${this.e.logFnc} 开始${type}${this.typeName}`) logger.mark(`${this.e.logFnc} 开始${type}${this.typeName}`)
await this.reply(`开始#${type}${this.typeName}`) await this.reply(`开始${type} ${this.typeName}`)
uping = true uping = true
let ret = await this.execSync(cm) const ret = await this.execSync(cm)
uping = false uping = false
if (ret.error) { if (ret.error) {
@ -124,39 +106,33 @@ export class update extends plugin {
return false return false
} }
let time = await this.getTime(plugin) const time = await this.getTime(plugin)
if (/Already up|已经是最新/g.test(ret.stdout)) { if (/Already up|已经是最新/g.test(ret.stdout)) {
await this.reply(`${this.typeName}是最新\n最后更新时间:${time}`) await this.reply(`${this.typeName} 已是最新\n最后更新时间:${time}`)
} else { } else {
await this.reply(`${this.typeName}更新成功\n更新时间:${time}`) await this.reply(`${this.typeName} 更新成功\n更新时间:${time}`)
this.isUp = true this.isUp = true
let log = await this.getLog(plugin) await this.reply(await this.getLog(plugin))
await this.reply(log)
} }
logger.mark(`${this.e.logFnc} 最后更新时间:${time}`) logger.mark(`${this.e.logFnc} 最后更新时间:${time}`)
return true return true
} }
async getcommitId(plugin = '') { async getcommitId(plugin = '') {
let cm = 'git rev-parse --short HEAD' let cm = 'git rev-parse --short HEAD'
if (plugin) { if (plugin)
cm = `cd "plugins/${plugin}" && git rev-parse --short HEAD` cm = `cd "plugins/${plugin}" && git rev-parse --short HEAD`
}
let commitId = await execSync(cm, { encoding: 'utf-8' }) const commitId = await execSync(cm, { encoding: 'utf-8' })
commitId = lodash.trim(commitId) return lodash.trim(commitId)
return commitId
} }
async getTime(plugin = '') { async getTime(plugin = '') {
let cm = 'git log -1 --pretty=format:"%cd" --date=format:"%F %T"' let cm = 'git log -1 --pretty=format:"%cd" --date=format:"%F %T"'
if (plugin) { if (plugin)
cm = `cd "plugins/${plugin}" && git log -1 --pretty=format:"%cd" --date=format:"%F %T"` cm = `cd "plugins/${plugin}" && git log -1 --pretty=format:"%cd" --date=format:"%F %T"`
}
let time = '' let time = ''
try { try {
@ -171,37 +147,33 @@ export class update extends plugin {
} }
async gitErr(err, stdout) { async gitErr(err, stdout) {
let msg = '更新失败!' const msg = '更新失败!'
let errMsg = err.toString() const errMsg = err.toString()
stdout = stdout.toString() stdout = stdout.toString()
if (errMsg.includes('Timed out')) { if (errMsg.includes('Timed out')) {
let remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '') const remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '')
await this.reply(msg + `\n连接超时:${remote}`) return this.reply(`${msg}\n连接超时:${remote}`)
return
} }
if (/Failed to connect|unable to access/g.test(errMsg)) { if (/Failed to connect|unable to access/g.test(errMsg)) {
let remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '') const remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '')
await this.reply(msg + `\n连接失败:${remote}`) return this.reply(`${msg}\n连接失败:${remote}`)
return
} }
if (errMsg.includes('be overwritten by merge')) { if (errMsg.includes('be overwritten by merge')) {
await this.reply(msg + `存在冲突:\n${errMsg}\n` + '请解决冲突后再更新,或者执行#强制更新,放弃本地修改') return this.reply(`${msg}\n存在冲突:\n${errMsg}\n请解决冲突后再更新,或者执行#强制更新,放弃本地修改`)
return
} }
if (stdout.includes('CONFLICT')) { if (stdout.includes('CONFLICT')) {
await this.reply([msg + '存在冲突\n', errMsg, stdout, '\n请解决冲突后再更新或者执行#强制更新,放弃本地修改']) return this.reply(`${msg}\n存在冲突:\n${errMsg}${stdout}\n请解决冲突后再更新,或者执行#强制更新,放弃本地修改`)
return
} }
await this.reply([errMsg, stdout]) return this.reply([errMsg, stdout])
} }
async updateAll() { async updateAll() {
let dirs = fs.readdirSync('./plugins/') const dirs = fs.readdirSync('./plugins/')
await this.runUpdate() await this.runUpdate()
@ -223,17 +195,16 @@ export class update extends plugin {
} }
async getLog(plugin = '') { async getLog(plugin = '') {
let cm = 'git log -20 --pretty=format:"%h||[%cd] %s" --date=format:"%F %T"' let cm = 'git log -100 --pretty=format:"%h||[%cd] %s" --date=format:"%F %T"'
if (plugin) { if (plugin)
cm = `cd "plugins/${plugin}" && ${cm}` cm = `cd "plugins/${plugin}" && ${cm}`
}
let logAll let logAll
try { try {
logAll = await execSync(cm, { encoding: 'utf-8' }) logAll = await execSync(cm, { encoding: 'utf-8' })
} catch (error) { } catch (error) {
logger.error(error.toString()) logger.error(error.toString())
this.reply(error.toString()) await this.reply(error.toString())
} }
if (!logAll) return false if (!logAll) return false
@ -248,19 +219,25 @@ export class update extends plugin {
log.push(str[1]) log.push(str[1])
} }
let line = log.length let line = log.length
log = log.join('\n\n') log = log.join('\n')
if (log.length <= 0) return '' if (log.length <= 0) return ''
let end = '' let end = ''
try {
end = await execSync('git config -l', { encoding: 'utf-8' })
end = end.match(/remote\..*\.url=.+/g).join('\n').replace(/remote\..*\.url=/g, '')
} catch (error) {
logger.error(error.toString())
await this.reply(error.toString())
}
log = await common.makeForwardMsg(this.e, [log, end], `${plugin || 'TRSS-Yunzai'}更新日志,共${line}`) return common.makeForwardMsg(this.e, [log, end], `${plugin || 'TRSS-Yunzai'} 更新日志,共${line}`)
return log
} }
async updateLog() { async updateLog() {
let log = await this.getLog() const plugin = this.getPlugin()
await this.reply(log) if (plugin === false) return false
return this.reply(await this.getLog(plugin))
} }
} }

View File

@ -223,7 +223,7 @@ export class add extends plugin {
fs.writeFileSync(`${this.path}${file.name}`, file.buffer) fs.writeFileSync(`${this.path}${file.name}`, file.buffer)
return file.name return file.name
} }
return url return data.url
} }
async getMessage() { async getMessage() {

View File

@ -10,7 +10,7 @@ export class friend extends plugin {
}) })
} }
async accept () { async accept() {
if (this.e.sub_type == 'add' || this.e.sub_type == 'single') { if (this.e.sub_type == 'add' || this.e.sub_type == 'single') {
if (cfg.other.autoFriend == 1) { if (cfg.other.autoFriend == 1) {
logger.mark(`[自动同意][添加好友] ${this.e.user_id}`) logger.mark(`[自动同意][添加好友] ${this.e.user_id}`)
@ -19,4 +19,4 @@ export class friend extends plugin {
} }
} }
} }
} }