<commit_before>'use strict';

var util = require('util');
var path = require('path');
var fs = require('fs');
var SQLite = require('sqlite3').verbose();
var Bot = require('slackbots');

/**
 * Constructor function. It accepts a settings object which should contain the following keys:
 *      token : the API token of the bot (mandatory)
 *      name : the name of the bot (will default to "trainbot")
 *      dbPath : the path to access the database (will default to "data/norrisbot.db")
 *
 * @param {object} settings
 * @constructor
 *
 * @author <NAME> <<EMAIL>>
 */
var TrainBot = function Constructor(settings) {
    this.settings = settings;
    this.settings.name = this.settings.name || 'trainbot';
    this.dbPath = settings.dbPath || path.resolve(__dirname, '..', 'data', 'norrisbot.db');

    this.user = null;
    this.db = null;
};

// inherits methods and properties from the Bot constructor
util.inherits(TrainBot, Bot);

/**
 * Run the bot
 * @public
 */
TrainBot.prototype.run = function () {
    TrainBot.super_.call(this, this.settings);

    this.on('start', this._onStart);
    this.on('message', this._onMessage);
};

/**
 * On Start callback, called when the bot connects to the Slack server and access the channel
 * @private
 */
TrainBot.prototype._onStart = function () {
    this._loadBotUser();
    this._connectDb();
    this._firstRunCheck();
};

/**
 * On message callback, called when a message (of any type) is detected with the real time messaging API
 * @param {object} message
 * @private
 */
TrainBot.prototype._onMessage = function (message) {
    if (this._isChatMessage(message) &&
        this._isChannelConversation(message) &&
        !this._isFromTrainBot(message) &&
        this._isMentioningTrainBot(message)
    ) {
        this._reply(message);
    }
};

/**
 * Replyes to a message with a random Joke
 * @param {object} originalMessage
 * @private
 */
TrainBot.prototype._reply = function (originalMessage) {
    var self = this;
    self.db.get('SELECT id, joke FROM jokes ORDER BY used ASC, RANDOM() LIMIT 1', function (err, record) {
        if (err) {
            return console.error('DATABASE ERROR:', err);
        }

        var channel = self._getChannelById(originalMessage.channel);
        self.postMessageToChannel(channel.name, record.joke, {as_user: true});
        self.db.run('UPDATE jokes SET used = used + 1 WHERE id = ?', record.id);
    });
};

/**
 * Loads the user object representing the bot
 * @private
 */
TrainBot.prototype._loadBotUser = function () {
    var self = this;
    this.user = this.users.filter(function (user) {
        return user.name === self.name;
    })[0];
};

/**
 * Open connection to the db
 * @private
 */
TrainBot.prototype._connectDb = function () {
    if (!fs.existsSync(this.dbPath)) {
        console.error('Database path ' + '"' + this.dbPath + '" does not exists or it\'s not readable.');
        process.exit(1);
    }

    this.db = new SQLite.Database(this.dbPath);
};
<commit_msg>basic functionality???! kinda?<commit_after>'use strict';

var util = require('util');
var path = require('path');
var fs = require('fs');
var SQLite = require('sqlite3').verbose();
var Bot = require('slackbots');
var moment = require('moment');

/**
 * Constructor function. It accepts a settings object which should contain the following keys:
 *      token : the API token of the bot (mandatory)
 *      name : the name of the bot (will default to "trainbot")
 *      dbPath : the path to access the database (will default to "data/norrisbot.db")
 *
 * @param {object} settings
 * @constructor
 *
 * @author <NAME> <<EMAIL>>
 */
var TrainBot = function Constructor(settings) {
    this.settings = settings;
    this.settings.name = this.settings.name || 'trainbot';
    this.dbPath = settings.dbPath || path.resolve(__dirname, '..', 'data', 'norrisbot.db');

    this.user = null;
    this.db = null;
};

// inherits methods and properties from the Bot constructor
util.inherits(TrainBot, Bot);

/**
 * Run the bot
 * @public
 */
TrainBot.prototype.run = function () {
    TrainBot.super_.call(this, this.settings);

    this.on('start', this._onStart);
    this.on('message', this._onMessage);
};

/**
 * On Start callback, called when the bot connects to the Slack server and access the channel
 * @private
 */
TrainBot.prototype._onStart = function () {
    this._loadBotUser();
    this._connectDb();
    this._firstRunCheck();
};

/**
 * On message callback, called when a message (of any type) is detected with the real time messaging API
 * @param {object} message
 * @private
 */
TrainBot.prototype._onMessage = function (message) {
    if (this._isChatMessage(message) &&
        this._isChannelConversation(message) &&
        !this._isFromTrainBot(message) &&
        this._isMentioningTrainBot(message)
    ) {
        this._reply(message);
    }
};

/**
 * Replyes to a message with a random Joke
 * @param {object} originalMessage
 * @private
 */



TrainBot.prototype._reply = function (input) {
    var self = this;

    var data = 
    [
        {from: '23:53'},    
        {from: '21:17'},
        {from: '19:00'},
        {from: '17:40'},
        {from: '17:04'},
        {from: '15:34'},
        {from: '14:32'},
        {from: '11:05'},
        {from: '9:42'},
        {from: '7:42'},
        {from: '7:18'},
        {from: '6:39'},
        {from: '6:00'},
        {from: '5:13'}
    ];

    var input_split = input.text.split(",");
    var origin = input_split[1];
    var dest = input_split[2];
    var time = input_split[3];

    var now = moment();
    var data_unix = data;
    var nearest = ''

    var minutesOfDay = function(m){
      return m.minutes() + m.hours() * 60;
    }

    for(var i=0; data.length > i; i++) {
        var compare_time = moment(data_unix[i].from, 'HH:mm');

        if(now.isAfter(compare_time)) {
            break;
        }
        nearest = data[i].from
    }


    var channel = self._getChannelById(input.channel);
    self.postMessageToChannel(channel.name, nearest, {as_user: true});

    // self.db.get('SELECT id, joke FROM jokes ORDER BY used ASC, RANDOM() LIMIT 1', function (err, record) {
    //     if (err) {
    //         return console.error('DATABASE ERROR:', err);
    //     }

    //     var channel = self._getChannelById(input.channel);
    //     self.postMessageToChannel(channel.name, record.joke, {as_user: true});
    //     self.db.run('UPDATE jokes SET used = used + 1 WHERE id = ?', record.id);
    // });
};

/**
 * Loads the user object representing the bot
 * @private
 */
TrainBot.prototype._loadBotUser = function () {
    var self = this;
    this.user = this.users.filter(function (user) {
        return user.name === self.name;
    })[0];
};

/**
 * Open connection to the db
 * @private
 */
TrainBot.prototype._connectDb = function () {
    if (!fs.existsSync(this.dbPath)) {
        console.error('Database path ' + '"' + this.dbPath + '" does not exists or it\'s not readable.');
        process.exit(1);
    }

    this.db = new SQLite.Database(this.dbPath);
};

{'fraction_non_alphanumeric': 0.15181985554454044, 'fraction_numerical': 0.009347117971958646, 'mean_word_length': 2.9059734513274336, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '1724964', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2178, 'n_words': 598}