diff --git a/src/undiscord-core.js b/src/undiscord-core.js index 955467f2..8e2aa3ef 100644 --- a/src/undiscord-core.js +++ b/src/undiscord-core.js @@ -22,6 +22,8 @@ class UndiscordCore { authorId: null, // Author of the messages you want to delete guildId: null, // Server were the messages are located channelId: null, // Channel were the messages are located + threadId: null, // Thread/forum where the messages are located + isThread: false, // Delete only messages in thread minId: null, // Only delete messages after this, leave blank do delete all maxId: null, // Only delete messages before this, leave blank do delete all content: null, // Filter messages that contains this text content @@ -312,6 +314,11 @@ class UndiscordCore { messagesToDelete = messagesToDelete.filter(msg => msg.type === 0 || (msg.type >= 6 && msg.type <= 21)); messagesToDelete = messagesToDelete.filter(msg => msg.pinned ? this.options.includePinned : true); + // only delete messages in the thread + if (this.options.isThread) { + messagesToDelete = messagesToDelete.filter(msg => msg.channel_id === this.options.threadId); + } + // custom filter of messages try { const regex = new RegExp(this.options.pattern, 'i'); diff --git a/src/undiscord-ui.js b/src/undiscord-ui.js index ec0a59b4..67644a0d 100644 --- a/src/undiscord-ui.js +++ b/src/undiscord-ui.js @@ -27,6 +27,10 @@ const WIKI = 'https://github.com/victornpb/undiscord/wiki'; const undiscordCore = new UndiscordCore(); messagePicker.init(); +const state = { + threadId: null, + isThread: false, +}; const ui = { undiscordWindow: null, undiscordBtn: null, @@ -103,12 +107,15 @@ function initUI() { $('button#stop').onclick = stopAction; $('button#clear').onclick = () => ui.logArea.innerHTML = ''; $('button#getAuthor').onclick = () => $('input#authorId').value = getAuthorId(); - $('button#getGuild').onclick = () => { + $('button#getGuild').onclick = async () => { const guildId = $('input#guildId').value = getGuildId(); - if (guildId === '@me') $('input#channelId').value = getChannelId(); + if (guildId === '@me') { state.isThread = 0; $('input#channelId').value = await getChannelId()[0]; } }; - $('button#getChannel').onclick = () => { - $('input#channelId').value = getChannelId(); + $('button#getChannel').onclick = async () => { + const id = await getChannelId(); + state.isThread = id[1]; + state.threadId = id[2]; + $('input#channelId').value = id[0]; $('input#guildId').value = getGuildId(); }; $('#redact').onchange = () => { @@ -252,6 +259,8 @@ async function startAction() { const authorId = $('input#authorId').value.trim(); const guildId = $('input#guildId').value.trim(); const channelIds = $('input#channelId').value.trim().split(/\s*,\s*/); + const isThread = state.isThread; + const threadId = state.threadId; const includeNsfw = $('input#includeNsfw').checked; // filter const content = $('input#search').value.trim(); @@ -286,6 +295,8 @@ async function startAction() { authorId, guildId, channelId: channelIds.length === 1 ? channelIds[0] : undefined, // single or multiple channel + isThread, + threadId, minId: minId || minDate, maxId: maxId || maxDate, content, diff --git a/src/utils/getIds.js b/src/utils/getIds.js index 600aaeda..470beb9d 100644 --- a/src/utils/getIds.js +++ b/src/utils/getIds.js @@ -23,9 +23,19 @@ export function getGuildId() { else alert('Could not find the Guild ID!\nPlease make sure you are on a Server or DM.'); } -export function getChannelId() { +export async function getChannelId() { const m = location.href.match(/channels\/([\w@]+)\/(\d+)/); - if (m) return m[2]; + if (m) { + try { + const response=await fetch(`https://discord.com/api/v9/channels/${m[2]}`,{headers:{Authorization:getToken()}}); + const data = await response.json(); + if ([10, 11, 12].includes(data.type)) {log.info('selecting parent channel'); return [data.parent_id,true,m[2]];} + else {return [m[2],false,null];} + } catch (err) { + log.info('Could not get channel type, assuming not thread.'); + return [m[2],false,null]; + } + } else alert('Could not find the Channel ID!\nPlease make sure you are on a Channel or DM.'); }