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,7 +364,8 @@ async function handleDirectMessage(message) {
"content": message.content "content": message.content
}); });
openai.chat.completions.create({ try {
const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-0125', model: 'gpt-3.5-turbo-0125',
messages: messages, messages: messages,
max_tokens: func.tokenizer('gpt-3.5-turbo-0125', messages).maxTokens, max_tokens: func.tokenizer('gpt-3.5-turbo-0125', messages).maxTokens,
@ -373,7 +374,8 @@ async function handleDirectMessage(message) {
frequency_penalty: 0.5, frequency_penalty: 0.5,
presence_penalty: 0.5, presence_penalty: 0.5,
stream: true stream: true
}).then(async (response) => { });
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 {
try {
await message.author.send("Hey there! What's up? Feel free to chat with me about anything!"); 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));
}
} }
} }