Skip to content

Commit

Permalink
fix: 删除上传的设定;一些设定共享的优化改进
Browse files Browse the repository at this point in the history
  • Loading branch information
ikechan8370 committed Mar 28, 2023
1 parent b11a653 commit b4f37b5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 29 deletions.
40 changes: 22 additions & 18 deletions apps/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions apps/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ let helpData = [
title: '#chatgpt(上传|分享|共享)设定',
desc: '上传设定'
},
{
icon: 'confirm',
title: '#chatgpt(删除|取消|撤销)共享设定+设定名',
desc: '从远端删除,只能删除自己上传的设定,根据机器人主人qq号判断。'
},
{
icon: 'confirm',
title: '#chatgpt浏览设定(+关键词)(页码X)',
Expand Down
41 changes: 34 additions & 7 deletions apps/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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') {
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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}个设定`
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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'))
Expand Down
9 changes: 9 additions & 0 deletions utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down

0 comments on commit b4f37b5

Please sign in to comment.