Skip to content
Jake edited this page Jul 28, 2017 · 6 revisions

discord.py

Click here to view the library.

Server Dumper (By jakeoid#6284/@jakeoid)

Using the rewrite of discord.py this script will create a user client, automatically shard it and then dump to a json file (in the same folder in which you ran this script) called aptly after your bot something that you can just as easily reupload to the server.

"""

BOTLIST DUMPER.

:copyright: (c) 2017 Jakeoid.
:license: MIT, see LICENSE.md for details.

"""

# DISCORD
import discord

# HOUSEKEEPING
import json
import time
import datetime

# VARIABLES
client = discord.AutoShardedClient()
ignore_guilds = [
    110373943822540800,
    264445053596991498
]
startTime = time.time()
info = "\u001b[0m[ \u001b[32;1mINFO \u001b[0m]"

# EDITME
token = ""
trigger_amt = 0.6
minimum_users = 10

print(f'{info} The bot has begun, give it some time to run.')


@client.event
async def on_ready():
    """Called when the bot is ready for Discord."""

    blacklist = []

    for guild in [guild for guild in client.guilds if len(list(filter(lambda member: member.bot, guild.members))) / len(guild.members) >= trigger_amt]:

        if guild.id not in ignore_guilds and len(guild.members) > minimum_users:

            print(f'{info} Dumping information for {guild.name} ({guild.id})')

            blacklist.append(guild)

        else:

            print(f'{info} Ignoring whitelisted guild {guild.name} ({guild.id})')

    print(f'{info} Generating JSON file..')

    json_output = []

    for guild in blacklist:

        ts = time.time()

        json_output.append({
            'id': guild.id,
            'name': guild.name,
            'totalMembers': len(guild.members),
            'userAccounts': len(list(filter(lambda m: m.bot, guild.members))),
            'botAccounts': len(list(filter(lambda m: not m.bot, guild.members))),
            'percentageBots': len(list(filter(lambda m: m.bot, guild.members))) / len(guild.members),
            'timestamp': datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%dT%H:%M:%S-%H:%M'),
            'icon': guild.icon_url
        })

    with open(f'{client.user.name.lower()}.json', 'w') as outfile:
        json.dump(json_output, outfile, ensure_ascii=False,
                  indent=4, sort_keys=True)

    print(f'{info} Finished dumping servers for {client.user.name}#{client.user.discriminator}!')

    print(f'{info} Took a total of {time.time() - startTime} seconds!')

    await client.logout()


@client.event
async def on_shard_ready(shard):
    """Called when the bots shard is ready for Discord."""

    print(f'{info} Shard ready, the shards number is {shard}')

client.run(token)

Clone this wiki locally