diff --git a/apps/entertainment.js b/apps/entertainment.js index 9a94945..17b4fdd 100644 --- a/apps/entertainment.js +++ b/apps/entertainment.js @@ -57,6 +57,9 @@ export class Entertainment extends plugin { } let toSend = Config.initiativeChatGroups || [] for (let i = 0; i < toSend.length; i++) { + if (!toSend[i]) { + continue + } let groupId = parseInt(toSend[i]) if (Bot.getGroupList().get(groupId)) { // 5%的概率打招呼 diff --git a/apps/help.js b/apps/help.js index f115be7..8361f84 100644 --- a/apps/help.js +++ b/apps/help.js @@ -154,15 +154,55 @@ let helpData = [ { icon: 'eat', title: '#chatgpt设置(API|Sydney)设定', - desc: '设置AI的风格设定' + desc: '设置AI的默认风格设定' }, { icon: 'eat', title: '#chatgpt查看(API|Sydney)设定', - desc: '查看AI的风格设定,文本形式返回,设定太长可能发不出来' + desc: '查看AI当前的风格设定,文本形式返回,设定太长可能发不出来' } ] }, + { + group: '设定', + list: [ + { + icon: 'smiley-wink', + title: '#chatgpt设定列表', + desc: '查看所有设定列表,以转发消息形式' + }, + { + icon: 'eat', + title: '#chatgpt查看设定【设定名】', + desc: '查看指定名字的设定内容。其中API默认和Sydney默认为锅巴面板配置的设定' + }, + { + icon: 'coin', + title: '#chatgpt添加设定', + desc: '添加一个设定,分此输入设定名称和设定内容' + }, + { + icon: 'switch', + title: '#chatgpt使用设定【设定名】', + desc: '使用某个设定' + }, + { + icon: 'confirm', + title: '#chatgpt(上传|分享|共享)设定', + desc: '敬请期待' + }, + { + icon: 'confirm', + title: '#chatgpt导入设定', + desc: '敬请期待' + }, + { + icon: 'help', + title: '#chatgpt设定帮助', + desc: '设定帮助' + }, + ] + }, { group: '其他', list: [ diff --git a/apps/prompts.js b/apps/prompts.js new file mode 100644 index 0000000..1fade88 --- /dev/null +++ b/apps/prompts.js @@ -0,0 +1,161 @@ +import plugin from '../../../lib/plugins/plugin.js' +import fs from 'fs' +import _ from 'lodash' +import { Config } from '../utils/config.js' +import { makeForwardMsg } from '../utils/common.js' +import { getPromptByName, readPrompts, saveOnePrompt } from '../utils/prompts.js' +export class help extends plugin { + constructor (e) { + super({ + name: 'ChatGPT-Plugin 设定管理', + dsc: 'ChatGPT-Plugin 设定管理', + event: 'message', + priority: 500, + rule: [ + { + reg: '^#(chatgpt|ChatGPT)设定列表$', + fnc: 'listPrompts' + }, + { + reg: '^#(chatgpt|ChatGPT)查看设定', + fnc: 'detailPrompt' + }, + { + reg: '^#(chatgpt|ChatGPT)使用设定', + fnc: 'usePrompt' + }, + { + reg: '^#(chatgpt|ChatGPT)添加设定', + fnc: 'addPrompt' + }, + { + reg: '^#(chatgpt|ChatGPT)(上传|分享|共享)设定', + fnc: 'uploadPrompt' + }, + { + reg: '^#(chatgpt|ChatGPT)导入设定', + fnc: 'importPrompt' + }, + { + reg: '^#(chatgpt|ChatGPT)设定帮助$', + fnc: 'helpPrompt' + } + ] + }) + } + + async listPrompts (e) { + let prompts = [] + let defaultPrompt = { + name: 'API默认', + content: Config.promptPrefixOverride + } + let defaultSydneyPrompt = { + name: 'Sydney默认', + content: Config.sydney + } + prompts.push(...[defaultPrompt, defaultSydneyPrompt]) + prompts.push(...readPrompts()) + e.reply(await makeForwardMsg(e, prompts.map(p => `《${p.name}》\n${p.content}`), '设定列表')) + } + + async detailPrompt (e) { + let promptName = e.msg.replace(/^#(chatgpt|ChatGPT)查看设定/, '').trim() + let prompt = getPromptByName(promptName) + if (!prompt) { + if (promptName === 'API默认') { + prompt = { + name: 'API默认', + content: Config.promptPrefixOverride + } + } else if (promptName === 'Sydney默认') { + prompt = { + name: 'Sydney默认', + content: Config.sydney + } + } else { + await e.reply('没有这个设定', true) + return + } + } + await e.reply(`《${prompt.name}》\n${prompt.content}`, true) + } + + async usePrompt (e) { + let promptName = e.msg.replace(/^#(chatgpt|ChatGPT)使用设定/, '').trim() + let prompt = getPromptByName(promptName) + if (!prompt) { + console.log(promptName) + if (promptName === 'API默认') { + prompt = { + name: 'API默认', + content: Config.promptPrefixOverride + } + } else if (promptName === 'Sydney默认') { + prompt = { + name: 'Sydney默认', + content: Config.sydney + } + } else { + await e.reply('没有这个设定', true) + return + } + } + let use = await redis.get('CHATGPT:USE') || 'api' + const keyMap = { + api: 'promptPrefixOverride', + sydney: 'sydney' + } + + await redis.set(`CHATGPT:PROMPT_USE_${use}`, promptName) + + if (keyMap[use]) { + Config[keyMap[use]] = prompt.content + await e.reply(`你当前正在使用${use}模式,已将该模式设定应用为"${promptName}。更该设定后建议结束对话以使设定更好生效"`, true) + } else { + await e.reply(`你当前正在使用${use}模式,改模式不支持设定`, true) + } + } + + async addPrompt (e) { + this.setContext('addPromptName') + await e.reply('请输入设定名称', true) + } + + async addPromptName () { + if (!this.e.msg) return + let name = this.e.msg + let prompt = getPromptByName(name) + if (prompt) { + await this.e.reply('该设定已存在', true) + this.finish('addPromptName') + return + } + await redis.set('CHATGPT:ADD_PROMPT_NAME', name) + await this.reply('请输入设定内容', true) + this.finish('addPromptName') + this.setContext('addPromptContext') + } + + async addPromptContext () { + if (!this.e.msg) return + let content = this.e.msg + let name = await redis.get('CHATGPT:ADD_PROMPT_NAME') + saveOnePrompt(name, content) + await redis.del('CHATGPT:ADD_PROMPT_NAME') + await this.reply('设定添加成功', true) + this.finish('addPromptContext') + } + + async uploadPrompt () { + await this.reply('敬请期待', true) + } + + async importPrompt () { + await this.reply('敬请期待', true) + } + + async helpPrompt () { + await this.reply('设定目录为/plugins/chatgpt-plugin/prompts,将会读取该目录下的所有[设定名].txt文件作为设定列表', true) + } +} diff --git a/utils/config.js b/utils/config.js index 809934e..9696ad7 100644 --- a/utils/config.js +++ b/utils/config.js @@ -60,7 +60,7 @@ const defaultConfig = { initiativeChatGroups: [], enableDraw: true, helloPrompt: '写一段话让大家来找我聊天。类似于“有人找我聊天吗?"这种风格,轻松随意一点控制在20个字以内', - version: 'v2.1.6' + version: 'v2.1.7' } const _path = process.cwd() let config = {} diff --git a/utils/prompts.js b/utils/prompts.js new file mode 100644 index 0000000..0250bb7 --- /dev/null +++ b/utils/prompts.js @@ -0,0 +1,42 @@ +import _ from 'lodash' +import fs from 'fs' +import {mkdirs} from "./common.js"; +export function readPrompts () { + const _path = process.cwd() + let prompts = [] + if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/prompts`)) { + if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/prompts`)) { + const files = fs.readdirSync(`${_path}/plugins/chatgpt-plugin/prompts`) + const txtFiles = files.filter(file => file.endsWith('.txt')) + txtFiles.forEach(txtFile => { + let name = _.trimEnd(txtFile, '.txt') + const content = fs.readFileSync(`${_path}/plugins/chatgpt-plugin/prompts/${txtFile}`, 'utf8') + prompts.push({ + name, + content + }) + }) + } + } + return prompts +} + +export function getPromptByName (name) { + if (!name) { + return null + } + let prompts = readPrompts() + let hits = prompts.filter(p => p.name.trim() === name.trim()) + if (hits && hits.length > 0) { + return hits[0] + } else { + return null + } +} + +export function saveOnePrompt (name, content) { + const _path = process.cwd() + mkdirs(`${_path}/plugins/chatgpt-plugin/prompts`) + let filePath = `${_path}/plugins/chatgpt-plugin/prompts/${name}.txt` + fs.writeFileSync(filePath, content) +}