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

discord.py

Click here to view the library.

Server Dumper (By jakeoid#6284/@jakeoid & Martmist#6078/@martmists)

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.

import datetime
import json

import discord


client = discord.AutoShardedClient()

ignore_guilds = [
    110373943822540800,
    264445053596991498
]

startTime = datetime.datetime.now()

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 [g for g in client.guilds
                  if ((len(g.members) < minimum_users) and
                      (sum(m.bot for m in g.members) /
                       len(guild.members) >= trigger_amt) and
                      g.id not in ignore_guilds)]:
        print(f'{info} Dumping information for {guild.name} ({guild.id})')
        blacklist.append(guild)

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

    json_output = []

    for guild in blacklist:
        ts = datetime.datetime.now()
        json_output.append({
            'id': guild.id,
            'name': guild.name,
            'totalMembers': len(guild.members),
            'userAccounts': sum(not m.bot for m in guild.members),
            'botAccounts': sum(m.bot for m in guild.members),
            'percentageBots': sum(m.bot for m in guild.members) / len(guild.members),
            'timestamp': 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}!') 
       print(f'{info} Took a total of {(ts - startTime).total_seconds()} 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