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:
parent
fa1caf3ad8
commit
aafefc3ad0
1 changed files with 15 additions and 8 deletions
23
src/index.ts
23
src/index.ts
|
@ -39,20 +39,27 @@ discord.on("messageCreate", async message => {
|
|||
max_tokens: 168
|
||||
});
|
||||
|
||||
const response = message.reply({
|
||||
content: answer.data.choices[0].message?.content,
|
||||
allowedMentions: {
|
||||
repliedUser: false,
|
||||
}
|
||||
});
|
||||
|
||||
const usage = answer.data.usage;
|
||||
if (usage != undefined) {
|
||||
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}`);
|
||||
}
|
||||
|
||||
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) {
|
||||
console.error(e);
|
||||
|
||||
|
|
Loading…
Reference in a new issue