<commit_before># coding: utf-8

"""
Created on 2016-08-23
@author: naoey
"""

import logging
import logging.config  # find out why this is necessary when logging already imported
import discord
import time
import datetime
import os
import sys
import json
import importlib
import threading

import config
import errors
import utils

from models import Server, Channel

LOG_CONFIG = {
    "version": 1,
    "formatters": {
        "debug": {
            "format": "%(asctime)s : %(name)s : %(levelname)s : %(message)s"
        },
        "error": {
            "format": "%(levelname)s at %(asctime)s in %(funcName)s in %(filename) at line %(lineno)d: %(message)s"
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "debug",
            "level": logging.DEBUG
        },
        "file": {
            "class": "logging.FileHandler",
            "formatter": "error",
            "level": logging.ERROR,
            "filename": os.path.join(config.PATHS["logs_dir"], "slash_bot_{}.log".format(str(int(time.time()))))
        },
        # "discord": {
        #     "class": "DiscordLogHandler",
        #     "formatter": "discord_info",
        #     "level": logging.INFO
        # }
    },
    "root": {
        "handlers": ("console", "file"),
        "level": "DEBUG"
    }
}

logging.config.dictConfig(LOG_CONFIG)


class SlashBot(discord.Client):

    _module_handler_prefix = "cmd_"

    """
    Bot setup handlers
    """
    def __init__(self):
        super().__init__()

        with open(config.PATHS["discord_creds"], "r") as cf_d:
            config.GLOBAL["discord"] = json.load(cf_d)

        config.GLOBAL["bot"] = self

        # discord_logger = DiscordLogHandler()
        # discord_logger.setLevel(logging.INFO)
        # discord_logger.setFormatter(logging.Formatter("%(message)s"))
        # logging.addHandler(discord_logger)

    def run(self):
        super().run(config.GLOBAL["discord"]["token"])

    def log(self, msg):
        self.send_message(config.GLOBAL["discord"]["log_channel_id"])

    async def activate_modules(self):
        self.modules_map = {}

        self.modules_map["bot"] = {
            "module": CoreFunctions(),
            "subcommands": [x for x, y in CoreFunctions.__dict__.items() if (
                type(y) == type(lambda:0) and x.startswith("cmd_"))]
        }

        for name, module_details in config.MODULES.items():
            if module_details["active"]:
                try:
                    imported_module = importlib.import_module("modules.{}".format(module_details["location"]))
                    main_class = getattr(imported_module, module_details["class"])

                    self.modules_map[module_details["prefix"]] = {
                        "module": main_class(),
                        "subcommands": [x for x, y in main_class.__dict__.items() if (
                            type(y) == type(lambda:0) and x.startswith("cmd_"))],
                    }

                    logging.debug("Activated module '{}".format(name))
                    config.STATS.MODULES_ACTIVE += 1

                except ImportError as ie:
                    logging.exception("Couldn't import module '{}'".format(name))

                except Exception as e:
                    logging.debug("{}".format(e))
                    logging.exception("Unkown error activating module {}".format(name))

        logging.info("Registered {} active modules".format(config.STATS.MODULES_ACTIVE))
        logging.debug("Registered modules map is {}".format(self.modules_map))

    async def begin_status_loop(self):
        try:
            if not self._last_status_idx:
                self._last_status_idx = 0
        except AttributeError:
            self._last_status_idx = 0

        if len(config.DISCORD_STATUS_ITER) == 0:
            await self.change_status(game=discord.Game(name="~slash"))
            return

        await self.change_status(game=discord.Game(name=config.DISCORD_STATUS_ITER[self._last_status_idx]))
        threading.Timer(120, self.begin_status_loop).start()

        self._last_status_idx += 1

        if self._last_status_idx >= len(config.DISCORD_STATUS_ITER):
            self._last_status_idx = 0

    """
    Discord event listeners
    """
    async def on_ready(self):
        logging.info("Ready!")
        logging.info("Bot version {}".format(config.VERSION))

        for server in self.servers:
            await self.on_server_join(server)

            for channel in server.channels:
                if channel.type == discord.ChannelType.text:
                    config.STATS.TEXT_CHANNELS += 1
                elif channel.type == discord.ChannelType.voice:
                    config.STATS.VOICE_CHANNELS += 1

                self.on_channel_create(channel)

        config.STATS = Stats()
        config.STATS.SERVERS = len(self.servers)

        self.modules_map = {}

        logging.info("Activating modules")
        await self.activate_modules()

        # await self.begin_status_loop()

    async def on_message(self, message):
        if message.content.startswith(config.BOT_PREFIX):<commit_msg>Move Stats creation to happen before registering servers and channels.<commit_after># coding: utf-8

"""
Created on 2016-08-23
@author: naoey
"""

