Skip to content

Instantly share code, notes, and snippets.

@wooningeire
Last active November 2, 2017 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wooningeire/6ddfd70ba72b1cc663df734a0f3ce487 to your computer and use it in GitHub Desktop.
Save wooningeire/6ddfd70ba72b1cc663df734a0f3ce487 to your computer and use it in GitHub Desktop.
Discord.JS self-bot code that fetches the message counts for each user in a guild. (originally written September 7, 2017, revised slightly today)
/**
* @file Defines a function used to count the messages of every user in a Discord guild.
* @author wooningc
* @requires discord.js.org
*/
"use strict";
/**
* @function countMessages
* @param {Guild} guild The guild whose members will be iterated through.
* @param {Object} [options] List of customizable options.
* @param {boolean} [options.debug] Whether or not to print progress/errors to the console.
* @param {number} [options.timeoutThreshold=30000] If this number of milliseconds has passed since the last fetched member, then resolve. Use 0 or any number lower for no timeout.
* @param {function} [options.onusercomplete] (event) The function to be called when a user's message count has been logged.
* @returns {Promise<Object[]>} Array of objects with pairs of User objects and their respective message counts, sorted by message count in descending order.
* @desc Takes a guild and returns a sorted list of all its members and their message counts.
*/
function countMessages(guild, options={}) {
return new Promise(function (resolve, reject) {
var members;
var size;
var date;
var interval;
var counts = [];
var log = options.debug ? console.log : () => undefined;
if (isNaN(options.timeoutThreshold)) options.timeoutThreshold = 30000;
if (typeof options.onusercomplete !== "function") options.onusercomplete = () => undefined;
if (guild.large) guild.fetchMembers().then(initialize);
else initialize();
function initialize() {
members = guild.members;
size = members.size;
date = Date.now();
if (options.timeoutThreshold > 0) {
interval = setInterval(function () {
if (Date.now() - date > options.timeoutThreshold) {
log(`Timed out when counting ${guild.name}`);
sort();
}
}, 1000);
}
iterate();
}
function iterate() {
members.forEach(function (member) {
var user = member.user;
guild.search({
author: user
}).then(
results => { increment(user, results); },
error => { console.log(error); }
).catch(
error => { console.log(error); }
);
});
}
function increment(user, results) {
date = new Date();
counts.push({ amount: results.totalResults, user });
log(`${guild.name}: ${counts.length} out of ${size} complete (${user.tag})`);
if (options.onusercomplete) options.onusercomplete({ user, results, i: counts.length, size });
if (counts.length >= size) sort();
}
function sort() {
clearInterval(interval);
counts.sort((a, b) => b.amount - a.amount);
log(`Finished counting messages in ${guild.name}`);
resolve(counts);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment