<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');

var NorrisBot = function Constructor(settings) {
	this.settings = settings;
	this.settings.name = this.settings.name || 'norrisbot';
	this.dbPath = settings.dbPath || path.resolve(process.cwd(), 'data', 'norrisbot.db');

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

util.inherits(NorrisBot, Bot);

module.exports = NorrisBot;<commit_msg>added all bot class functions<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 NorrisBot = function Constructor(settings) {
	this.settings = settings;
	this.settings.name = this.settings.name || 'norrisbot';
	this.dbPath = settings.dbPath || path.resolve(process.cwd(), 'data', 'norrisbot.db');

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

util.inherits(NorrisBot, Bot);

NorrisBot.prototype.run = function() {
	NorrisBot.super_.call(this, this.settings);

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

NorrisBot.prototype._onStart = function() {
	this._loadBotUser();
	this._connectDb();
	this._firstRunCheck();
};

NorrisBot.prototype._loadBotUser = function() {
	var self = this;
	this.user = this.users.filter(function(user) {
		return user.name === self.name;
	})[0];
}

NorrisBot.prototype._connectDb = function() {
	if(!fs.existsSync(this.dbPath)) {
		console.error('Database path ' + '''' + this.dbPath + ''' does not exist or it\'s not readable.');
		process.exit(1);
	}

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

NorrisBot.prototype._firstRunCheck = function() {
	var self = this;
	self.db.get('SELECT val FROM info WHERE name = "lastrun" LIMIT 1', function(err, record) {
		if(err) {
			return console.error('DATABASE ERROR:', err);
		}

		var currentTime = (new Date()).toJSON();

		if(!record) {
			self._welcomeMessage();
			return self.db.run('INSERT INTO info(name, val) VALUES("lastrun", ?)', currentTime);
		}

		self.db.run('UPDATE info SET val = ? WHERE name = "lastrun"', currentTime);
	});
};

NorrisBot.prototype._welcomeMessage = function() {
	this.postMessageToChannel(this.channels[0].name, 'Hi guys, roundhouse-kick anyone?' +
		'\n I can tell jokes, but very honest ones. Just say `<NAME>` or `' + this.name + '` to invoke me!', {as_user: true});
};

NorrisBot.prototype._onMessage = function(message) {
	if (this._isChatmessage(message) &&
	    this._isChannelConversation(message) &&
	    !this._isFromNorrisBot(message) &&
	    this._isMentioningChuckNorris(message)
	) {
		this._replyWithRandomJoke(message);
	}
};

NorrisBot.prototype._isChatmessage = function(message) {
	return message.type === 'message' && Boolean(message.text);
};

NorrisBot.prototype._isChannelConversation = function(message) {
	return typeof message.channel === 'string' &&
		message.channel[0] === 'C';
};

NorrisBot.prototype._isFromNorrisBot = function(message) {
	return message.user === this.user.id;
};

NorrisBot.prototype._isMentioningChuckNorris = function(message) {
	return message.text.toLowerCase().indexOf('chuck norris') > -1 ||
		message.text.toLowerCase().indexOf(this.name) > -1;
};

NorrisBot.prototype._replyWithRandomJoke = 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);
	});
};

NorrisBot.prototype._getChannelById = function(channelId) {
	return this.channels.filter(function(item) {
		return item.id === channelId;
	})[0];
};

module.exports = NorrisBot;
{'fraction_non_alphanumeric': 0.16687737041719342, 'fraction_numerical': 0.003034134007585335, 'mean_word_length': 4.93993993993994, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '6152720', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1318, 'n_words': 273}