import logging
import logging.config  # find out why this is necessary when logging already imported
import discord
import time
import datetime
import os
import sys
import json
import importlib
import threading

import config
import errors
import utils

from models import Server, Channel

LOG_CONFIG = {
    "version": 1,
    "formatters": {
        "debug": {
            "format": "%(asctime)s : %(name)s : %(levelname)s : %(message)s"
        },
        "error": {
            "format": "%(levelname)s at %(asctime)s in %(funcName)s in %(filename) at line %(lineno)d: %(message)s"
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "debug",
            "level": logging.DEBUG
        },
        "file": {
            "class": "logging.FileHandler",
            "formatter": "error",
            "level": logging.ERROR,
            "filename": os.path.join(config.PATHS["logs_dir"], "slash_bot_{}.log".format(str(int(time.time()))))
        },
        # "discord": {
        #     "class": "DiscordLogHandler",
        #     "formatter": "discord_info",
        #     "level": logging.INFO
        # }
    },
    "root": {
        "handlers": ("console", "file"),
        "level": "DEBUG"
    }
}

logging.config.dictConfig(LOG_CONFIG)


class SlashBot(discord.Client):

    _module_handler_prefix = "cmd_"

    """
    Bot setup handlers
    """
    def __init__(self):
        super().__init__()

        with open(config.PATHS["discord_creds"], "r") as cf_d:
            config.GLOBAL["discord"] = json.load(cf_d)

        config.GLOBAL["bot"] = self

        # discord_logger = DiscordLogHandler()
        # discord_logger.setLevel(logging.INFO)
        # discord_logger.setFormatter(logging.Formatter("%(message)s"))
        # logging.addHandler(discord_logger)

    def run(self):
        super().run(config.GLOBAL["discord"]["token"])

    def log(self, msg):
        self.send_message(config.GLOBAL["discord"]["log_channel_id"])

    async def activate_modules(self):
        self.modules_map = {}

        self.modules_map["bot"] = {
            "module": CoreFunctions(),
            "subcommands": [x for x, y in CoreFunctions.__dict__.items() if (
                type(y) == type(lambda:0) and x.startswith("cmd_"))]
        }

        for name, module_details in config.MODULES.items():
            if module_details["active"]:
                try:
                    imported_module = importlib.import_module("modules.{}".format(module_details["location"]))
                    main_class = getattr(imported_module, module_details["class"])

                    self.modules_map[module_details["prefix"]] = {
                        "module": main_class(),
                        "subcommands": [x for x, y in main_class.__dict__.items() if (
                            type(y) == type(lambda:0) and x.startswith("cmd_"))],
                    }

                    logging.debug("Activated module '{}".format(name))
                    config.STATS.MODULES_ACTIVE += 1

                except ImportError as ie:
                    logging.exception("Couldn't import module '{}'".format(name))

                except Exception as e:
                    logging.debug("{}".format(e))
                    logging.exception("Unkown error activating module {}".format(name))

        logging.info("Registered {} active modules".format(config.STATS.MODULES_ACTIVE))
        logging.debug("Registered modules map is {}".format(self.modules_map))

    async def begin_status_loop(self):
        try:
            if not self._last_status_idx:
                self._last_status_idx = 0
        except AttributeError:
            self._last_status_idx = 0

        if len(config.DISCORD_STATUS_ITER) == 0:
            await self.change_status(game=discord.Game(name="~slash"))
            return

        await self.change_status(game=discord.Game(name=config.DISCORD_STATUS_ITER[self._last_status_idx]))
        threading.Timer(120, self.begin_status_loop).start()

        self._last_status_idx += 1

        if self._last_status_idx >= len(config.DISCORD_STATUS_ITER):
            self._last_status_idx = 0

    """
    Discord event listeners
    """
    async def on_ready(self):
        logging.info("Ready!")
        logging.info("Bot version {}".format(config.VERSION))

        config.STATS = Stats()
        config.STATS.SERVERS = len(self.servers)

        for server in self.servers:
            await self.on_server_join(server)

            for channel in server.channels:
                if channel.type == discord.ChannelType.text:
                    config.STATS.TEXT_CHANNELS += 1
                elif channel.type == discord.ChannelType.voice:
                    config.STATS.VOICE_CHANNELS += 1

                self.on_channel_create(channel)

        self.modules_map = {}

        logging.info("Activating modules")
        await self.activate_modules()

        # await self.begin_status_loop()

    async def on_message(self, message):
        if message.content.startswith(config.BOT_PREFIX):
{'fraction_non_alphanumeric': 0.12862197571005068, 'fraction_numerical': 0.004398967199005451, 'mean_word_length': 2.1662125340599454, 'pattern_counts': {'":': 54, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1953578', 'n_tokens_mistral': 3102, 'n_tokens_neox': 2977, 'n_words': 590}