fix: 适配无oicq&icqq;删除存储的azure语音

This commit is contained in:
ikechan8370 2023-04-23 21:01:03 +08:00
parent 1bfb4d7d6b
commit d5e3d52d43
2 changed files with 33 additions and 17 deletions

View file

@ -8,6 +8,7 @@ import { BingAIClient } from '@waylaidwanderer/chatgpt-api'
import SydneyAIClient from '../utils/SydneyAIClient.js' import SydneyAIClient from '../utils/SydneyAIClient.js'
import { PoeClient } from '../utils/poe/index.js' import { PoeClient } from '../utils/poe/index.js'
import AzureTTS from '../utils/tts/microsoft-azure.js' import AzureTTS from '../utils/tts/microsoft-azure.js'
import fs from 'fs'
import { import {
render, renderUrl, render, renderUrl,
getMessageById, getMessageById,
@ -987,7 +988,7 @@ export class chatgpt extends plugin {
} }
try { try {
try { try {
let sendable = await uploadRecord(wav) let sendable = await uploadRecord(wav, Config.ttsMode === 'azure')
if (sendable) { if (sendable) {
await e.reply(sendable) await e.reply(sendable)
} else { } else {
@ -1002,6 +1003,14 @@ export class chatgpt extends plugin {
logger.error(err) logger.error(err)
await this.reply('合成语音发生错误~') await this.reply('合成语音发生错误~')
} }
if (Config.ttsMode === 'azure' && Config.azureTTSKey) {
// 清理文件
try {
fs.unlinkSync(wav)
} catch (err) {
logger.warn(err)
}
}
} else if (userSetting.usePicture || (Config.autoUsePicture && response.length > Config.autoUsePictureThreshold)) { } else if (userSetting.usePicture || (Config.autoUsePicture && response.length > Config.autoUsePictureThreshold)) {
// todo use next api of chatgpt to complete incomplete respoonse // todo use next api of chatgpt to complete incomplete respoonse
try { try {

View file

@ -12,31 +12,38 @@ let module
try { try {
module = await import('oicq') module = await import('oicq')
} catch (err) { } catch (err) {
try {
module = await import('icqq') module = await import('icqq')
} catch (err1) {
// 可能是go-cqhttp之类的
}
} }
const { core } = module let pcm2slk, core, Contactable
const Contactable = module.default if (module) {
// import { pcm2slk } from 'node-silk' core = module.core
let errors = {} Contactable = module.default
let pcm2slk try {
try {
pcm2slk = (await import('node-silk')).pcm2slk pcm2slk = (await import('node-silk')).pcm2slk
} catch (e) { } catch (e) {
if (Config.cloudTranscode) { if (Config.cloudTranscode) {
logger.warn('未安装node-silk将尝试使用云转码服务进行合成') logger.warn('未安装node-silk将尝试使用云转码服务进行合成')
} else { } else {
Config.debug && logger.error(e) Config.debug && logger.error(e)
logger.warn('未安装node-silk如ffmpeg不支持amr编码请安装node-silk以支持语音模式') logger.warn('未安装node-silk如ffmpeg不支持amr编码请安装node-silk以支持语音模式')
} }
}
} }
async function uploadRecord (recordUrl) { // import { pcm2slk } from 'node-silk'
let errors = {}
async function uploadRecord (recordUrl, forceFile) {
let result let result
if (pcm2slk) { if (pcm2slk) {
result = await getPttBuffer(recordUrl, Bot.config.ffmpeg_path) result = await getPttBuffer(recordUrl, Bot.config.ffmpeg_path)
} else if (Config.cloudTranscode) { } else if (Config.cloudTranscode) {
try { try {
if (Config.cloudMode === 'file') { if (forceFile || Config.cloudMode === 'file') {
const formData = new FormData() const formData = new FormData()
let buffer let buffer
if (!recordUrl.startsWith('http')) { if (!recordUrl.startsWith('http')) {