Refactor handleDirectMessage function to use async/await syntax in messageCreate.js

This commit is contained in:
ShadowVirtual 2024-04-30 18:49:34 -08:00
parent 30248e0475
commit 3c61ca3d41

View file

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