chore: error handling (#18)

* error handling

* add floating promises linter rule
This commit is contained in:
Daniel Dietzler
2023-12-02 21:30:04 +01:00
committed by GitHub
parent 374e7daf6c
commit 494803c1b0
5 changed files with 65 additions and 41 deletions
+6
View File
@@ -1,6 +1,12 @@
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
tsconfigRootDir: __dirname,
},
plugins: ['@typescript-eslint'],
rules: { '@typescript-eslint/no-floating-promises': 'error' },
root: true,
};
+30 -25
View File
@@ -55,8 +55,8 @@ export class Commands {
})
message: string | null,
interaction: CommandInteraction,
): void {
interaction.reply({
) {
return interaction.reply({
content: message ? `${message}: ${linkCommands[name]}` : linkCommands[name],
flags: [MessageFlags.SuppressEmbeds],
});
@@ -74,7 +74,7 @@ export class Commands {
name: keyof typeof HELP_TEXTS,
interaction: CommandInteraction,
) {
interaction.reply({
return interaction.reply({
content: HELP_TEXTS[name],
flags: [MessageFlags.SuppressEmbeds],
});
@@ -91,7 +91,7 @@ export class Commands {
const delta = lastStarsCount && starsCount - lastStarsCount;
const formattedDelta = delta && Intl.NumberFormat(undefined, { signDisplay: 'always' }).format(delta);
interaction.reply(
await interaction.reply(
`Stars ⭐: ${starsCount}${
formattedDelta ? ` (${formattedDelta} stars since the last call in this channel)` : ''
}`,
@@ -99,7 +99,7 @@ export class Commands {
_star_history[interaction.channelId] = starsCount;
} catch (error) {
interaction.reply("Couldn't fetch stars count from github api");
await interaction.reply("Couldn't fetch stars count from github api");
}
}
@@ -114,13 +114,13 @@ export class Commands {
const delta = lastForksCount && forksCount - lastForksCount;
const formattedDelta = delta && Intl.NumberFormat(undefined, { signDisplay: 'always' }).format(delta);
interaction.reply(
await interaction.reply(
`Forks: ${forksCount}${formattedDelta ? ` (${formattedDelta} forks since the last call in this channel)` : ''}`,
);
_fork_history[interaction.channelId] = forksCount;
} catch (error) {
interaction.reply("Couldn't fetch forks count from github api");
await interaction.reply("Couldn't fetch forks count from github api");
}
}
@@ -153,24 +153,29 @@ export class Commands {
return interaction.respond([]);
}
const result = await octokit.rest.search
.issuesAndPullRequests({
q: `repo:immich-app/immich in:title ${value}`,
per_page: 5,
page: 1,
sort: 'updated',
order: 'desc',
})
.then((response) => response.data);
return interaction.respond(
result.items.map((item) => {
const name = `${item.pull_request ? '[PR]' : '[Issue]'} (${item.number}) ${item.title}`;
return {
name: name.length > 100 ? name.substring(0, 97) + '...' : name,
value: `${item.pull_request ? '[PR]' : '[Issue]'} ([#${item.number}](${item.html_url}))`,
};
}),
);
try {
const result = await octokit.rest.search
.issuesAndPullRequests({
q: `repo:immich-app/immich in:title ${value}`,
per_page: 5,
page: 1,
sort: 'updated',
order: 'desc',
})
.then((response) => response.data);
return interaction.respond(
result.items.map((item) => {
const name = `${item.pull_request ? '[PR]' : '[Issue]'} (${item.number}) ${item.title}`;
return {
name: name.length > 100 ? name.substring(0, 97) + '...' : name,
value: `${item.pull_request ? '[PR]' : '[Issue]'} ([#${item.number}](${item.html_url}))`,
};
}),
);
} catch (error) {
console.log('Could not fetch search results from GitHub');
return interaction.respond([]);
}
},
})
content: string,
-4
View File
@@ -52,11 +52,9 @@ export class HelpTicket {
@On({ event: 'messageReactionRemove' })
@On({ event: 'messageReactionAdd' })
async handleReaction([reaction]: ArgsOf<'messageReactionAdd'>) {
console.time();
if (reaction.partial) {
await reaction.fetch();
}
console.timeLog();
if (!reaction.message.author?.bot) {
return;
@@ -74,7 +72,6 @@ export class HelpTicket {
reaction.message.thread?.ownerId ?? '',
reaction.message.reactions.cache.map((reaction) => reaction.count > 1),
);
console.timeLog();
if (!message.includes(Constants.Icons.Unchecked)) {
mainButtonRow.components.at(-1)?.setDisabled(false);
@@ -87,7 +84,6 @@ export class HelpTicket {
await reaction.message.edit({ content: message, components: [mainButtonRow] });
}
console.timeEnd();
}
@On({ event: 'threadCreate' })
+20 -8
View File
@@ -49,17 +49,29 @@ export class MessageEvents {
for (const match of matches) {
if (match?.groups) {
const id = match.groups.id;
const response = await octokit.rest.issues.get({ ...IMMICH_REPOSITORY_BASE_OPTIONS, issue_number: Number(id) });
if (response.status === 200) {
const type = response.data.pull_request ? 'PR' : 'ISSUE';
links.add(`[${type}] ${response.data.title} ([#${id}](${response.data.html_url}))`);
continue;
try {
const response = await octokit.rest.issues.get({
...IMMICH_REPOSITORY_BASE_OPTIONS,
issue_number: Number(id),
});
if (response.status === 200) {
const type = response.data.pull_request ? 'PR' : 'ISSUE';
links.add(`[${type}] ${response.data.title} ([#${id}](${response.data.html_url}))`);
continue;
}
} catch (error) {
console.log(`Could not fetch #${id}`);
}
const { status: discussionStatus } = await fetch(`${Constants.Urls.Discussions}/${id}}`);
if (discussionStatus === 200) {
links.add(`[Discussion] ([#${id}](${Constants.Urls.Discussions}/${id}))`);
try {
const { status: discussionStatus } = await fetch(`${Constants.Urls.Discussions}/${id}}`);
if (discussionStatus === 200) {
links.add(`[Discussion] ([#${id}](${Constants.Urls.Discussions}/${id}))`);
}
} catch (error) {
console.log(`Could not fetch #${id}`);
}
}
}
+9 -4
View File
@@ -29,7 +29,7 @@ export const bot = new Client({
const birthdayJob = new CronJob(Constants.Misc.ImmichBirthdayCron, async () => {
const channel = (await bot.channels.fetch(Constants.Channels.General)) as TextChannel;
if (channel) {
channel.send(`"Happy birthday my other child" - Alex`);
await channel.send(`"Happy birthday my other child" - Alex`);
}
});
@@ -45,7 +45,7 @@ bot.once('ready', async () => {
const channel = (await bot.channels.fetch(Constants.Channels.BotSpam)) as TextChannel;
if (channel && fullVersion) {
channel.send(`I'm alive, running ${fullVersion}!`);
await channel.send(`I'm alive, running ${fullVersion}!`);
}
birthdayJob.start();
@@ -56,10 +56,15 @@ bot.on('interactionCreate', async (interaction: Interaction) => {
});
bot.on('messageCreate', async (message: Message) => {
// execute simple commands
await bot.executeCommand(message);
});
bot.on('error', async (error) => {
console.log(`Error handling bot interaction: ${error}`);
const botSpamChannel = (await bot.channels.fetch(Constants.Channels.BotSpam)) as TextChannel;
await botSpamChannel.send(`Error handling bot interaction: ${error}`);
});
async function run() {
// The following syntax should be used in the commonjs environment
//
@@ -77,4 +82,4 @@ async function run() {
await bot.login(process.env.BOT_TOKEN);
}
run();
await run();