<commit_before>path = require('path');
var fs = require('fs');

var config = require(path.resolve(__dirname, 'config.json'));
var models = require(path.resolve(__dirname, 'models'));
var DubAPI = require('dubapi');

var roomHasActiveMods = false;
var skipTimer;
var botUser = {};

new DubAPI(config.auth, function (err, bot) {

    initializeModules(config.auth, bot);

    bot.connect(config.roomName);

    bot.on('connected', function (data) {
        console.log('Connected: ', data);

        console.log('[EVENT] Ready - joined room: ' + config.roomName);
        if (config.verboseLogging) {
            console.log('[INIT] Room data: ' + JSON.stringify(data, null, 2));
        }

        botUser = bot.getSelf();
        getDbUserFromSiteUser(botUser, function (row) {
            botUser.db = row;
        });

        console.log('[INIT] Data loaded for ' + botUser.username + '\n ' + JSON.stringify(botUser, null, 2));

        if (config.responses.botConnect !== "") {
            bot.sendChat(config.responses.botConnect);
        }

        bot.getUsers().forEach(function (user) {
            updateDbUser(user);
        });

        if (config.upvoteSongs == 'ALL') {
            bot.updub();
        }

    });

    bot.on('chat-message', function (data) {

        if (config.verboseLogging) {
            console.log('[CHAT]', JSON.stringify(data, null, 2));
        }
        else if (data.user.username !== undefined && data.user.username !== null) {
            console.log('[CHAT]', data.user.username + ': ' + data.message);
        }

        if (data.user.username !== undefined && data.user.username !== null) {
            data.message = data.message.trim();
            handleCommand(data);
            models.User.update({
                last_active: new Date(),
                last_seen: new Date(),
                locale: data.raw.user.userInfo.locale
            }, {where: {site_id: data.user.id}});
        }
    });

    bot.on('room_playlist-update', function (data) {

        if (config.verboseLogging) {
            console.log('[EVENT] room_playlist-update ', JSON.stringify(data, null, 2));
        } else {
            console.log('[EVENT] room_playlist-update');
        }

        //saveWaitList(true);

        // Writes current room state to outfile so it can be used for the web
        if (config.roomStateFile) {

            var JSONstats = {}

            JSONstats.media = bot.getMedia();
            JSONstats.dj = bot.getDJ();
            JSONstats.queue = bot.getQueue();
            JSONstats.users = bot.getUsers();
            JSONstats.staff = bot.getStaff();

            fs.writeFile(
                config.roomStateFile,
                JSON.stringify(JSONstats, null, 2),
                function (err) {
                    if (err) {
                        console.log(err);
                        return console.log(err);
                    }
                }
            );

        }

        // Write previous play data to DB<commit_msg>Fix bug in room queue<commit_after>path = require('path');
var fs = require('fs');

var config = require(path.resolve(__dirname, 'config.json'));
var models = require(path.resolve(__dirname, 'models'));
var DubAPI = require('dubapi');

var roomHasActiveMods = false;
var skipTimer;
var botUser = {};

new DubAPI(config.auth, function (err, bot) {

    initializeModules(config.auth, bot);

    bot.connect(config.roomName);

    bot.on('connected', function (data) {
        console.log('Connected: ', data);

        console.log('[EVENT] Ready - joined room: ' + config.roomName);
        if (config.verboseLogging) {
            console.log('[INIT] Room data: ' + JSON.stringify(data, null, 2));
        }

        botUser = bot.getSelf();
        getDbUserFromSiteUser(botUser, function (row) {
            botUser.db = row;
        });

        console.log('[INIT] Data loaded for ' + botUser.username + '\n ' + JSON.stringify(botUser, null, 2));

        if (config.responses.botConnect !== "") {
            bot.sendChat(config.responses.botConnect);
        }

        bot.getUsers().forEach(function (user) {
            updateDbUser(user);
        });

        if (config.upvoteSongs == 'ALL') {
            bot.updub();
        }

    });

    bot.on('chat-message', function (data) {

        if (config.verboseLogging) {
            console.log('[CHAT]', JSON.stringify(data, null, 2));
        }
        else if (data.user.username !== undefined && data.user.username !== null) {
            console.log('[CHAT]', data.user.username + ': ' + data.message);
        }

        if (data.user.username !== undefined && data.user.username !== null) {
            data.message = data.message.trim();
            handleCommand(data);
            models.User.update({
                last_active: new Date(),
                last_seen: new Date(),
                locale: data.raw.user.userInfo.locale
            }, {where: {site_id: data.user.id}});
        }
    });

    bot.on('room_playlist-update', function (data) {

        if (config.verboseLogging) {
            console.log('[EVENT] room_playlist-update ', JSON.stringify(data, null, 2));
        } else {
            console.log('[EVENT] room_playlist-update');
        }

        //saveWaitList(true);

        // Writes current room state to outfile so it can be used for the web
        if (config.roomStateFile) {

            var JSONstats = {}

            JSONstats.media = bot.getMedia();
            JSONstats.dj = bot.getDJ();
            JSONstats.roomQueue = bot.getQueue();
            JSONstats.users = bot.getUsers();
            JSONstats.staff = bot.getStaff();

            fs.writeFile(
                config.roomStateFile,
                JSON.stringify(JSONstats, null, 2),
                function (err) {
                    if (err) {
                        console.log(err);
                        return console.log(err);
                    }
                }
            );

        }

        // Write previous play data to DB
{'fraction_non_alphanumeric': 0.14262078093977498, 'fraction_numerical': 0.0016545334215751159, 'mean_word_length': 1.8234469873890706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6006411', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1681, 'n_words': 349}