Skip to content

Instantly share code, notes, and snippets.

@mkpoli
Created January 27, 2023 12:21
Show Gist options
  • Select an option

  • Save mkpoli/3c300b4973d2090c778b72d10106349f to your computer and use it in GitHub Desktop.

Select an option

Save mkpoli/3c300b4973d2090c778b72d10106349f to your computer and use it in GitHub Desktop.
UserScript for Count Sticker Usage in Discord Server
// ==UserScript==
// @name Discord Count Sticker Usage
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Count Sticker Usage
// @author mkpoli
// @match https://discord.com/channels/**
// @icon https://www.google.com/s2/favicons?sz=64&domain=discord.com
// @grant none
// ==/UserScript==
// Usage:
// 1. Open discord.com/app
// 2. Search manually "has:stickers"
// 3. Run in console "countDiscordServerStickersUsage()"
// 4. Wait until the result appears
(function() {
'use strict';
const sectionSearchResults = "section[aria-label='Search Results']";
async function waitUntil(condition, timeOut=10000, attemptInterval=500) {
const startTime = Date.now();
return new Promise((resolve, reject) => {
const checkCondition = () => {
if (condition()) {
resolve();
} else if (Date.now() - startTime > timeOut) {
reject(new Error("Timed out"));
} else {
setTimeout(checkCondition, attemptInterval);
}
};
checkCondition();
});
}
async function waitMS(ms) {
return new Promise((res) => setTimeout(res, ms))
}
function isLoading() {
return document.querySelector(`${sectionSearchResults} [role=status] > div`).textContent === 'Searching…'
}
window.countDiscordServerStickersUsage = async () => {
console.log("Counting Discord server stickers usage...");
const state = {
pages: 0
}
const results = []
while (true) {
state.pages += 1
console.log(`Processing page ${state.pages}`);
await waitUntil(() => !isLoading());
await waitMS(50);
for (const img of document.querySelectorAll("#search-results img[data-type='sticker']")) {
results.push(img.getAttribute("alt").substring(9))
}
const button = document.querySelector("#search-results+div button[rel='next']");
if (!button) {
console.log("No next button found. Assuming finished.")
break
}
if (button.disabled) {
console.log("Button is disabled. Finished.")
break
}
button.click()
await waitMS(50);
// For a large server, add the following to avoid HTTP 429 Too Many Requests error
// await waitMS(500);
}
return results.reduce((counts, element) => {
counts[element] = (counts[element] || 0) + 1;
return counts;
}, {});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment