From b4f37b5339e2fc0e22afff36f0920ac6329f2485 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Tue, 28 Mar 2023 16:18:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=A0=E9=99=A4=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E7=9A=84=E8=AE=BE=E5=AE=9A=EF=BC=9B=E4=B8=80=E4=BA=9B=E8=AE=BE?= =?UTF-8?q?=E5=AE=9A=E5=85=B1=E4=BA=AB=E7=9A=84=E4=BC=98=E5=8C=96=E6=94=B9?= =?UTF-8?q?=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/chat.js | 40 ++++++++++++++++++++++------------------ apps/help.js | 5 +++++ apps/prompts.js | 41 ++++++++++++++++++++++++++++++++++------- index.js | 6 +++--- utils/common.js | 9 +++++++++ utils/config.js | 2 +- 6 files changed, 74 insertions(+), 29 deletions(-) diff --git a/apps/chat.js b/apps/chat.js index 61e18a93..4f2735df 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -1011,25 +1011,29 @@ export class chatgpt extends plugin { opt.toneStyle = Config.toneStyle opt.context = Config.sydneyContext if (Config.enableGroupContext && e.isGroup) { - opt.groupId = e.group_id - opt.qq = e.sender.user_id - opt.nickname = e.sender.card - opt.groupName = e.group.name - let latestChat = await e.group.getChatHistory(0, 1) - let seq = latestChat[0].seq - let chats = [] - while (chats.length < Config.groupContextLength) { - let chatHistory = await e.group.getChatHistory(seq, 20) - chats.push(...chatHistory) + try { + opt.groupId = e.group_id + opt.qq = e.sender.user_id + opt.nickname = e.sender.card + opt.groupName = e.group.name + let latestChat = await e.group.getChatHistory(0, 1) + let seq = latestChat[0].seq + let chats = [] + while (chats.length < Config.groupContextLength) { + let chatHistory = await e.group.getChatHistory(seq, 20) + chats.push(...chatHistory) + } + chats = chats.slice(0, Config.groupContextLength) + let mm = await e.group.getMemberMap() + chats.forEach(chat => { + let sender = mm.get(chat.sender.user_id) + chat.sender = sender + }) + // console.log(chats) + opt.chats = chats + } catch (err) { + logger.warn('获取群聊聊天记录失败,本次对话不携带聊天记录', err) } - chats = chats.slice(0, Config.groupContextLength) - let mm = await e.group.getMemberMap() - chats.forEach(chat => { - let sender = mm.get(chat.sender.user_id) - chat.sender = sender - }) - console.log(chats) - opt.chats = chats } response = await bingAIClient.sendMessage(prompt, opt, (token) => { reply += token diff --git a/apps/help.js b/apps/help.js index 903df6b8..6460aec5 100644 --- a/apps/help.js +++ b/apps/help.js @@ -206,6 +206,11 @@ let helpData = [ title: '#chatgpt(上传|分享|共享)设定', desc: '上传设定' }, + { + icon: 'confirm', + title: '#chatgpt(删除|取消|撤销)共享设定+设定名', + desc: '从远端删除,只能删除自己上传的设定,根据机器人主人qq号判断。' + }, { icon: 'confirm', title: '#chatgpt浏览设定(+关键词)(页码X)', diff --git a/apps/prompts.js b/apps/prompts.js index 901773d2..2f9b5213 100644 --- a/apps/prompts.js +++ b/apps/prompts.js @@ -2,7 +2,7 @@ import plugin from '../../../lib/plugins/plugin.js' import fs from 'fs' import _ from 'lodash' import { Config } from '../utils/config.js' -import { getMasterQQ, limitString, makeForwardMsg } from '../utils/common.js' +import {getMasterQQ, limitString, makeForwardMsg, maskQQ} from '../utils/common.js' import { deleteOnePrompt, getPromptByName, readPrompts, saveOnePrompt } from '../utils/prompts.js' export class help extends plugin { constructor (e) { @@ -42,6 +42,11 @@ export class help extends plugin { fnc: 'uploadPrompt', permission: 'master' }, + { + reg: '^#(chatgpt|ChatGPT)(删除|取消|撤销)共享设定', + fnc: 'removeSharePrompt', + permission: 'master' + }, { reg: '^#(chatgpt|ChatGPT)导入设定', fnc: 'importPrompt', @@ -225,10 +230,32 @@ export class help extends plugin { this.finish('addPromptContext') } + async removeSharePrompt (e) { + let master = (await getMasterQQ())[0] + let name = e.msg.replace(/^#(chatgpt|ChatGPT)(删除|取消|撤销)共享设定/, '') + let response = await fetch(`https://chatgpt.roki.best/prompt?name=${name}&qq=${master}?`, { + method: 'DELETE', + headers: { + 'FROM-CHATGPT': 'ikechan8370' + } + }) + if (response.status === 200) { + let json = await response.json() + if (json.code === 200 && json.data) { + await e.reply('已从云端删除该设定') + } else { + await e.reply('操作失败:' + json.msg) + } + } else { + await e.reply('操作失败:' + await response.text()) + } + } + async uploadPrompt (e) { if (await redis.get('CHATGPT:UPLOAD_PROMPT')) { - await this.reply('本机器人存在其他人正在上传设定,请稍后') - return + await redis.del('CHATGPT:UPLOAD_PROMPT') + // await this.reply('本机器人存在其他人正在上传设定,请稍后') + // return } let use = await redis.get('CHATGPT:USE') || 'api' if (use.toLowerCase() === 'bing') { @@ -240,7 +267,7 @@ export class help extends plugin { if (!currentUse) { currentUse = await redis.get(`CHATGPT:PROMPT_USE_${use}`) } - await this.reply(`即将上传设定${currentUse},确定请回复确定,取消请回复取消,或者回复其他本地存在设定的名字`, true) + await this.reply(`即将向云端上传设定${currentUse},确定请回复确定,取消请回复取消,或者回复其他本地存在设定的名字`, true) let extraData = { currentUse, use @@ -268,7 +295,7 @@ export class help extends plugin { this.finish('uploadPromptConfirm') return } - await redis.set('CHATGPT:UPLOAD_PROMPT', JSON.stringify(extraData), 300) + // await redis.set('CHATGPT:UPLOAD_PROMPT', JSON.stringify(extraData), 300) await this.reply('请输入对该设定的描述或备注,便于其他人快速了解该设定', true) this.finish('uploadPromptConfirm') this.setContext('uploadPromptDescription') @@ -278,7 +305,7 @@ export class help extends plugin { if (!this.e.msg) return let description = this.e.msg.trim() if (description === '取消') { - await redis.del('CHATGPT:UPLOAD_PROMPT') + // await redis.del('CHATGPT:UPLOAD_PROMPT') await this.reply('已取消上传', true) this.finish('uploadPromptDescription') return @@ -356,7 +383,7 @@ export class help extends plugin { let output = '| 【设定名称】 | 上传者QQ | 上传时间 | 是否R18 | 使用场景 |\n' output += '----------------------------------------------------------------------------------------\n' content.forEach(c => { - output += `| 【${c.title}】 | ${c.qq} | ${c.createTime} | ${c.r18} | ${c.use}|\n` + output += `| 【${c.title}】 | ${maskQQ(c.qq)} | ${c.createTime} | ${c.r18} | ${c.use}|\n` }) output += '**************************************************************************\n' output += ` 当前为第${pageable.pageNumber + 1}页,共${totalElements}个设定` diff --git a/index.js b/index.js index a128683d..905abfbc 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ import fs from 'node:fs' -import {Config} from "./utils/config.js"; - +import { Config } from './utils/config.js' + if (!global.segment) { - global.segment = (await import("oicq")).segment + global.segment = (await import('oicq')).segment } const files = fs.readdirSync('./plugins/chatgpt-plugin/apps').filter(file => file.endsWith('.js')) diff --git a/utils/common.js b/utils/common.js index 670c3887..5a0a3071 100644 --- a/utils/common.js +++ b/utils/common.js @@ -477,3 +477,12 @@ export function wrapTextByLanguage (text) { } // console.log(wrapTextByLanguage('你好,这里是哈哈,こんにちは,Hello!')) + +export function maskQQ (qq) { + if (!qq) { + return '未知' + } + let len = qq.length // QQ号长度 + let newqq = qq.slice(0, 3) + '*'.repeat(len - 7) + qq.slice(len - 3) // 替换中间3位为* + return newqq +} diff --git a/utils/config.js b/utils/config.js index 89ff20c3..13512579 100644 --- a/utils/config.js +++ b/utils/config.js @@ -72,7 +72,7 @@ const defaultConfig = { emojiBaseURL: 'https://www.gstatic.com/android/keyboard/emojikitchen', enableGroupContext: false, groupContextLength: 50, - version: 'v2.4.2' + version: 'v2.4.3' } const _path = process.cwd() let config = {}