<commit_before>const fs = require('fs');
const EventEmitter = require('events').EventEmitter;
const request = require('superagent');
const WebSocket = require('ws');

const User = require('./user');
const Guild = require('./guild');
const Collection = require('./collection');
const EndPoints = require('./api').EndPoints;

module.exports =
class Client extends EventEmitter {
    constructor() {
        super();
        this.token = '';
        this.gateway = '';
        this.websocket = null;
        this.heartbeat =  null;

        // TEMP
        this.guilds = new Collection();
    }

    login(token) {
        this.token = token;

        // Don't request new gateway url when we have one already
        if (this.gateway)
            return;

        this.requestGateWay().then(url => {
            this.gateway = url;
            this.createWebSocket(url);
        });
    }

    setClientGame(name) {
        this.websocket.send(JSON.stringify({op: 3, d: {idle_since: null, game: {name: name}}}));
    }

    requestGateWay() {
        let req = request('GET', 'https://discordapp.com/api/gateway');
        req.set('authorization', this.token);
        return req.then(res => {
            return res.body.url;
        });
    }

    createWebSocket(url) {
        this.websocket = new WebSocket(url);
        this.websocket.on('open', () => {
            // Send Gateway Identify message (op = 2)
            let data = {
                "op": 2,
                "d": {
                    "token": this.token,
                    "v": 3,
                    "compress": true,
                    "large_threshold": 250,
                    "properties": {
                        "$os": "linux",
                        "$browser": "",
                        "$device": "discord.js",
                        "$referrer": "",
                        "$referring_domain": ""
                    }
                }
            };
            this.websocket.send(JSON.stringify(data));
        });

        this.websocket.on('error', (error) => {
            console.log(`WebSocket error: ${error}`);
        });

        this.websocket.on('close', (code, message) => {
            console.log(`WebSocket close: (${code}) ${message}`);
        });

        this.websocket.on('message', (packet, flags) => {
            if (packet instanceof Buffer) {
                const zlib = require('zlib');
                packet = zlib.inflateSync(packet).toString();
            }
            const msg = JSON.parse(packet);
            const msg_data = msg.d;


            if (msg.t === 'READY') {
                this.heartbeat = setInterval(() => {
                    this.websocket.send(JSON.stringify({op: 1, d: 0}));
                }, msg_data.heartbeat_interval);

                this.emit('ready');
                fs.appendFileSync(`${msg.t}.log`, JSON.stringify(msg, null, 2));
                fs.appendFileSync(`${msg.t}.log`, "\n-------------------------------\n");
            }
            else if (msg.t === 'GUILD_CREATE') {
                let server = new Guild(msg_data);
                this.guilds.add(server);
                this.emit('server-created', server);
                let test_str = "Members online: ";
                this.guilds.get("id", msg_data.id).members.all("online", true).forEach(member => {
                    console.log(member.status);
                    test_str += member.username + ", ";
                });
                console.log(test_str.substring(0, test_str.length - 2));
                if (msg_data.name === "Ho-BoBot Testing Grounds") {
                    let req = request('POST', EndPoints.CHANNEL_MESSAGE(msg_data.id));
                    req.set('User-Agent', {url:"https://github.com/stenverbois/ho-bobot", version: 1})
                       .send({'content': test_str.substring(0, test_str.length - 2)})
                       .set('authorization', this.token)
                       .then(res => {
                        // console.log(res)
                    });
                }

            }
            else if (msg.t === 'PRESENCE_UPDATE') {
                // Log messages
                fs.appendFileSync(`${msg.t}.log`, JSON.stringify(msg, null, 2));
                fs.appendFileSync(`${msg.t}.log`, "\n-------------------------------\n");
            }
            else {
                // Log messages
                fs.appendFileSync(`${msg.t}.log`, JSON.stringify(msg, null, 2));
                fs.appendFileSync(`${msg.t}.log`, "\n-------------------------------\n");
                console.log("Unknown t: " + JSON.stringify(msg.t));
            }
        });
    }
};<commit_msg>Always log messages for now<commit_after>const fs = require('fs');
const EventEmitter = require('events').EventEmitter;
const request = require('superagent');
const WebSocket = require('ws');

