From 6b7c96e6ba985d0c731e9f5a2af31d3a3ff06ad4 Mon Sep 17 00:00:00 2001 From: xJvner <72538254+xJuner@users.noreply.github.com> Date: Wed, 10 Sep 2025 23:32:11 -0600 Subject: [PATCH] Create ConvertInstagramLinks.ts New file required for the kkinstagram converter function --- lib/ConvertInstagramLinks.ts | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lib/ConvertInstagramLinks.ts diff --git a/lib/ConvertInstagramLinks.ts b/lib/ConvertInstagramLinks.ts new file mode 100644 index 00000000..0845e1bd --- /dev/null +++ b/lib/ConvertInstagramLinks.ts @@ -0,0 +1,61 @@ +import { Message, Guild, PermissionsBitField, TextChannel, MessageReaction, User } from 'discord.js'; + +import { + getUsername, + truncateResponse, + sleep, + tryReactMessage, +} from './Utilities.js'; + +export async function convertTwitterLinks(msg: Message): Promise { + if (!msg.guild!.members.me!.permissionsIn(msg.channel as TextChannel).has(PermissionsBitField.Flags.SendMessages)) { + return; + } + + try { + const content = msg.content.trim(); + const instagramRegex = /https:\/\/(instagram\.com|x\.com)\/(.+)\/reel\/(\d+)/gi; + + let match; + + const fixedURLs = []; + + while ((match = instagramRegex.exec(content)) !== null) { + const user = match[2]; + const statusId = match[3]; + const fixedURL = `https://kkinstagram.com/${user}/reel/${statusId}`; + fixedURLs.push(fixedURL); + } + + if (fixedURLs.length > 0) { + const username = await getUsername(msg.author.id, msg.guild); + const content = `${fixedURLs.join('\n')}`; + + const suppressPromise = msg.suppressEmbeds(true); + const sendPromise = msg.channel.send(content); + + const [, sentMsg] = await Promise.all([ + suppressPromise, + sendPromise, + ]); + + await tryReactMessage(sentMsg, '❌'); + + // Create a reaction collector + const filter = (reaction: MessageReaction, user: User) => reaction.emoji.name === '❌' && user.id === msg.author.id; + + const collector = sentMsg.createReactionCollector({ filter, time: 60000, max: 1 }); + + collector.on('collect', async () => { + await sentMsg.delete(); + }); + + await sleep(2000); + + /* Sometimes takes time for embed to load */ + await msg.suppressEmbeds(true); + } + } catch (err) { + console.log(err); + } +}