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

Delete only if Regular Expression Matches #171

Merged
merged 2 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions deleteDiscordMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<label><input id="hasFile" type="checkbox">has: file</label><br>
<label><input id="includePinned" type="checkbox">Include pinned</label>
</span>
<span>Pattern<br>
<input id="pattern" type="text" placeholder="pattern to remove" priv>
</span>
</div>
<hr>
<button id="start" style="background:#43b581;width:80px;">Start</button>
Expand Down Expand Up @@ -73,6 +76,7 @@
const hasFile = $('input#hasFile').checked;
const includeNsfw = $('input#includeNsfw').checked;
const includePinned = $('input#includePinned').checked;
const pattern = $('input#pattern').value;
const progress = $('#progress');

const fileSelection = $("input#file");
Expand Down Expand Up @@ -100,7 +104,7 @@

stop = stopBtn.disabled = !(startBtn.disabled = true);
for (let i = 0; i < channelIds.length; i++) {
await deleteMessages(authToken, authorId, guildId, channelIds[i], minId || minDate, maxId || maxDate, content, hasLink, hasFile, includeNsfw, includePinned, logger, stopHndl, onProg);
await deleteMessages(authToken, authorId, guildId, channelIds[i], minId || minDate, maxId || maxDate, content, hasLink, hasFile, includeNsfw, includePinned, pattern, logger, stopHndl, onProg);
stop = stopBtn.disabled = !(startBtn.disabled = false);
}
};
Expand Down Expand Up @@ -147,7 +151,7 @@
* @author Victornpb <https://www.github.com/victornpb>
* @see https://github.com/victornpb/deleteDiscordMessages
*/
async function deleteMessages(authToken, authorId, guildId, channelId, minId, maxId, content,hasLink, hasFile, includeNsfw, includePinned, extLogger, stopHndl, onProgress) {
async function deleteMessages(authToken, authorId, guildId, channelId, minId, maxId, content,hasLink, hasFile, includeNsfw, includePinned, pattern, extLogger, stopHndl, onProgress) {
const start = new Date();
const delayReductionGeometricFactor = 0.6; // 1/n
const delayAdjustmentThreshold = 5; // ms
Expand Down Expand Up @@ -252,12 +256,20 @@
log.verb(`Search delay lowered to ${searchDelay}ms`);
}

let regex;

try {
regex = new RegExp(pattern);
} catch(e) {
log.warn('Ignoring RegExp because pattern is malformed')
}

const data = await resp.json();
const total = data.total_results;
if (!grandTotal) grandTotal = total;
const discoveredMessages = data.messages.map(convo => convo.find(message => message.hit===true));
const messagesToDelete = discoveredMessages.filter(msg => {
return msg.type === 0 || msg.type === 6 || (msg.pinned && includePinned);
return (msg.type === 0 || msg.type === 6 || (msg.pinned && includePinned)) && (!regex || msg.content.match(regex));
});
const skippedMessages = discoveredMessages.filter(msg=>!messagesToDelete.find(m=> m.id===msg.id));

Expand Down
18 changes: 15 additions & 3 deletions deleteDiscordMessages.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author Victornpb <https://www.github.com/victornpb>
* @see https://github.com/victornpb/deleteDiscordMessages
*/
async function deleteMessages(authToken, authorId, guildId, channelId, minId, maxId, content, hasLink, hasFile, includeNsfw, includePinned, searchDelay, deleteDelay, extLogger, stopHndl, onProgress) {
async function deleteMessages(authToken, authorId, guildId, channelId, minId, maxId, content, hasLink, hasFile, includeNsfw, includePinned, pattern, searchDelay, deleteDelay, extLogger, stopHndl, onProgress) {
const start = new Date();
let delCount = 0;
let failCount = 0;
Expand Down Expand Up @@ -122,12 +122,20 @@ async function deleteMessages(authToken, authorId, guildId, channelId, minId, ma
}
}

let regex;

try {
regex = new RegExp(pattern);
} catch(e) {
log.warn('Ignoring RegExp because pattern is malformed')
}

const data = await resp.json();
const total = data.total_results;
if (!grandTotal) grandTotal = total;
const discoveredMessages = data.messages.map(convo => convo.find(message => message.hit === true));
const messagesToDelete = discoveredMessages.filter(msg => {
return msg.type === 0 || msg.type === 6 || (msg.pinned && includePinned);
return (msg.type === 0 || msg.type === 6 || (msg.pinned && includePinned)) && (!regex || msg.content.match(regex));
});
const skippedMessages = discoveredMessages.filter(msg => !messagesToDelete.find(m => m.id === msg.id));

Expand Down Expand Up @@ -304,6 +312,9 @@ function initUI() {
<label><input id="hasFile" type="checkbox">has: file</label><br>
<label><input id="includePinned" type="checkbox">Include pinned</label>
</span><br>
<span>Pattern<br>
<input id="pattern" type="text" placeholder="pattern to remove" priv>
</span>
<span>Search Delay <a
href="https://github.com/victornpb/deleteDiscordMessages/blob/master/help/delay.md" title="Help"
target="_blank">?</a><br>
Expand Down Expand Up @@ -385,6 +396,7 @@ function initUI() {
const hasFile = $('input#hasFile').checked;
const includeNsfw = $('input#includeNsfw').checked;
const includePinned = $('input#includePinned').checked;
const pattern = $('input#pattern').value;
const searchDelay = parseInt($('input#searchDelay').value.trim());
const deleteDelay = parseInt($('input#deleteDelay').value.trim());
const progress = $('#progress');
Expand Down Expand Up @@ -421,7 +433,7 @@ function initUI() {

stop = stopBtn.disabled = !(startBtn.disabled = true);
for (let i = 0; i < channelIds.length; i++) {
await deleteMessages(authToken, authorId, guildId, channelIds[i], minId || minDate, maxId || maxDate, content, hasLink, hasFile, includeNsfw, includePinned, searchDelay, deleteDelay, logger, stopHndl, onProg);
await deleteMessages(authToken, authorId, guildId, channelIds[i], minId || minDate, maxId || maxDate, content, hasLink, hasFile, includeNsfw, includePinned, pattern, searchDelay, deleteDelay, logger, stopHndl, onProg);
stop = stopBtn.disabled = !(startBtn.disabled = false);
}
};
Expand Down