-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (113 loc) · 4.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
var util = require('util');
var path = require('path');
var fs = require('fs');
var SQLite = require('sqlite3').verbose();
var Bot = require('slackbots');
var BoxLunch = 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;
};
// inherits methods and properties from the Bot constructor
util.inherits(BoxLunch, Bot);
BoxLunch.prototype.run = function () {
BoxLunch.super_.call(this, this.settings);
this.on('start', this._onStart);
this.on('message', this._onMessage);
};
BoxLunch.prototype._onStart = function () {
this._loadBotUser();
this._connectDb();
this._firstRunCheck();
};
// 3 FUNCTIONS TO EXECUTE ON START
BoxLunch.prototype._loadBotUser = function () {
var self = this;
this.user = this.users.filter(function (user) {
return user.name === self.name;
})[0];
};
BoxLunch.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);
};
BoxLunch.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();
// this is a first run
if (!record) {
self._welcomeMessage();
return self.db.run('INSERT INTO info(name, val) VALUES("lastrun", ?)', currentTime);
}
// updates with new last running time
self.db.run('UPDATE info SET val = ? WHERE name = "lastrun"', currentTime);
});
};
BoxLunch.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 `Chuck Norris` or `' + this.name + '` to invoke me!',
{as_user: true});
};
// ON MESSAGE FUNCTIONS
BoxLunch.prototype._onMessage = function (message) {
if (this._isChatMessage(message) &&
this._isChannelConversation(message) &&
!this._isFromBoxLunch(message) &&
this._isMentioningChuckNorris(message)
) {
this._replyWithRandomJoke(message);
}
};
BoxLunch.prototype._isChatMessage = function (message) {
return message.type === 'message' && Boolean(message.text);
};
BoxLunch.prototype._isChannelConversation = function (message) {
return typeof message.channel === 'string' &&
( message.channel[0] === 'G' ||
message.channel[0] === 'C' );
};
BoxLunch.prototype._isFromBoxLunch = function (message) {
return message.user === this.user.id;
};
BoxLunch.prototype._isMentioningChuckNorris = function (message) {
return message.text.toLowerCase().indexOf('hungry') > -1 ||
message.text.toLowerCase().indexOf(this.name) > -1;
};
BoxLunch.prototype._replyWithRandomJoke = function (message) {
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);
}
if (message.channel[0] === 'C') {
var channel = self._getChannelById(message.channel);
self.postMessageToChannel(channel.name, record.joke, {as_user: true});
}
if (message.channel[0] === 'G') {
var group = self._getGroupById(message.channel);
self.postMessageToGroup(group.name, record.joke, {as_user: true});
}
self.db.run('UPDATE jokes SET used = used + 1 WHERE id = ?', record.id);
});
};
BoxLunch.prototype._getChannelById = function (channelId) {
return this.channels.filter(function (item) {
return item.id === channelId;
})[0];
};
BoxLunch.prototype._getGroupById = function (groupId) {
return this.groups.filter(function (item) {
return item.id === groupId;
})[0];
};
module.exports = BoxLunch;