From 2b99eb057931f4274e8cfcbe137ff5d13a676213 Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Thu, 16 Feb 2023 22:23:18 +0100 Subject: [PATCH] refactor: use matchAll instead of while loop Signed-off-by: Sefa Eyeoglu --- src/utils/resolveMessage.ts | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index e4e4531..b836cc2 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -22,41 +22,30 @@ function findFirstImage(message: Message): string | undefined { export async function expandDiscordLink(message: Message): Promise { const re = /(https?:\/\/)?(?:canary\.|ptb\.)?discord(?:app)?\.com\/channels\/(?\d+)\/(?\d+)\/(?\d+)/g; - let execResult = re.exec(message.content); - while (!(execResult == null || execResult.groups == undefined)) { - if (execResult.groups.server_id != message.guildId) { - execResult = re.exec(message.content); + const results = message.content.matchAll(re); + + for (const r of results) { + if (r.groups == undefined && r.groups.server_id != message.guildId) continue; // do not let the bot leak messages from one server to another - } const channel = await message.guild?.channels.fetch( - execResult.groups.channel_id + r.groups.channel_id ); - if (channel == undefined || channel == null || !channel.isTextBased()) { - execResult = re.exec(message.content); + if (!channel || !channel.isTextBased()) continue; - } if (channel instanceof ThreadChannel) { - if ( - !channel.parent?.members?.some((user) => user.id == message.author.id) - ) { - execResult = re.exec(message.content); + 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)) { - execResult = re.exec(message.content); + 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 messageToShow = await channel.messages.fetch( - execResult.groups.message_id - ); + const messageToShow = await channel.messages.fetch(r.groups.message_id); const builder = new EmbedBuilder() .setAuthor({ @@ -93,10 +82,6 @@ export async function expandDiscordLink(message: Message): Promise { await message.channel.send({ embeds: [builder], components: [row] }); } catch (e) { console.error(e); - execResult = re.exec(message.content); - continue; } - - execResult = re.exec(message.content); } }