Fix DiscordAPIError on receiving empty content.

Previously, when OpenAI returned a message with an empty string,
it would try to send that empty message, which throwed an error.

Now it will react with an emoji to the message that triggered the request.
This commit is contained in:
Wroclaw 2023-03-19 02:26:28 +01:00
parent fa1caf3ad8
commit aafefc3ad0

View file

@ -39,20 +39,27 @@ discord.on("messageCreate", async message => {
max_tokens: 168 max_tokens: 168
}); });
const response = message.reply({
content: answer.data.choices[0].message?.content,
allowedMentions: {
repliedUser: false,
}
});
const usage = answer.data.usage; const usage = answer.data.usage;
if (usage != undefined) { if (usage != undefined) {
const channelName: string = message.inGuild() ? `${message.channel.name} (${message.guild.name})` : `@${message.author.tag}`; const channelName: string = message.inGuild() ? `${message.channel.name} (${message.guild.name})` : `@${message.author.tag}`;
console.log(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens for ${message.author.tag} (${message.author.id}) in #${channelName}`); console.log(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens for ${message.author.tag} (${message.author.id}) in #${channelName}`);
} }
Moderation.checkMessage(await response); const answerContent = answer.data.choices[0].message?.content;
if (answerContent != undefined && answerContent != "") {
const response = message.reply({
content: answerContent,
allowedMentions: {
repliedUser: false,
}
});
Moderation.checkMessage(await response);
}
else {
message.react("😶");
}
} catch (e) { } catch (e) {
console.error(e); console.error(e);