Refactor handleDirectMessage function to use async/await syntax in messageCreate.js
This commit is contained in:
parent
30248e0475
commit
3c61ca3d41
1 changed files with 25 additions and 17 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue