Skip to content

Commit

Permalink
Merge pull request #171 from SethFalco/issue-170
Browse files Browse the repository at this point in the history
Delete only if Regular Expression Matches
  • Loading branch information
victornpb committed Apr 26, 2022
2 parents 80b97ef + e713943 commit f8587bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
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

0 comments on commit f8587bb

Please sign in to comment.