Skip to content

Instantly share code, notes, and snippets.

@Digital39999
Created August 22, 2022 17:14
Show Gist options
  • Save Digital39999/0120e6e61779403e76c9266f9b2c53ef to your computer and use it in GitHub Desktop.
Save Digital39999/0120e6e61779403e76c9266f9b2c53ef to your computer and use it in GitHub Desktop.
This code checks which users own multiple servers which are causing inorganic growth issue.
const Discord = require('discord.js');
const config = {
token: ``, // your discord bot token, get it from https://discordapp.com/developers/applications/me
leaveIfMoreThen5: false, // would you like to leave guilds if same owner owns more then 5 of them?
};
const client = new Discord.Client({
intents: Discord.GatewayIntentBits.Guilds,
});
/* Output format:
{
ownerID: [guilds with that owner],
}
*/
client.on("ready", () => {
let duplicatesList = checkForDuplicates(client.guilds.cache);
if (Object.keys(duplicatesList).length > 0) console.log(duplicatesList);
if (config.leaveIfMoreThen5) leaveGuilds(duplicatesList);
client.destroy();
function leaveGuilds(duplicatesList) {
Object.entries(duplicatesList).forEach(([ownerID, guilds]) => {
if (guilds.length > 4) {
for (let i = 5; i <= guilds.length; i++) {
guilds[i].leave();
console.log(`\nLeft '${guilds[i]}' because same owner owns more then 5 guilds.`);
};
};
});
};
function checkForDuplicates (arrayOfObjects) {
let duplicates = {}; arrayOfObjects.forEach(object => {
if (duplicates[object.ownerId]) duplicates[object.ownerId].push(object?.id);
else duplicates[object.ownerId] = [object?.id];
});
return Object.keys(duplicates).filter((key) => (duplicates[key].length > 1)).map((key) => {
return { ownerId: key, ids: duplicates[key] }
}).reduce((acc, curr) => {
acc[curr.ownerId] = curr.ids; return acc;
}, {});
};
});
client.login(config.token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment