Snippets

killerspaz Discord Channel Wiper

Created by killerspaz

File snippet.js Added

  • Ignore whitespace
  • Hide word diff
+const BEFORE_MSG_ID = '';
+const AUTH_TOKEN = '';
+const BATCH_SIZE = 1;
+
+/**
+ * Discord Chanel Wiper
+ *
+ * Completely wipes a channel starting at a message ID.
+ *
+ * Automatically throttles based on responses from Discord servers.
+ */
+class DiscordChannelWiper {
+  constructor({beforeMsgId, authToken, batchSize, channelId}) {
+    this.beforeMsgId = beforeMsgId;
+    this.authToken = authToken;
+    this.batchSize = batchSize;
+    this.baseUrl = `https://discordapp.com/api/channels/${channelId}/messages`;
+  }
+
+  wipe() {
+    let messages;
+
+    return this._getMessages()
+      .then(_messages => {
+        messages = _messages;
+        return this._deleteMessageBatch(_messages);
+      })
+      .then(result => this._determineCompletion(result, messages))
+      .then(() => console.info('[DiscordChannelWiper] - Done Wiping Channel!'))
+      .catch(err => console.error('[DiscordChannelWiper] - Serious Failure: ', err));
+  }
+
+  _getHeaders() {
+    return {
+      headers: { 
+        Authorization: this.authToken 
+      }
+    };
+  }
+
+  _getMessages() {
+    return fetch(
+      `${this.baseUrl}?before=${this.beforeMsgId}&limit=10`, 
+      this._getHeaders()
+    )
+    .then(resp => resp.json());
+  }
+
+  _deleteMessageBatch(messages) {
+    let initPromise = Promise.resolve();
+
+    // Iterate over every message and SEQUENTIALLY DELETE!
+    messages.forEach(msg => {
+      initPromise = initPromise
+        .then(() => this._deleteMessage(msg))
+        .then(result => this._determineDeleteSuccess(result, msg))
+    });
+
+    return initPromise;
+  }
+
+  _deleteMessage(message) {
+    console.log(`[DiscordChannelWiper] - Deleting Message from ${message.author.username}, ${message.id}`);
+
+    return fetch(`${this.baseUrl}/${message.id}`, Object.assign(
+        this._getHeaders(),
+        { method: 'DELETE' }
+      ))
+      .then(resp => {
+        return resp.text().then(text => {
+          return text ? JSON.parse(text) : {}
+        });
+      });
+  }
+
+  _determineDeleteSuccess(result, lastMsg) {
+    if (result.retry_after) {
+      return this._delayAndRetry(result.retry_after);
+    }
+
+    this.beforeMsgId = lastMsg.id;
+
+    return true;
+  }
+
+  _determineCompletion(result, messages) {
+    if (messages.length > 0) {
+      return this.wipe();
+    }
+
+    return result;
+  }
+
+  _delayAndRetry(delay) {
+    return new Promise(resolve => {
+      console.log(`[DiscordChannelWiper] - Delaying batch for ${delay / 1000} seconds`);
+      setTimeout(resolve, delay);
+    }).then(() => this.wipe());
+  }
+}
+
+const initConfig = {
+  beforeMsgId: BEFORE_MSG_ID,
+  authToken: AUTH_TOKEN,
+  batchSize: BATCH_SIZE,
+  channelId: window.location.href.split('/').pop()
+};
+
+console.info(initConfig);
+
+new DiscordChannelWiper(initConfig).wipe();
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.