From 6a56a38f51f578d18cb06cb0cd2e3ff21f6fbeab Mon Sep 17 00:00:00 2001 From: Wizzard Date: Tue, 6 Feb 2024 15:13:02 -0500 Subject: [PATCH] Add IMAGES enviornment variable for showing links and images --- EXAMPLE.env | 1 + main.js | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/EXAMPLE.env b/EXAMPLE.env index 08ca536..030344b 100644 --- a/EXAMPLE.env +++ b/EXAMPLE.env @@ -1,3 +1,4 @@ SOURCE_CHANNEL_ID_1=111111111111111111 TARGET_CHANNEL_ID_1=111111111111111111 DISCORD_TOKEN=mASJIUFHKWHG.18295790udqawihKS +IMAGES=0 diff --git a/main.js b/main.js index a891675..9a71952 100644 --- a/main.js +++ b/main.js @@ -10,6 +10,8 @@ const channelMappings = { [process.env.SOURCE_CHANNEL_ID_2]: process.env.TARGET_CHANNEL_ID_2, }; +const includeImages = process.env.IMAGES === '1'; + client.on('ready', () => { console.log(`${client.user.tag} is ready!`); setInterval(processMessageQueue, BATCH_INTERVAL); @@ -19,14 +21,22 @@ client.on('messageCreate', async message => { if (channelMappings[message.channel.id]) { if (message.author.id === client.user.id) return; + let content = message.content; + if (!includeImages) { + content = content.replace(/https?:\/\/\S+/g, '[Link removed]'); + } + const timestamp = new Date(message.createdTimestamp).toISOString(); - let attachmentUrls = message.attachments.map(a => a.url).join(' '); - if (attachmentUrls) { - attachmentUrls = ' ' + attachmentUrls; + let attachmentUrls = ''; + if (includeImages) { + attachmentUrls = message.attachments.map(a => a.url).join(' '); + if (attachmentUrls) { + attachmentUrls = ' ' + attachmentUrls; + } } const formattedMessage = { - content: `<@${message.author.id}> / **${message.author.tag}**: ${message.content}${attachmentUrls} \`${timestamp}\``, + content: `<@${message.author.id}> / **${message.author.tag}**: ${content}${attachmentUrls} \`${timestamp}\``, target: channelMappings[message.channel.id] }; @@ -36,7 +46,7 @@ client.on('messageCreate', async message => { async function processMessageQueue() { if (messageQueue.length === 0) return; - + while (messageQueue.length > 0) { let batchMessage = ''; let targetChannelId = messageQueue[0].target;