mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 13:27:08 +00:00
增加语音合成失败处理。 (#278)
This commit is contained in:
parent
1432723de5
commit
73689f4639
1 changed files with 39 additions and 32 deletions
71
apps/chat.js
71
apps/chat.js
|
|
@ -50,8 +50,8 @@ const defaultPropmtPrefix = ', a large language model trained by OpenAI. You ans
|
||||||
const newFetch = (url, options = {}) => {
|
const newFetch = (url, options = {}) => {
|
||||||
const defaultOptions = Config.proxy
|
const defaultOptions = Config.proxy
|
||||||
? {
|
? {
|
||||||
agent: proxy(Config.proxy)
|
agent: proxy(Config.proxy)
|
||||||
}
|
}
|
||||||
: {}
|
: {}
|
||||||
const mergedOptions = {
|
const mergedOptions = {
|
||||||
...defaultOptions,
|
...defaultOptions,
|
||||||
|
|
@ -61,7 +61,7 @@ const newFetch = (url, options = {}) => {
|
||||||
return fetch(url, mergedOptions)
|
return fetch(url, mergedOptions)
|
||||||
}
|
}
|
||||||
export class chatgpt extends plugin {
|
export class chatgpt extends plugin {
|
||||||
constructor () {
|
constructor() {
|
||||||
let toggleMode = Config.toggleMode
|
let toggleMode = Config.toggleMode
|
||||||
super({
|
super({
|
||||||
/** 功能名称 */
|
/** 功能名称 */
|
||||||
|
|
@ -175,7 +175,7 @@ export class chatgpt extends plugin {
|
||||||
* @param e
|
* @param e
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async getConversations (e) {
|
async getConversations(e) {
|
||||||
// todo 根据use返回不同的对话列表
|
// todo 根据use返回不同的对话列表
|
||||||
let keys = await redis.keys('CHATGPT:CONVERSATIONS:*')
|
let keys = await redis.keys('CHATGPT:CONVERSATIONS:*')
|
||||||
if (!keys || keys.length === 0) {
|
if (!keys || keys.length === 0) {
|
||||||
|
|
@ -198,7 +198,7 @@ export class chatgpt extends plugin {
|
||||||
* @param e
|
* @param e
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async destroyConversations (e) {
|
async destroyConversations(e) {
|
||||||
let ats = e.message.filter(m => m.type === 'at')
|
let ats = e.message.filter(m => m.type === 'at')
|
||||||
let use = await redis.get('CHATGPT:USE')
|
let use = await redis.get('CHATGPT:USE')
|
||||||
if (ats.length === 0) {
|
if (ats.length === 0) {
|
||||||
|
|
@ -308,7 +308,7 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async endAllConversations (e) {
|
async endAllConversations(e) {
|
||||||
let use = await redis.get('CHATGPT:USE') || 'api'
|
let use = await redis.get('CHATGPT:USE') || 'api'
|
||||||
let deleted = 0
|
let deleted = 0
|
||||||
switch (use) {
|
switch (use) {
|
||||||
|
|
@ -362,7 +362,7 @@ export class chatgpt extends plugin {
|
||||||
await this.reply(`结束了${deleted}个用户的对话。`, true)
|
await this.reply(`结束了${deleted}个用户的对话。`, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteConversation (e) {
|
async deleteConversation(e) {
|
||||||
let ats = e.message.filter(m => m.type === 'at')
|
let ats = e.message.filter(m => m.type === 'at')
|
||||||
let use = await redis.get('CHATGPT:USE') || 'api'
|
let use = await redis.get('CHATGPT:USE') || 'api'
|
||||||
if (use !== 'api3') {
|
if (use !== 'api3') {
|
||||||
|
|
@ -420,7 +420,7 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async switch2Picture (e) {
|
async switch2Picture(e) {
|
||||||
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
|
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
|
||||||
if (!userSetting) {
|
if (!userSetting) {
|
||||||
userSetting = getDefaultUserSetting()
|
userSetting = getDefaultUserSetting()
|
||||||
|
|
@ -433,7 +433,7 @@ export class chatgpt extends plugin {
|
||||||
await this.reply('ChatGPT回复已转换为图片模式')
|
await this.reply('ChatGPT回复已转换为图片模式')
|
||||||
}
|
}
|
||||||
|
|
||||||
async switch2Text (e) {
|
async switch2Text(e) {
|
||||||
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
|
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
|
||||||
if (!userSetting) {
|
if (!userSetting) {
|
||||||
userSetting = getDefaultUserSetting()
|
userSetting = getDefaultUserSetting()
|
||||||
|
|
@ -446,7 +446,7 @@ export class chatgpt extends plugin {
|
||||||
await this.reply('ChatGPT回复已转换为文字模式')
|
await this.reply('ChatGPT回复已转换为文字模式')
|
||||||
}
|
}
|
||||||
|
|
||||||
async switch2Audio (e) {
|
async switch2Audio(e) {
|
||||||
if (!Config.ttsSpace) {
|
if (!Config.ttsSpace) {
|
||||||
await this.reply('您没有配置VITS API,请前往锅巴面板进行配置')
|
await this.reply('您没有配置VITS API,请前往锅巴面板进行配置')
|
||||||
return
|
return
|
||||||
|
|
@ -462,7 +462,7 @@ export class chatgpt extends plugin {
|
||||||
await this.reply('ChatGPT回复已转换为语音模式')
|
await this.reply('ChatGPT回复已转换为语音模式')
|
||||||
}
|
}
|
||||||
|
|
||||||
async setDefaultRole (e) {
|
async setDefaultRole(e) {
|
||||||
if (!Config.ttsSpace) {
|
if (!Config.ttsSpace) {
|
||||||
await this.reply('您没有配置VITS API,请前往锅巴面板进行配置')
|
await this.reply('您没有配置VITS API,请前往锅巴面板进行配置')
|
||||||
return
|
return
|
||||||
|
|
@ -489,7 +489,7 @@ export class chatgpt extends plugin {
|
||||||
* #chatgpt
|
* #chatgpt
|
||||||
* @param e oicq传递的事件参数e
|
* @param e oicq传递的事件参数e
|
||||||
*/
|
*/
|
||||||
async chatgpt (e) {
|
async chatgpt(e) {
|
||||||
let prompt
|
let prompt
|
||||||
if (this.toggleMode === 'at') {
|
if (this.toggleMode === 'at') {
|
||||||
if (!e.msg || e.msg.startsWith('#')) {
|
if (!e.msg || e.msg.startsWith('#')) {
|
||||||
|
|
@ -498,6 +498,7 @@ export class chatgpt extends plugin {
|
||||||
if (e.isGroup && !e.atme) {
|
if (e.isGroup && !e.atme) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if (e.user_id == Bot.uin) return false
|
||||||
prompt = e.msg.trim()
|
prompt = e.msg.trim()
|
||||||
} else {
|
} else {
|
||||||
let ats = e.message.filter(m => m.type === 'at')
|
let ats = e.message.filter(m => m.type === 'at')
|
||||||
|
|
@ -521,7 +522,7 @@ export class chatgpt extends plugin {
|
||||||
await this.abstractChat(e, prompt, use)
|
await this.abstractChat(e, prompt, use)
|
||||||
}
|
}
|
||||||
|
|
||||||
async abstractChat (e, prompt, use) {
|
async abstractChat(e, prompt, use) {
|
||||||
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
|
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
|
||||||
if (userSetting) {
|
if (userSetting) {
|
||||||
userSetting = JSON.parse(userSetting)
|
userSetting = JSON.parse(userSetting)
|
||||||
|
|
@ -573,7 +574,7 @@ export class chatgpt extends plugin {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
prompt = imgOcrText + prompt
|
prompt = imgOcrText + prompt
|
||||||
} catch (err) {}
|
} catch (err) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 检索是否有屏蔽词
|
// 检索是否有屏蔽词
|
||||||
|
|
@ -661,11 +662,11 @@ export class chatgpt extends plugin {
|
||||||
key = `CHATGPT:CONVERSATIONS:${e.sender.user_id}`
|
key = `CHATGPT:CONVERSATIONS:${e.sender.user_id}`
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'bing':{
|
case 'bing': {
|
||||||
key = `CHATGPT:CONVERSATIONS_BING:${e.sender.user_id}`
|
key = `CHATGPT:CONVERSATIONS_BING:${e.sender.user_id}`
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'chatglm':{
|
case 'chatglm': {
|
||||||
key = `CHATGPT:CONVERSATIONS_CHATGLM:${e.sender.user_id}`
|
key = `CHATGPT:CONVERSATIONS_CHATGLM:${e.sender.user_id}`
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -740,9 +741,15 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
if (useTTS) {
|
if (useTTS) {
|
||||||
if (Config.ttsSpace && response.length <= Config.ttsAutoFallbackThreshold) {
|
if (Config.ttsSpace && response.length <= Config.ttsAutoFallbackThreshold) {
|
||||||
let wav = await generateAudio(response, speaker, '中日混合(中文用[ZH][ZH]包裹起来,日文用[JA][JA]包裹起来)')
|
let audio_err = false
|
||||||
await e.reply(segment.record(wav))
|
try {
|
||||||
if (Config.alsoSendText) {
|
let wav = await generateAudio(response, speaker, '中日混合(中文用[ZH][ZH]包裹起来,日文用[JA][JA]包裹起来)')
|
||||||
|
await e.reply(segment.record(wav))
|
||||||
|
} catch (err) {
|
||||||
|
await this.reply('合成语音发生错误,我用文本回复你吧')
|
||||||
|
audio_err = true
|
||||||
|
}
|
||||||
|
if (Config.alsoSendText || audio_err) {
|
||||||
await this.reply(`${response}`, e.isGroup)
|
await this.reply(`${response}`, e.isGroup)
|
||||||
if (quotemessage.length > 0) {
|
if (quotemessage.length > 0) {
|
||||||
this.reply(await makeForwardMsg(this.e, quotemessage))
|
this.reply(await makeForwardMsg(this.e, quotemessage))
|
||||||
|
|
@ -806,7 +813,7 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async chatgpt1 (e) {
|
async chatgpt1(e) {
|
||||||
if (!Config.allowOtherMode) {
|
if (!Config.allowOtherMode) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -825,7 +832,7 @@ export class chatgpt extends plugin {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async chatgpt3 (e) {
|
async chatgpt3(e) {
|
||||||
if (!Config.allowOtherMode) {
|
if (!Config.allowOtherMode) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -844,7 +851,7 @@ export class chatgpt extends plugin {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async chatglm (e) {
|
async chatglm(e) {
|
||||||
if (!Config.allowOtherMode) {
|
if (!Config.allowOtherMode) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -863,7 +870,7 @@ export class chatgpt extends plugin {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async bing (e) {
|
async bing(e) {
|
||||||
if (!Config.allowOtherMode) {
|
if (!Config.allowOtherMode) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -882,7 +889,7 @@ export class chatgpt extends plugin {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async renderImage (e, template, content, prompt, quote = [], cache = false) {
|
async renderImage(e, template, content, prompt, quote = [], cache = false) {
|
||||||
let cacheData = { file: '', cacheUrl: Config.cacheUrl }
|
let cacheData = { file: '', cacheUrl: Config.cacheUrl }
|
||||||
if (cache) {
|
if (cache) {
|
||||||
if (Config.cacheEntry) cacheData.file = randomString()
|
if (Config.cacheEntry) cacheData.file = randomString()
|
||||||
|
|
@ -924,7 +931,7 @@ export class chatgpt extends plugin {
|
||||||
}, { retType: Config.quoteReply ? 'base64' : '' }), e.isGroup && Config.quoteReply)
|
}, { retType: Config.quoteReply ? 'base64' : '' }), e.isGroup && Config.quoteReply)
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendMessage (prompt, conversation = {}, use, e) {
|
async sendMessage(prompt, conversation = {}, use, e) {
|
||||||
if (!conversation) {
|
if (!conversation) {
|
||||||
conversation = {
|
conversation = {
|
||||||
timeoutMs: Config.defaultTimeoutMs
|
timeoutMs: Config.defaultTimeoutMs
|
||||||
|
|
@ -1115,12 +1122,12 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async emptyQueue (e) {
|
async emptyQueue(e) {
|
||||||
await redis.lTrim('CHATGPT:CHAT_QUEUE', 1, 0)
|
await redis.lTrim('CHATGPT:CHAT_QUEUE', 1, 0)
|
||||||
await this.reply('已清空当前等待队列')
|
await this.reply('已清空当前等待队列')
|
||||||
}
|
}
|
||||||
|
|
||||||
async removeQueueFirst (e) {
|
async removeQueueFirst(e) {
|
||||||
let uid = await redis.lPop('CHATGPT:CHAT_QUEUE', 0)
|
let uid = await redis.lPop('CHATGPT:CHAT_QUEUE', 0)
|
||||||
if (!uid) {
|
if (!uid) {
|
||||||
await this.reply('当前等待队列为空')
|
await this.reply('当前等待队列为空')
|
||||||
|
|
@ -1129,7 +1136,7 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllConversations (e) {
|
async getAllConversations(e) {
|
||||||
const use = await redis.get('CHATGPT:USE')
|
const use = await redis.get('CHATGPT:USE')
|
||||||
if (use === 'api3') {
|
if (use === 'api3') {
|
||||||
let conversations = await getConversations(e.sender.user_id, newFetch)
|
let conversations = await getConversations(e.sender.user_id, newFetch)
|
||||||
|
|
@ -1150,7 +1157,7 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async joinConversation (e) {
|
async joinConversation(e) {
|
||||||
let ats = e.message.filter(m => m.type === 'at')
|
let ats = e.message.filter(m => m.type === 'at')
|
||||||
let use = await redis.get('CHATGPT:USE') || 'api'
|
let use = await redis.get('CHATGPT:USE') || 'api'
|
||||||
// if (use !== 'api3') {
|
// if (use !== 'api3') {
|
||||||
|
|
@ -1181,7 +1188,7 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async attachConversation (e) {
|
async attachConversation(e) {
|
||||||
const use = await redis.get('CHATGPT:USE')
|
const use = await redis.get('CHATGPT:USE')
|
||||||
if (use !== 'api3') {
|
if (use !== 'api3') {
|
||||||
await this.reply('该功能目前仅支持API3模式')
|
await this.reply('该功能目前仅支持API3模式')
|
||||||
|
|
@ -1198,7 +1205,7 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async totalAvailable (e) {
|
async totalAvailable(e) {
|
||||||
if (!Config.apiKey) {
|
if (!Config.apiKey) {
|
||||||
this.reply('当前未配置OpenAI API key,请在锅巴面板或插件配置文件config/config.js中配置。若使用免费的API3则无需关心计费。')
|
this.reply('当前未配置OpenAI API key,请在锅巴面板或插件配置文件config/config.js中配置。若使用免费的API3则无需关心计费。')
|
||||||
return false
|
return false
|
||||||
|
|
@ -1231,7 +1238,7 @@ export class chatgpt extends plugin {
|
||||||
* @param prompt 问题
|
* @param prompt 问题
|
||||||
* @param conversation 对话
|
* @param conversation 对话
|
||||||
*/
|
*/
|
||||||
async chatgptBrowserBased (prompt, conversation) {
|
async chatgptBrowserBased(prompt, conversation) {
|
||||||
let option = { markdown: true }
|
let option = { markdown: true }
|
||||||
if (Config['2captchaToken']) {
|
if (Config['2captchaToken']) {
|
||||||
option.captchaToken = Config['2captchaToken']
|
option.captchaToken = Config['2captchaToken']
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue