<commit_before>import fs from "fs";
import {CommandClient} from "eris";
import request from "request";
import mongoose from "mongoose";
import querystring from "querystring";

import SetTokenCommand from "./Command/SetTokenCommand";
import InfoCommand from "./Command/InfoCommand";
import IgnoreCommand from "./Command/IgnoreCommand";
import UnignoreCommand from "./Command/UnignoreCommand";
import ChannelLeaderboardCommand from "./Command/ChannelLeaderboardCommand";
import LeaderboardCommand from "./Command/LeaderboardCommand";
import StatsCommand from "./Command/StatsCommand";
import CleanCommand from "./Command/CleanCommand";
import RestartCommand from "./Command/RestartCommand";
import EvalCommand from "./Command/EvalCommand";

import ReadyHandler from "./Handler/ReadyHandler";
import VoiceEvent from "./Model/VoiceEvent";
import Aggregator from "./Aggregator";

const AGGREGATOR_INTERVAL = 1 * 60 * 1000;

export default class Kernel {
    constructor() {
        try {
            fs.writeFileSync(__dirname + '/../config.json', '{}', {flag: 'wx'});
        } catch (e) {
        }
        try {
            this.config = JSON.parse(fs.readFileSync(__dirname + '/../config.json'));
        } catch (e) {
            this.config = {};
        }
        
        mongoose.connect(process.env.MONGO_URL);
        var db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error:'));
        db.once('open', function () {
            console.log("DB Connected");
        });
        
        this.client = new CommandClient(process.env.BOT_TOKEN, {
            messageLimit: 5,
            getAllUsers:  true,
        }, {
            owner:       "Aaron",
            description: "Google Analytics for your Discord Server!",
            prefix:      process.env.NODE_ENV === 'development' ? '\\' : '/'
        });
        
        let cmds = [
            SetTokenCommand,
            EvalCommand,
            RestartCommand,
            StatsCommand,
            InfoCommand,
            UnignoreCommand,
            IgnoreCommand,
            ChannelLeaderboardCommand,
            LeaderboardCommand,
            CleanCommand
        ];
        for (let cmd of cmds) {
            this.client.registerCommand(cmd.name, this.wrapError.bind(this, cmd.run.bind(this)), cmd.config);
        }
        
        ReadyHandler.run.call(this);
        this.client.stats = {
            disconnects: 0,
            connects: 0,
            resumes: 0,
            raw: [],
            rawTotal: {}
        };
        
        
        this.client.on('err', console.error);
        this.client.on('error', console.error);
        this.client.on('disconnect', () => this.client.stats.disconnects++);
        this.client.on('connect', () => this.client.stats.connects++);
        this.client.on('resume', () => this.client.stats.resumes++);
        this.client.on('rawWS', (packet, id) => {
            const type = packet.t || 'MISC';
            if (!this.client.stats.raw[id]) {
                this.client.stats.raw[id] = {};
            }
            if (!this.client.stats.raw[id][type]) {
                this.client.stats.raw[id][type] = 0;
            }
            if (!this.client.stats.rawTotal[type]) {
                this.client.stats.rawTotal[type] = 0;
            }
            
            this.client.stats.rawTotal[type]++;
        });
        
        process.on('SIGUSR2', () => {
            console.log("SIGUSR2");
            this.gracefulShutdown();
        });
        
        setInterval(Aggregator.aggregate.bind(null, this), AGGREGATOR_INTERVAL);
        this.client.on('ready', Aggregator.aggregate.bind(null, this));
    }
    
