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 9e65526..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; @@ -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}`, @@ -58,27 +64,32 @@ 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) { 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) ); - message.channel.send({ embeds: [builder], components: [row] }); + + await message.channel.send({ embeds: [builder], components: [row] }); } catch (e) { console.error(e); continue; } + execResult = re.exec(message.content); } }