Skip to content

Commit 9cc70f6

Browse files
committed
discord: Fix TTS functionality by updating @discordjs/voice (Resolves #1114)
1 parent f64892f commit 9cc70f6

5 files changed

Lines changed: 531 additions & 137 deletions

File tree

discord/speeches/openai.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const speech: SynthesizeFunction = async (text: string, voiceType: VoiceType, op
1212
});
1313

1414
return {
15-
data: Buffer.from(await mp3.arrayBuffer()),
15+
data: Buffer.from(mp3.data),
1616
};
1717
};
1818

discord/tts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ export default class TTS extends EventEmitter {
388388
]);
389389
});
390390
} catch (error) {
391+
log.error('stack' in error ? error.stack : error);
391392
this.emit('message', `エラー😢: ${error.message ? error.message : inspect(error, {depth: null, colors: false})}`);
392393
}
393394
}

lib/openai.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const checkAudioSpeechCreateModel = (params: SpeechCreateParams) => {
9898
throw new Error(`Unsupported model for audio.speech.create: ${params.model}`);
9999
}
100100

101-
const audioSpeechCreate = async (params: SpeechCreateParams): Promise<Response> => {
101+
const audioSpeechCreate = async (params: SpeechCreateParams): Promise<Response & {data: ArrayBuffer}> => {
102102
await checkUsageLimit('discord');
103103
const model = checkAudioSpeechCreateModel(params);
104104

@@ -110,14 +110,14 @@ const audioSpeechCreate = async (params: SpeechCreateParams): Promise<Response>
110110
}
111111

112112
let cost: number | null = null;
113+
const data = await response.arrayBuffer();
113114

114115
if (model === 'gpt-4o-mini-tts') {
115116
// The price for the gpt-4o-mini-tts model is calculated against the number of tokens, but
116117
// currently it is not possible to get the usage information from the response.
117118
// For now, we will use the official cost estimation equation provided by OpenAI:
118119
// $0.015 / minute
119120
// https://community.openai.com/t/how-do-i-calculate-the-usage-cost-when-using-the-gpt-4o-mini-tts-model/1263804
120-
const data = await response.arrayBuffer();
121121
const duration = await mp3Duration(Buffer.from(data));
122122
cost = (duration / 60) * 0.015;
123123
} else {
@@ -144,7 +144,10 @@ const audioSpeechCreate = async (params: SpeechCreateParams): Promise<Response>
144144
cost,
145145
});
146146

147-
return response;
147+
return {
148+
...response,
149+
data,
150+
};
148151
};
149152

150153
const checkChatCompletionCreateModel = (params: ChatCompletionCreateParamsNonStreaming): string => {

0 commit comments

Comments
 (0)