From 73676c28f4a036bf6a605ed56d8d7f91ccc00754 Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Sat, 14 Jan 2023 16:24:32 -0800 Subject: [PATCH 01/14] feat: expanding discord links --- src/index.ts | 3 +- src/utils/resolveMessage.ts | 72 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/utils/resolveMessage.ts diff --git a/src/index.ts b/src/index.ts index 75c6797..d69e2af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { reuploadCommands } from './_reupload'; import * as BuildConfig from './constants'; import { parseLog } from './logs'; import { getLatestMinecraftVersion } from './utils/remoteVersions'; +import { expandDiscordLink } from "./utils/resolveMessage"; import { membersCommand } from './commands/members'; import { starsCommand } from './commands/stars'; @@ -87,12 +88,12 @@ client.once('ready', async () => { `${random(BuildConfig.ETA_MESSAGES)} <:pofat:1031701005559144458>` ); } - const log = await parseLog(e.content); if (log != null) { e.reply({ embeds: [log] }); return; } + await expandDiscordLink(e); }); }); diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts new file mode 100644 index 0000000..9776f4e --- /dev/null +++ b/src/utils/resolveMessage.ts @@ -0,0 +1,72 @@ +import { Colors, EmbedBuilder, Message, ThreadChannel } from "discord.js"; + +function findFirstImage(message: Message): string | undefined { + const result = message.attachments.find((attach) => { + return attach.contentType?.startsWith("image/"); + }); + if (result == undefined) { + return undefined; + } else { + return result.url; + } +} + +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) { + continue; // do not let the bot leak messages from one server to another + } + const channel = await message.guild?.channels.fetch( + execResult.groups.channel_id + ); + if (channel == undefined || channel == null || !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 messageToShow = await channel.messages.fetch( + execResult.groups.message_id + ); + const builder = new EmbedBuilder() + .setAuthor({ + name: `${messageToShow.author.username}#${messageToShow.author.discriminator}`, + iconURL: messageToShow.author.displayAvatarURL(), + }) + .setDescription(messageToShow.content) + .setColor(Colors.Aqua); + if (messageToShow.attachments.size > 0) { + let attachmentsString = ""; + messageToShow.attachments.forEach((value) => { + attachmentsString += `[${value.name}](${value.url}) `; + }); + builder.addFields({ name: "Attachments", value: attachmentsString }); + const firstImage = findFirstImage(messageToShow); + if (firstImage != undefined) { + builder.setImage(firstImage); + } + } + builder.addFields({ + name: "Source", + value: `[Jump to original message](${messageToShow.url})`, + }); + message.channel.send({ embeds: [builder] }); + } catch (e) { + console.error(e); + continue; + } + execResult = re.exec(message.content); + } +} From d2119299fa59316140bf43f08451cf1d625b8a50 Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Tue, 17 Jan 2023 02:38:36 +0000 Subject: [PATCH 02/14] Import the message type as a type Co-authored-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Signed-off-by: PandaNinjas --- src/utils/resolveMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 9776f4e..96f43f4 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -1,4 +1,4 @@ -import { Colors, EmbedBuilder, Message, ThreadChannel } from "discord.js"; +import { Colors, EmbedBuilder, type Message, ThreadChannel } from "discord.js"; function findFirstImage(message: Message): string | undefined { const result = message.attachments.find((attach) => { From 90fbd3f717c6a731be6bfa6a163dd7fd4d3fab94 Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Mon, 16 Jan 2023 19:03:28 -0800 Subject: [PATCH 03/14] Make it a link button --- src/utils/resolveMessage.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 96f43f4..698152e 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -1,4 +1,12 @@ -import { Colors, EmbedBuilder, type Message, ThreadChannel } from "discord.js"; +import { + ActionRowBuilder, + ButtonBuilder, + ButtonStyle, + Colors, + EmbedBuilder, + type Message, + ThreadChannel, +} from "discord.js"; function findFirstImage(message: Message): string | undefined { const result = message.attachments.find((attach) => { @@ -58,11 +66,13 @@ export async function expandDiscordLink(message: Message): Promise { builder.setImage(firstImage); } } - builder.addFields({ - name: "Source", - value: `[Jump to original message](${messageToShow.url})`, - }); - message.channel.send({ embeds: [builder] }); + const row = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setLabel("Jump to original message") + .setStyle(ButtonStyle.Link) + .setURL(messageToShow.url) + ); + message.channel.send({ embeds: [builder], components: [row] }); } catch (e) { console.error(e); continue; From 2be950d71df6d365f62d38924135d7bc6718f6ae Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Tue, 17 Jan 2023 03:45:39 +0000 Subject: [PATCH 04/14] Add await (thank you for the ping /srs) Signed-off-by: PandaNinjas --- src/utils/resolveMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 698152e..0a6f4aa 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -72,7 +72,7 @@ export async function expandDiscordLink(message: Message): Promise { .setStyle(ButtonStyle.Link) .setURL(messageToShow.url) ); - message.channel.send({ embeds: [builder], components: [row] }); + await message.channel.send({ embeds: [builder], components: [row] }); } catch (e) { console.error(e); continue; From e856c041080fd50df8920543799e8a7b15211f6e Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Tue, 17 Jan 2023 13:11:36 +0800 Subject: [PATCH 05/14] formatting issues --- src/index.ts | 6 +++--- src/utils/resolveMessage.ts | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index d69e2af..1c49c39 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,14 +10,14 @@ import { reuploadCommands } from './_reupload'; import * as BuildConfig from './constants'; import { parseLog } from './logs'; import { getLatestMinecraftVersion } from './utils/remoteVersions'; -import { expandDiscordLink } from "./utils/resolveMessage"; +import { expandDiscordLink } from './utils/resolveMessage'; import { membersCommand } from './commands/members'; import { starsCommand } from './commands/stars'; import { modrinthCommand } from './commands/modrinth'; import { tagsCommand } from './commands/tags'; import { jokeCommand } from './commands/joke'; -import { roryCommand } from "./commands/rory"; +import { roryCommand } from './commands/rory'; import random from 'just-random'; import { green, bold, yellow, cyan } from 'kleur/colors'; @@ -122,7 +122,7 @@ client.on('interactionCreate', async (interaction) => { await tagsCommand(interaction); } else if (commandName === 'joke') { await jokeCommand(interaction); - } else if (commandName === "rory") { + } else if (commandName === 'rory') { await roryCommand(interaction); } } diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 0a6f4aa..7899a88 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -6,11 +6,11 @@ import { EmbedBuilder, type Message, ThreadChannel, -} from "discord.js"; +} from 'discord.js'; function findFirstImage(message: Message): string | undefined { const result = message.attachments.find((attach) => { - return attach.contentType?.startsWith("image/"); + return attach.contentType?.startsWith('image/'); }); if (result == undefined) { return undefined; @@ -23,16 +23,20 @@ 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) { continue; // do not let the bot leak messages from one server to another } + const channel = await message.guild?.channels.fetch( execResult.groups.channel_id ); + if (channel == undefined || channel == null || !channel.isTextBased()) { continue; } + if (channel instanceof ThreadChannel) { if ( !channel.parent?.members?.some((user) => user.id == message.author.id) @@ -44,10 +48,12 @@ export async function expandDiscordLink(message: Message): Promise { 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 builder = new EmbedBuilder() .setAuthor({ name: `${messageToShow.author.username}#${messageToShow.author.discriminator}`, @@ -55,28 +61,34 @@ export async function expandDiscordLink(message: Message): Promise { }) .setDescription(messageToShow.content) .setColor(Colors.Aqua); + if (messageToShow.attachments.size > 0) { - let attachmentsString = ""; + let attachmentsString = ''; messageToShow.attachments.forEach((value) => { attachmentsString += `[${value.name}](${value.url}) `; }); - builder.addFields({ name: "Attachments", value: attachmentsString }); + + builder.addFields({ name: 'Attachments', value: attachmentsString }); + const firstImage = findFirstImage(messageToShow); if (firstImage != undefined) { builder.setImage(firstImage); } } - const row = new ActionRowBuilder().addComponents( + + const row = new ActionRowBuilder().addComponents( new ButtonBuilder() - .setLabel("Jump to original message") + .setLabel('Jump to original message') .setStyle(ButtonStyle.Link) .setURL(messageToShow.url) ); + await message.channel.send({ embeds: [builder], components: [row] }); } catch (e) { console.error(e); continue; } + execResult = re.exec(message.content); } } From c2ad28c36d69fe9976609bcf08c0d31da7cff4fa Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Tue, 17 Jan 2023 08:27:55 -0800 Subject: [PATCH 06/14] Try to fix bot crash --- src/utils/resolveMessage.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 698152e..9e65526 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -53,8 +53,10 @@ export async function expandDiscordLink(message: Message): Promise { name: `${messageToShow.author.username}#${messageToShow.author.discriminator}`, iconURL: messageToShow.author.displayAvatarURL(), }) - .setDescription(messageToShow.content) .setColor(Colors.Aqua); + if (messageToShow.content) { + builder.setDescription(messageToShow.content); + } if (messageToShow.attachments.size > 0) { let attachmentsString = ""; messageToShow.attachments.forEach((value) => { From 69c78c1458df06095f69f1e603143e8417b87fec Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Tue, 17 Jan 2023 08:58:37 -0800 Subject: [PATCH 07/14] Formatting --- src/utils/resolveMessage.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 9295e55..2af2e6a 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -6,11 +6,11 @@ import { EmbedBuilder, type Message, ThreadChannel, -} from 'discord.js'; +} from "discord.js"; function findFirstImage(message: Message): string | undefined { const result = message.attachments.find((attach) => { - return attach.contentType?.startsWith('image/'); + return attach.contentType?.startsWith("image/"); }); if (result == undefined) { return undefined; @@ -64,12 +64,12 @@ export async function expandDiscordLink(message: Message): Promise { builder.setDescription(messageToShow.content); } if (messageToShow.attachments.size > 0) { - let attachmentsString = ''; + let attachmentsString = ""; messageToShow.attachments.forEach((value) => { attachmentsString += `[${value.name}](${value.url}) `; }); - builder.addFields({ name: 'Attachments', value: attachmentsString }); + builder.addFields({ name: "Attachments", value: attachmentsString }); const firstImage = findFirstImage(messageToShow); if (firstImage != undefined) { @@ -79,7 +79,7 @@ export async function expandDiscordLink(message: Message): Promise { const row = new ActionRowBuilder().addComponents( new ButtonBuilder() - .setLabel('Jump to original message') + .setLabel("Jump to original message") .setStyle(ButtonStyle.Link) .setURL(messageToShow.url) ); From ca1c609c941d24ea9ebf0bfc599ae94ed172689d Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Tue, 17 Jan 2023 09:01:08 -0800 Subject: [PATCH 08/14] Change double quote to single quote (prettier moment) --- src/utils/resolveMessage.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 2af2e6a..9295e55 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -6,11 +6,11 @@ import { EmbedBuilder, type Message, ThreadChannel, -} from "discord.js"; +} from 'discord.js'; function findFirstImage(message: Message): string | undefined { const result = message.attachments.find((attach) => { - return attach.contentType?.startsWith("image/"); + return attach.contentType?.startsWith('image/'); }); if (result == undefined) { return undefined; @@ -64,12 +64,12 @@ export async function expandDiscordLink(message: Message): Promise { builder.setDescription(messageToShow.content); } if (messageToShow.attachments.size > 0) { - let attachmentsString = ""; + let attachmentsString = ''; messageToShow.attachments.forEach((value) => { attachmentsString += `[${value.name}](${value.url}) `; }); - builder.addFields({ name: "Attachments", value: attachmentsString }); + builder.addFields({ name: 'Attachments', value: attachmentsString }); const firstImage = findFirstImage(messageToShow); if (firstImage != undefined) { @@ -79,7 +79,7 @@ export async function expandDiscordLink(message: Message): Promise { const row = new ActionRowBuilder().addComponents( new ButtonBuilder() - .setLabel("Jump to original message") + .setLabel('Jump to original message') .setStyle(ButtonStyle.Link) .setURL(messageToShow.url) ); From d02d4e07ef8557572f36afe4172f01c11a664a62 Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Fri, 20 Jan 2023 20:43:50 -0800 Subject: [PATCH 09/14] Add footer (not skidding Pepperjack (real)) --- src/utils/resolveMessage.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 9295e55..eaa4cf3 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -59,7 +59,9 @@ export async function expandDiscordLink(message: Message): Promise { name: `${messageToShow.author.username}#${messageToShow.author.discriminator}`, iconURL: messageToShow.author.displayAvatarURL(), }) - .setColor(Colors.Aqua); + .setColor(Colors.Aqua) + .setTimestamp(messageToShow.createdTimestamp) + .setFooter({ text: `#${messageToShow.channel.name}`}) if (messageToShow.content) { builder.setDescription(messageToShow.content); } From 3cdc51ced431fd56f1ac14e11ed2c864cadeb763 Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Sun, 12 Feb 2023 20:34:06 +0000 Subject: [PATCH 10/14] Fix loop Signed-off-by: PandaNinjas --- src/utils/resolveMessage.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index eaa4cf3..67be077 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -26,6 +26,7 @@ export async function expandDiscordLink(message: Message): Promise { while (!(execResult == null || execResult.groups == undefined)) { if (execResult.groups.server_id != message.guildId) { + execResult = re.exec(message.content); continue; // do not let the bot leak messages from one server to another } From f099cc9900cb8a1a4a968577afb77c71fa67215c Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Sun, 12 Feb 2023 18:05:50 -0800 Subject: [PATCH 11/14] More trolling Signed-off-by: PandaNinjas --- src/utils/resolveMessage.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 67be077..e4e4531 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -35,6 +35,7 @@ export async function expandDiscordLink(message: Message): Promise { ); if (channel == undefined || channel == null || !channel.isTextBased()) { + execResult = re.exec(message.content); continue; } @@ -42,10 +43,12 @@ export async function expandDiscordLink(message: Message): Promise { if ( !channel.parent?.members?.some((user) => user.id == message.author.id) ) { + execResult = re.exec(message.content); 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); continue; // do not reveal a message to a user who can't see it } } @@ -90,6 +93,7 @@ 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; } From 2b99eb057931f4274e8cfcbe137ff5d13a676213 Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Thu, 16 Feb 2023 22:23:18 +0100 Subject: [PATCH 12/14] 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); } } From 6f1823bfd4e6312a96c4324c5bf3ba8c4dba19c2 Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Thu, 16 Feb 2023 22:25:46 +0100 Subject: [PATCH 13/14] feat: use reply instead Signed-off-by: Sefa Eyeoglu --- src/utils/resolveMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index b836cc2..58f0a73 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -79,7 +79,7 @@ export async function expandDiscordLink(message: Message): Promise { .setURL(messageToShow.url) ); - await message.channel.send({ embeds: [builder], components: [row] }); + await message.reply({ embeds: [builder], components: [row], allowedMentions: {repliedUser: false}}); } catch (e) { console.error(e); } From 2d41ce361a3f089328f92fbbfefecf9644588e10 Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Thu, 16 Feb 2023 22:30:02 +0100 Subject: [PATCH 14/14] fix: restrict to three message previews Signed-off-by: Sefa Eyeoglu --- src/utils/resolveMessage.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/utils/resolveMessage.ts b/src/utils/resolveMessage.ts index 58f0a73..a7bb38b 100644 --- a/src/utils/resolveMessage.ts +++ b/src/utils/resolveMessage.ts @@ -25,7 +25,12 @@ export async function expandDiscordLink(message: Message): Promise { const results = message.content.matchAll(re); + let n = 0; + for (const r of results) { + if (n >= 3) + break; // only process three previews + if (r.groups == undefined && r.groups.server_id != message.guildId) continue; // do not let the bot leak messages from one server to another @@ -80,6 +85,7 @@ export async function expandDiscordLink(message: Message): Promise { ); await message.reply({ embeds: [builder], components: [row], allowedMentions: {repliedUser: false}}); + n++; } catch (e) { console.error(e); }