11import splitMessage from "./splitMessage.js" ;
22import handleApiErrors from "./handleApiErrors.js" ;
3- import { GPT_MODE , MODEL_NAME , MAX_RETRIES , BETTER_LOG } from "../config.js" ;
3+ import {
4+ GPT_MODE ,
5+ MODEL_NAME ,
6+ MAX_RETRIES ,
7+ BETTER_LOG ,
8+ MAX_TOKENS
9+ } from "../config.js" ;
410import fs from "node:fs" ;
511import path from "node:path" ;
612
713let lastSpeechFile = null ;
814
15+ /**
16+ * Handles responding to a message with either text or voice
17+ * @param {Message } message - The Discord message to respond to
18+ * @param {Client } client - The Discord client instance
19+ * @param {Array } conversationLog - Array of previous messages for context
20+ */
921async function respondToMessage ( message , client , conversationLog ) {
1022 const typingInterval = setInterval ( ( ) => message . channel . sendTyping ( ) , 5000 ) ;
1123
@@ -22,7 +34,7 @@ async function respondToMessage(message, client, conversationLog) {
2234 } ;
2335
2436 if ( client . currentMode === GPT_MODE . TEXT ) {
25- requestPayload . max_tokens = 4096 ;
37+ requestPayload . max_tokens = MAX_TOKENS ;
2638 }
2739
2840 if ( BETTER_LOG ) {
@@ -61,6 +73,12 @@ async function respondToMessage(message, client, conversationLog) {
6173 }
6274}
6375
76+ /**
77+ * Handles generating and sending TTS audio responses
78+ * @param {Message } message - The Discord message to respond to
79+ * @param {Client } client - The Discord client instance
80+ * @param {string } text - The text to convert to speech
81+ */
6482async function sendTtsResponse ( message , client , text ) {
6583 const speechFile = path . resolve ( `./speech-${ Date . now ( ) } .mp3` ) ;
6684
@@ -75,9 +93,11 @@ async function sendTtsResponse(message, client, text) {
7593 voice : "echo" ,
7694 input : text ,
7795 } ) ;
96+
7897 const buffer = Buffer . from ( await mp3 . arrayBuffer ( ) ) ;
7998 await fs . promises . writeFile ( speechFile , buffer ) ;
8099 lastSpeechFile = speechFile ;
100+
81101 await message . channel . send ( {
82102 files : [
83103 {
@@ -94,4 +114,4 @@ async function sendTtsResponse(message, client, text) {
94114 }
95115}
96116
97- export default respondToMessage ;
117+ export default respondToMessage ;
0 commit comments