diff --git a/src/index.ts b/src/index.ts index 11001ba..f85a8ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -77,30 +77,36 @@ client.once('ready', async () => { }); client.on('messageCreate', async (e) => { - if (e.channel.partial) await e.channel.fetch(); - if (e.author.partial) await e.author.fetch(); + try { + if (e.channel.partial) await e.channel.fetch(); + if (e.author.partial) await e.author.fetch(); - if (!e.content) return; - if (!e.channel.isTextBased()) return; + if (!e.content) return; + if (!e.channel.isTextBased()) return; - if (e.author === client.user) return; + if (e.author === client.user) return; - if (e.cleanContent.match(BuildConfig.ETA_REGEX)) { - await e.reply( - `${random(BuildConfig.ETA_MESSAGES)} <:pofat:1031701005559144458>` - ); + if (e.cleanContent.match(BuildConfig.ETA_REGEX)) { + await e.reply( + `${random(BuildConfig.ETA_MESSAGES)} <:pofat:1031701005559144458>` + ); + } + const log = await parseLog(e.content); + if (log != null) { + e.reply({ embeds: [log] }); + return; + } + await expandDiscordLink(e); + } catch (error) { + console.error('Unhandled exception on messageCreate', error); } - const log = await parseLog(e.content); - if (log != null) { - e.reply({ embeds: [log] }); - return; - } - await expandDiscordLink(e); }); }); client.on('interactionCreate', async (interaction) => { - if (interaction.isChatInputCommand()) { + try { + if (!interaction.isChatInputCommand()) return; + const { commandName } = interaction; if (commandName === 'ping') { @@ -123,26 +129,32 @@ client.on('interactionCreate', async (interaction) => { } else if (commandName === 'rory') { await roryCommand(interaction); } + } catch (error) { + console.error('Unhandled exception on interactionCreate', error); } }); client.on('messageReactionAdd', async (reaction, user) => { - if (reaction.partial) { - try { - await reaction.fetch(); - } catch (error) { - console.error('Something went wrong when fetching the message:', error); - return; + try { + if (reaction.partial) { + try { + await reaction.fetch(); + } catch (error) { + console.error('Something went wrong when fetching the message:', error); + return; + } } - } - if ( - reaction.message.interaction && - reaction.message.interaction?.type === InteractionType.ApplicationCommand && - reaction.message.interaction?.user === user && - reaction.emoji.name === '❌' - ) { - await reaction.message.delete(); + if ( + reaction.message.interaction && + reaction.message.interaction?.type === InteractionType.ApplicationCommand && + reaction.message.interaction?.user === user && + reaction.emoji.name === '❌' + ) { + await reaction.message.delete(); + } + } catch (error) { + console.error('Unhandled exception on messageReactionAdd', error); } }); diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 559a935..50ada1d 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -32,21 +32,21 @@ export async function expandDiscordLink(message: Message): Promise { if (r.groups == undefined || r.groups.serverId != message.guildId) continue; // do not let the bot leak messages from one server to another - const channel = await message.guild?.channels.fetch(r.groups.channelId); - - if (!channel || !channel.isTextBased()) continue; - - if (channel instanceof ThreadChannel) { - if ( - !channel.parent?.members?.some((user) => user.id == message.author.id) - ) - continue; // do not reveal a message to a user who can't see it - } else { - if (!channel.members?.some((user) => user.id == message.author.id)) - continue; // do not reveal a message to a user who can't see it - } - try { + const channel = await message.guild?.channels.fetch(r.groups.channelId); + + if (!channel || !channel.isTextBased()) continue; + + if (channel instanceof ThreadChannel) { + if ( + !channel.parent?.members?.some((user) => user.id == message.author.id) + ) + continue; // do not reveal a message to a user who can't see it + } else { + if (!channel.members?.some((user) => user.id == message.author.id)) + continue; // do not reveal a message to a user who can't see it + } + const originalMessage = await channel.messages.fetch(r.groups.messageId); const embed = new EmbedBuilder() @@ -78,8 +78,7 @@ export async function expandDiscordLink(message: Message): Promise { } resultEmbeds.push(embed); - } catch (e) { - console.error(e); + } catch (ignored) { } }