import asyncio
import logging
import json

from .base import BaseCog
import config

CARBON_API = 'https://www.carbonitex.net/discord/data/botdata.php'
DBOTSPW_API = 'https://bots.discord.pw/api/bots/{.user.id}/stats'
DBOTSORG_API = 'https://discordbots.org/api/bots/{.user.id}/stats'


class BotList(BaseCog):
    """Most code here was adapted from R. Danny's carbonitex cog."""
    def __init__(self, bot):
        super().__init__(bot)
        self.carbon_key = config.carbon_key
        self.dbotspw_key = config.dbotspw_key
        self.dbotsorg_key = config.dbotsorg_key

    async def update(self, *args, **kwargs):
        guilds = len(self.bot.guilds)
        if self.carbon_key is not None:
            carbon_payload = {'key': self.carbon_key,
                              'servercount': guilds}
            try:
                resp = await self.bot.request(CARBON_API, data=carbon_payload, type_='text')
            except asyncio.TimeoutError:
                logging.info(f'Carbon failed to respond for\n{json.dumps(carbon_payload, indent=2)}')
            else:
                logging.info(f'Carbon returned {resp.status} for\n{json.dumps(carbon_payload, indent=2)}')

        for site, link, key in (('DBots.pw', DBOTSPW_API, self.dbotspw_key),
                                ('DBots.org', DBOTSORG_API, self.dbotsorg_key)):
            if key is not None:
                dbots_payload = {'server_count': guilds}
                dbots_headers = {'authorization': key,
                                 'content-type': 'application/json'}
                try:
                    resp = await self.bot.request(link.format(self.bot), data=dbots_payload,
                                                  headers=dbots_headers, type_='text')
                except asyncio.TimeoutError:
                    logging.info(f'{site} failed to respond for\n{json.dumps(dbots_payload, indent=2)}')
                else:
                    logging.info(f'{site} returned {resp.status} for\n{json.dumps(dbots_payload, indent=2)}')

    on_ready = update
    on_guild_join = update
    on_guild_remove = update


def setup(bot):
    bot.add_cog(BotList(bot))

@@ -457,23 +457,33 @@
 n_key = 
+getattr(
 config
-.
+, '
 carbon_k
@@ -480,24 +480,32 @@
  'carbon_key
+', None)
 %0A        sel
@@ -516,31 +516,41 @@
 otspw_key = 
+getattr(
 config
-.
+, '
 dbotspw_key%0A
@@ -548,16 +548,24 @@
 tspw_key
+', None)
 %0A       
@@ -589,15 +589,25 @@
 y = 
+getattr(
 config
-.
+, '
 dbot
@@ -614,16 +614,24 @@
 sorg_key
+', None)
 %0A%0A    as

fix BotList cog not getting loaded if missing any api keys