    track(action, server, user, extra = {}) {
        if (!action) {
            console.log("No action!");
            return;
        }
        
        if (server && user) {
            let tokens = [process.env.BOT_ANALYTICS];
            if (this.getConfig(server).token) {
                tokens.push(this.getConfig(server).token);
            }<commit_msg>Aggregating on ready<commit_after>import fs from "fs";
import {CommandClient} from "eris";
import request from "request";
import mongoose from "mongoose";
import querystring from "querystring";

import SetTokenCommand from "./Command/SetTokenCommand";
import InfoCommand from "./Command/InfoCommand";
import IgnoreCommand from "./Command/IgnoreCommand";
import UnignoreCommand from "./Command/UnignoreCommand";
import ChannelLeaderboardCommand from "./Command/ChannelLeaderboardCommand";
import LeaderboardCommand from "./Command/LeaderboardCommand";
import StatsCommand from "./Command/StatsCommand";
import CleanCommand from "./Command/CleanCommand";
import RestartCommand from "./Command/RestartCommand";
import EvalCommand from "./Command/EvalCommand";

import ReadyHandler from "./Handler/ReadyHandler";
import VoiceEvent from "./Model/VoiceEvent";
import Aggregator from "./Aggregator";

const AGGREGATOR_INTERVAL = 1 * 60 * 1000;

export default class Kernel {
    constructor() {
        try {
            fs.writeFileSync(__dirname + '/../config.json', '{}', {flag: 'wx'});
        } catch (e) {
        }
        try {
            this.config = JSON.parse(fs.readFileSync(__dirname + '/../config.json'));
        } catch (e) {
            this.config = {};
        }
        
        mongoose.connect(process.env.MONGO_URL);
        var db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error:'));
        db.once('open', function () {
            console.log("DB Connected");
        });
        
        this.client = new CommandClient(process.env.BOT_TOKEN, {
            messageLimit: 5,
            getAllUsers:  true,
        }, {
            owner:       "Aaron",
            description: "Google Analytics for your Discord Server!",
            prefix:      process.env.NODE_ENV === 'development' ? '\\' : '/'
        });
        
        let cmds = [
            SetTokenCommand,
            EvalCommand,
            RestartCommand,
            StatsCommand,
            InfoCommand,
            UnignoreCommand,
            IgnoreCommand,
            ChannelLeaderboardCommand,
            LeaderboardCommand,
            CleanCommand
        ];
        for (let cmd of cmds) {
            this.client.registerCommand(cmd.name, this.wrapError.bind(this, cmd.run.bind(this)), cmd.config);
        }
        
        ReadyHandler.run.call(this);
        this.client.stats = {
            disconnects: 0,
            connects: 0,
            resumes: 0,
            raw: [],
            rawTotal: {}
        };
        
        
        this.client.on('err', console.error);
        this.client.on('error', console.error);
        this.client.on('disconnect', () => this.client.stats.disconnects++);
        this.client.on('connect', () => this.client.stats.connects++);
        this.client.on('resume', () => this.client.stats.resumes++);
        this.client.on('rawWS', (packet, id) => {
            const type = packet.t || 'MISC';
            if (!this.client.stats.raw[id]) {
                this.client.stats.raw[id] = {};
            }
            if (!this.client.stats.raw[id][type]) {
                this.client.stats.raw[id][type] = 0;
            }
            if (!this.client.stats.rawTotal[type]) {
                this.client.stats.rawTotal[type] = 0;
            }
            
            this.client.stats.rawTotal[type]++;
        });
        
        process.on('SIGUSR2', () => {
            console.log("SIGUSR2");
            this.gracefulShutdown();
        });
        
        this.client.on('ready', () => {
            Aggregator.aggregate(this);
            setInterval(Aggregator.aggregate.bind(null, this), AGGREGATOR_INTERVAL);
    
        });
    }
    
    track(action, server, user, extra = {}) {
        if (!action) {
            console.log("No action!");
            return;
        }
        
        if (server && user) {
            let tokens = [process.env.BOT_ANALYTICS];
            if (this.getConfig(server).token) {
                tokens.push(this.getConfig(server).token);
            }
{'fraction_non_alphanumeric': 0.13692081392498162, 'fraction_numerical': 0.003677371904878647, 'mean_word_length': 1.9056267806267806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '4460325', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2253, 'n_words': 429}