From 250e610a7c7bdb8908725decedd63dfbfab23cad Mon Sep 17 00:00:00 2001 From: Wizzard Date: Mon, 19 Feb 2024 14:34:01 -0500 Subject: [PATCH] Filter function --- main.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/main.js b/main.js index 48e30bd..0ced04f 100644 --- a/main.js +++ b/main.js @@ -33,6 +33,8 @@ if (fs.existsSync(settingsFilePath)) { fs.writeFileSync(settingsFilePath, JSON.stringify(channelSettings, null, 2)); } +channelSettings.filters = channelSettings.filters || {}; + client.on('ready', () => { console.log(`${client.user.tag} is ready!`); client.user.setPresence({ @@ -62,6 +64,21 @@ function logMessageToFile(channelId, message) { } client.on('messageCreate', async message => { + + if (message.channel.id === COMMAND_CHANNEL_ID && message.content.startsWith('.filter')) { + const args = message.content.split(' '); + if (args.length < 3) { + return message.reply("Usage: .filter ").then(msg => setTimeout(() => msg.delete(), 5000)); + } + + const [_, channelId, userId] = args; + channelSettings.filters[channelId] = userId; + fs.writeFileSync(settingsFilePath, JSON.stringify(channelSettings, null, 2)); + + return message.reply(`Messages from user ID ${userId} in channel ID ${channelId} will now be specifically logged.`).then(msg => setTimeout(() => msg.delete(), 5000)); + } + + if (message.channel.id === COMMAND_CHANNEL_ID && message.content.startsWith('.toggleimages')) { const args = message.content.split(' '); if (args.length === 1) { @@ -89,6 +106,19 @@ client.on('messageCreate', async message => { if (channelMappings[message.channel.id]) { if (message.author.id === client.user.id) return; + const filterUserId = channelSettings.filters[message.channel.id]; + let shouldLog = true; + + if (filterUserId) { + const isFromFilteredUser = message.author.id === filterUserId; + const isReplyToFilteredUser = message.reference && message.reference.messageId ? await checkIfReplyToFilteredUser(message, filterUserId) : false; + const isMentioningFilteredUser = message.mentions.users.has(filterUserId); + + shouldLog = isFromFilteredUser || isReplyToFilteredUser || isMentioningFilteredUser; + } + + if (!shouldLog) return; + const includeImages = channelSettings.includeImages[message.channel.id] || false; let content = message.content; @@ -117,6 +147,16 @@ client.on('messageCreate', async message => { } }); +async function checkIfReplyToFilteredUser(message, userId) { + try { + const referencedMessage = await message.channel.messages.fetch(message.reference.messageId); + return referencedMessage.author.id === userId; + } catch (error) { + console.error('Failed to fetch referenced message:', error); + return false; + } +} + async function processMessageQueue() { if (messageQueue.length === 0) return;