diff --git a/events/messageCreate.js b/events/messageCreate.js index b8c1df0..07dbf8b 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -364,16 +364,18 @@ async function handleDirectMessage(message) { "content": message.content }); - openai.chat.completions.create({ - model: 'gpt-3.5-turbo-0125', - messages: messages, - max_tokens: func.tokenizer('gpt-3.5-turbo-0125', messages).maxTokens, - temperature: 0.8, - top_p: 1, - frequency_penalty: 0.5, - presence_penalty: 0.5, - stream: true - }).then(async (response) => { + try { + const response = await openai.chat.completions.create({ + model: 'gpt-3.5-turbo-0125', + messages: messages, + max_tokens: func.tokenizer('gpt-3.5-turbo-0125', messages).maxTokens, + temperature: 0.8, + top_p: 1, + frequency_penalty: 0.5, + presence_penalty: 0.5, + stream: true + }); + let fullAnswer = ''; for await (const part of response) { @@ -383,16 +385,22 @@ async function handleDirectMessage(message) { await message.author.send(fullAnswer); conversations.set(message.author.id, messages.concat([{ "role": "assistant", "content": fullAnswer }])); - }).catch(async (error) => { + } catch (error) { console.error(chalk.bold.redBright(error)); - if (error.response) await message.author.send(error.response.error.message.substring(0, 2000)); - else if (error.message) await message.author.send(error.message.substring(0, 2000)); - }); + if (error.response) { + await message.author.send(error.response.error.message.substring(0, 2000)); + } else if (error.message) { + await message.author.send(error.message.substring(0, 2000)); + } + } } else { - await message.author.send("Hey there! What's up? Feel free to chat with me about anything!"); - - conversations.set(message.author.id, []); + try { + await message.author.send("Hey there! What's up? Feel free to chat with me about anything!"); + conversations.set(message.author.id, []); + } catch (error) { + console.error(chalk.bold.redBright(error)); + } } }