import asyncio
import logging
import sys

from ruamel.yaml import YAML

from dog import DogBot
from dog.core.bot import DogSelfbot

with open('config.yml', 'r') as config_file:
    cfg = YAML(typ='safe').load(config_file)

# configure logging
# set the root logger's info to INFO
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)

# formatters
console_fmt = logging.Formatter('[{asctime}] [{levelname: <7}] {name}: {message}', '%I:%M:%S %p', style='{')
nice_fmt = logging.Formatter('%(asctime)s [%(name)s %(levelname)s] %(message)s', '%m/%d/%Y %I:%M:%S %p')

logging.getLogger('discord').setLevel(logging.INFO)
logging.getLogger('dog').setLevel(logging.DEBUG)

# main file handler, only info
file_handler = logging.FileHandler(filename='dog.log', encoding='utf-8')
file_handler.setFormatter(nice_fmt)

# stream handler (stdout)
stream = logging.StreamHandler()
stream.setFormatter(console_fmt)

# handle from all logs
root_logger.addHandler(stream)
root_logger.addHandler(file_handler)

logger = logging.getLogger('dog')

logger.info('Bot is starting...')

try:
    import uvloop

    # uvloop for speedups
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    logger.info('Using uvloop\'s event loop policy.')
except ModuleNotFoundError:
    pass


# gather additional options from the configuration file
additional_options = getattr(cfg, 'options', {})
additional_options.update({
    'owner_id': getattr(cfg, 'owner_id', None),
    'postgresql_auth': cfg['db']['postgres'],
    'redis_url': cfg['db']['redis']
})

logger.info('Bot options: %s', additional_options)

# create dogbot instance
selfbot_mode = '--selfbot' in ' '.join(sys.argv)
if selfbot_mode:
    logger.warning('Running in selfbot mode!')
    # change the database
    additional_options['db']['postgres']['database'] = 'dog_selfbot'
    d = DogSelfbot(cfg=cfg, **additional_options)
    d.load_exts_recursively('dog/selfext', 'Initial selfbot recursive load')
else:
    d = DogBot(cfg=cfg, **additional_options)
    d.load_exts_recursively('dog/ext', 'Initial recursive load')
d.run(cfg['tokens']['selfbot'] if selfbot_mode else cfg['tokens']['bot'], bot=not selfbot_mode)

# close log handlers (why)
# https://github.com/Rapptz/RoboDanny/blob/master/bot.py#L128-L132
handlers = root_logger.handlers[:]
for hndlr in handlers:
    hndlr.close()
    root_logger.removeHandler(hndlr)

import asyncio
import logging
import sys

from ruamel.yaml import YAML

from dog import DogBot
from dog.core.bot import DogSelfbot

with open('config.yml', 'r') as config_file:
    cfg = YAML(typ='safe').load(config_file)

# configure logging
# set the root logger's info to INFO
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)

# formatters
console_fmt = logging.Formatter('[{asctime}] [{levelname: <7}] {name}: {message}', '%I:%M:%S %p', style='{')
nice_fmt = logging.Formatter('%(asctime)s [%(name)s %(levelname)s] %(message)s', '%m/%d/%Y %I:%M:%S %p')

logging.getLogger('discord').setLevel(logging.INFO)
logging.getLogger('dog').setLevel(logging.DEBUG)

# main file handler, only info
file_handler = logging.FileHandler(filename='dog.log', encoding='utf-8')
file_handler.setFormatter(nice_fmt)

# stream handler (stdout)
stream = logging.StreamHandler()
stream.setFormatter(console_fmt)

# handle from all logs
root_logger.addHandler(stream)
root_logger.addHandler(file_handler)

logger = logging.getLogger('dog')

logger.info('Bot is starting...')

try:
    import uvloop

    # uvloop for speedups
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    logger.info('Using uvloop\'s event loop policy.')
except ModuleNotFoundError:
    pass


# gather additional options from the configuration file
additional_options = getattr(cfg, 'options', {})
additional_options.update({
    'owner_id': getattr(cfg, 'owner_id', None),
    'postgresql_auth': cfg['db']['postgres'],
    'redis_url': cfg['db']['redis']
})

logger.info('Bot options: %s', additional_options)

# create dogbot instance
selfbot_mode = '--selfbot' in ' '.join(sys.argv)
if selfbot_mode:
    logger.warning('Running in selfbot mode!')
    # change the database
    cfg['db']['postgres']['database'] = 'dog_selfbot'
    d = DogSelfbot(cfg=cfg, **additional_options)
    d.load_exts_recursively('dog/selfext', 'Initial selfbot recursive load')
else:
    d = DogBot(cfg=cfg, **additional_options)
    d.load_exts_recursively('dog/ext', 'Initial recursive load')
d.run(cfg['tokens']['selfbot'] if selfbot_mode else cfg['tokens']['bot'], bot=not selfbot_mode)

# close log handlers (why)
# https://github.com/Rapptz/RoboDanny/blob/master/bot.py#L128-L132
handlers = root_logger.handlers[:]
for hndlr in handlers:
    hndlr.close()
    root_logger.removeHandler(hndlr)

Fix selfbots