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

Skip archived messages in deleteMessages.js #392

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 10 additions & 1 deletion src/deleteMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async function deleteMessages(authToken, authorId, guildId, channelId, minId, ma
let throttledTotalTime = 0;
let offset = 0;
let iterations = -1;
let archivedMessages = new Set();

const wait = async ms => new Promise(done => setTimeout(done, ms));
const msToHMS = s => `${s / 3.6e6 | 0}h ${(s % 3.6e6) / 6e4 | 0}m ${(s % 6e4) / 1000 | 0}s`;
Expand Down Expand Up @@ -153,6 +154,10 @@ async function deleteMessages(authToken, authorId, guildId, channelId, minId, ma
for (let i = 0; i < messagesToDelete.length; i++) {
const message = messagesToDelete[i];
if (stopHndl && stopHndl()) return end(log.error('Stopped by you!'));

if (archivedMessages.has(message.id)) {
continue;
}

log.debug(`${((delCount + 1) / grandTotal * 100).toFixed(2)}% (${delCount + 1}/${grandTotal})`,
`Deleting ID:${redact(message.id)} <b>${redact(message.author.username + '#' + message.author.discriminator)} <small>(${redact(new Date(message.timestamp).toLocaleString())})</small>:</b> <i>${redact(message.content).replace(/\n/g, '↵')}</i>`,
Expand Down Expand Up @@ -188,6 +193,10 @@ async function deleteMessages(authToken, authorId, guildId, channelId, minId, ma
log.verb(`Cooling down for ${w * 2}ms before retrying...`);
await wait(w * 2);
i--; // retry
} else if ((await resp.json()).message.toLowerCase().includes('archived')) {
log.error(`Message is archived. Skipping.`);
archivedMessages.add(message.id);
failCount++;
} else {
log.error(`Error deleting message, API responded with status ${resp.status}!`, await resp.json());
log.verb('Related object:', redact(JSON.stringify(message)));
Expand Down Expand Up @@ -222,4 +231,4 @@ async function deleteMessages(authToken, authorId, guildId, channelId, minId, ma
return await recurse();
}

export default deleteMessages;
export default deleteMessages;