Merge branch 'ikechan8370:main' into main

This commit is contained in:
夏叶 2022-12-09 14:32:31 +08:00 committed by GitHub
commit 55816b8ccd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 212 additions and 35 deletions

View file

@ -30,7 +30,8 @@ export class chatgpt extends plugin {
},
{
reg: 'chatgpt对话列表',
fnc: 'getConversations'
fnc: 'getConversations',
permission: 'master'
},
{
reg: '^#结束对话([\s\S]*)',
@ -56,19 +57,19 @@ export class chatgpt extends plugin {
*/
async getConversations (e) {
let keys = await redis.keys('CHATGPT:CONVERSATIONS:*')
if (!keys || keys.length === 0) {
await this.reply('当前没有人正在与机器人对话', true)
} else {
let response = '当前对话列表:(格式为【开始时间 qq昵称 对话长度 最后活跃时间】)\n'
await Promise.all(keys.map(async (key) => {
let conversation = await redis.get(key)
if (conversation) {
conversation = JSON.parse(conversation)
response += `${conversation.ctime} ${conversation.sender.nickname} ${conversation.num} ${conversation.utime} \n`
if (!keys || keys.length === 0) {
await this.reply('当前没有人正在与机器人对话', true)
} else {
let response = '当前对话列表:(格式为【开始时间 qq昵称 对话长度 最后活跃时间】)\n'
await Promise.all(keys.map(async (key) => {
let conversation = await redis.get(key)
if (conversation) {
conversation = JSON.parse(conversation)
response += `${conversation.ctime} ${conversation.sender.nickname} ${conversation.num} ${conversation.utime} \n`
}
}))
await this.reply(`${response}`, true)
}
}))
await this.reply(`${response}`, true)
}
}
/**
@ -102,10 +103,12 @@ export class chatgpt extends plugin {
async help (e) {
let response = 'chatgpt-plugin使用帮助文字版\n' +
'#chatgpt+聊天内容: 发起对话与AI进行聊天\n' +
'chatgpt对话列表: 查看当前发起的对话\n' +
'#结束对话: 结束自己或@用户的对话'
await this.reply(response)
'#chatgpt+聊天内容: 发起对话与AI进行聊天\n' +
'chatgpt对话列表: 查看当前发起的对话\n' +
'#结束对话: 结束自己或@用户的对话\n' +
'chatgpt帮助: 查看本帮助\n' +
'源代码https://github.com/ikechan8370/chatgpt-plugin'
await this.reply(response)
}
/**
@ -121,18 +124,20 @@ export class chatgpt extends plugin {
let previousConversation = await redis.get(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
if (!previousConversation) {
c = this.chatGPTApi.getConversation()
let ctime = new Date()
previousConversation = {
sender: e.sender,
conversation: c,
ctime: new Date(),
utime: new Date(),
ctime,
utime: ctime,
num: 0
}
await redis.set(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`, JSON.stringify(previousConversation), { EX: CONVERSATION_PRESERVE_TIME })
} else {
previousConversation = JSON.parse(previousConversation)
c = this.chatGPTApi.getConversation({
conversationId: JSON.parse(previousConversation).conversation.conversationId,
parentMessageId: JSON.parse(previousConversation).conversation.parentMessageId
conversationId: previousConversation.conversation.conversationId,
parentMessageId: previousConversation.conversation.parentMessageId
})
}
try {
@ -156,10 +161,10 @@ export class chatgpt extends plugin {
}
/** 最后回复消息 */
await this.reply(`${response}`, true)
await this.reply(`${response}`, e.isGroup)
} catch (e) {
logger.error(e)
await this.reply(`与OpenAI通信异常请稍后重试${e}`, true)
await this.reply(`与OpenAI通信异常请稍后重试${e}`, e.isGroup, { recallMsg: e.isGroup ? 10 : 0 })
}
}
}