const User = require('./user');
const Guild = require('./guild');
const Collection = require('./collection');
const EndPoints = require('./api').EndPoints;

module.exports =
class Client extends EventEmitter {
    constructor() {
        super();
        this.token = '';
        this.gateway = '';
        this.websocket = null;
        this.heartbeat =  null;

        // TEMP
        this.guilds = new Collection();
    }

    login(token) {
        this.token = token;

        // Don't request new gateway url when we have one already
        if (this.gateway)
            return;

        this.requestGateWay().then(url => {
            this.gateway = url;
            this.createWebSocket(url);
        });
    }

    setClientGame(name) {
        this.websocket.send(JSON.stringify({op: 3, d: {idle_since: null, game: {name: name}}}));
    }

    requestGateWay() {
        let req = request('GET', 'https://discordapp.com/api/gateway');
        req.set('authorization', this.token);
        return req.then(res => {
            return res.body.url;
        });
    }

    createWebSocket(url) {
        this.websocket = new WebSocket(url);
        this.websocket.on('open', () => {
            // Send Gateway Identify message (op = 2)
            let data = {
                "op": 2,
                "d": {
                    "token": this.token,
                    "v": 3,
                    "compress": true,
                    "large_threshold": 250,
                    "properties": {
                        "$os": "linux",
                        "$browser": "",
                        "$device": "discord.js",
                        "$referrer": "",
                        "$referring_domain": ""
                    }
                }
            };
            this.websocket.send(JSON.stringify(data));
        });

        this.websocket.on('error', (error) => {
            console.log(`WebSocket error: ${error}`);
        });

        this.websocket.on('close', (code, message) => {
            console.log(`WebSocket close: (${code}) ${message}`);
        });

        this.websocket.on('message', (packet, flags) => {
            if (packet instanceof Buffer) {
                const zlib = require('zlib');
                packet = zlib.inflateSync(packet).toString();
            }
            const msg = JSON.parse(packet);
            const msg_data = msg.d;

            // Log messages
            fs.appendFileSync(`${msg.t}.log`, JSON.stringify(msg, null, 2));
            fs.appendFileSync(`${msg.t}.log`, "\n-------------------------------\n");

            if (msg.t === 'READY') {
                this.heartbeat = setInterval(() => {
                    this.websocket.send(JSON.stringify({op: 1, d: 0}));
                }, msg_data.heartbeat_interval);

                this.emit('ready');
            }
            else if (msg.t === 'GUILD_CREATE') {
                let server = new Guild(msg_data);
                this.guilds.add(server);
                this.emit('server-created', server);
                let test_str = "Members online: ";
                this.guilds.get("id", msg_data.id).members.all("online", true).forEach(member => {
                    console.log(member.status);
                    test_str += member.username + ", ";
                });
                console.log(test_str.substring(0, test_str.length - 2));
                if (msg_data.name === "Ho-BoBot Testing Grounds") {
                    let req = request('POST', EndPoints.CHANNEL_MESSAGE(msg_data.id));
                    req.set('User-Agent', {url:"https://github.com/stenverbois/ho-bobot", version: 1})
                       .send({'content': test_str.substring(0, test_str.length - 2)})
                       .set('authorization', this.token)
                       .then(res => {
                        // console.log(res)
                    });
                }

            }
            else if (msg.t === 'PRESENCE_UPDATE') {
            }
            else {
                console.log("Unknown t: " + JSON.stringify(msg.t));
            }
        });
    }
};
{'fraction_non_alphanumeric': 0.16529660075538768, 'fraction_numerical': 0.003554765607642746, 'mean_word_length': 1.3931419457735248, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 4, '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': '2586346', 'n_tokens_mistral': 2541, 'n_tokens_neox': 2379, 'n_words': 450}