Handle almost all of the promise rejections

fixes #7
This commit is contained in:
Wroclaw 2023-07-31 12:17:14 +02:00
parent cf3102cbc5
commit 5a116b0531
4 changed files with 17 additions and 15 deletions

View file

@ -38,7 +38,7 @@ export default class MyLimit extends Command implements Command {
const nthUseInLimitTimestamp = await nthUseInLimitTimestampPromise;
if (userLimit === false || nthUseInLimitTimestamp === false) {
interaction.reply({
await interaction.reply({
embeds: [{
author: { name: interaction.user.username, icon_url: interaction.user.displayAvatarURL({ size: 128 }) },
description: "User is a VIP, so there is no limit",
@ -48,7 +48,7 @@ export default class MyLimit extends Command implements Command {
return;
}
interaction.reply({
await interaction.reply({
embeds: [{
author: { name: interaction.user.username, icon_url: interaction.user.displayAvatarURL({ size: 128 }) },
fields: [

View file

@ -157,7 +157,7 @@ export async function queueRequest(request: apiRequest) {
"You've used up your message limit for today,\n" +
`${userLimit.limit} requests in last 24 hours`,
}]
}).catch(() => {/* GRACEFAIL: */});
}).catch(() => {/* FIXME: What should the bot do in this case to inform of limit reached?*/});
}
else if (request.isRepliable()) {
request.reply({
@ -175,7 +175,7 @@ export async function queueRequest(request: apiRequest) {
const shouldStart = messagesForChannel.length === 0;
messagesForChannel.push(request as request);
if (shouldStart)
executeFromQueue(request.channelId);
void executeFromQueue(request.channelId);
}
/**
@ -234,10 +234,10 @@ async function executeFromQueue(channel: string) {
messages.forEach(m => { Moderation.checkMessage(m); });
if (message instanceof DiscordApi.Message) {
message.channel.sendTyping();
await message.channel.sendTyping();
}
else if (message.isRepliable()) {
message.deferReply();
await message.deferReply();
}
OpenAImessages = toOpenAIMessages(messages);
@ -270,7 +270,7 @@ async function executeFromQueue(channel: string) {
const answerContent = answer.data.choices[0].message?.content;
if (answerContent === undefined || answerContent === "") {
if (message instanceof DiscordApi.Message) message.react("😶").catch(/*it's okay*/);
if (message instanceof DiscordApi.Message) message.react("😶").catch(() => {/* GRACEFAIL: It's okay if the bot won't reply */});
}
else {
const answerMessagesContent :string[] = [""];
@ -286,8 +286,7 @@ async function executeFromQueue(channel: string) {
for (const i of answerMessagesContent) {
const response = requestReply(message, {content: i}, {allowedMentions: { repliedUser: false }});
response.then(rval => Moderation.checkMessage(rval));
await response;
await response.then(rval => Moderation.checkMessage(rval));
}
}
} catch (e) {
@ -305,7 +304,7 @@ async function executeFromQueue(channel: string) {
}
else errorText = "";
requestReply(
await requestReply(
message,
{
embeds: [{
@ -322,5 +321,5 @@ async function executeFromQueue(channel: string) {
if (channelQueue.length === 0)
channelsRunning.delete(channel);
else
executeFromQueue(channel);
return executeFromQueue(channel);
}

View file

@ -31,7 +31,7 @@ discord.on("messageCreate", message => {
if (message.author.bot) return;
if (!message.mentions.has(message.client.user)) return;
queueRequest(message);
return queueRequest(message);
});
if (require.main === module) discord.login(config.tokens.Discord);
if (require.main === module) void discord.login(config.tokens.Discord);

View file

@ -36,7 +36,7 @@ export default class CommandManager {
color: 0xff0000,
description: `Failed to perform interaction:\n\`${e}\``,
}]
}).catch(() => {/* GRACEFAIL: We're still logging the issue that happened to the console */});
}).catch(() => {/* NOTE: We're still logging the issue that happened to the console */});
console.error(`Failed to perform interaction: ${interaction.commandName}`);
console.error(e);
});
@ -46,7 +46,10 @@ export default class CommandManager {
const foundCommand = this.commands.find((command) => command.name === interaction.commandName );
if (!foundCommand) throw new Error(`Unknown command received (${interaction.commandName}). Did you forgot to push updated commands?`);
if (!foundCommand.autocomplete) return;
foundCommand.autocomplete(interaction);
foundCommand.autocomplete(interaction).catch(e => {
console.error(`Failed to perform autocomplete interaction: ${interaction.commandName}`);
console.error(e);
});
return;
}
}