Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

be able to autofill thread channel id and only delete messages in that thread #610

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/undiscord-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down
19 changes: 15 additions & 4 deletions src/undiscord-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions src/utils/getIds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down