route.js
2.83 KB
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
const MongoClient = require('mongodb').MongoClient;
const dboperation = require('../operations');
const dbname = 'jokeapi';
const collec = 'jokes';
const url = 'mongodb://localhost:27017/';
exports.randomJoke= ()=>{
MongoClient.connect('mongodb://localhost:27017', function (err, client){
if (err) throw err;
var db = client.db('jokeapi');
json_max = 376;
function getRandomInt() {
min = Math.ceil(1);
max = Math.floor(376);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
random = getRandomInt();
result = db.collection('jokes').findOne({id: random});
user = result;
user.then(function(total){
question = total.setup;
joke = total.punchline;
const face = {
icon_emoji: ':laughing:'
};
bot.postMessageToChannel('everyone', question, joke, face);
bot.postMessageToChannel('full-stack-web', question, joke, face);
bot.postMessageToChannel('bot_test', question, joke, face);
})
client.close();
})
}
function generalJoke(){
dboperation.getdata(dbname, collec, "general")
.then((data) =>{
var query = { state: 'OK' };
var n = data.count(query);
var r = Math.floor(Math.random() * n);
var randomElement = data.find(query).limit(1).skip(r);
var question = randomElement.setup;
var joke = randomElement.punchline;
const face = {
icon_emoji: ':laughing:'
};
bot.postMessageToChannel('everyone', question, joke, face);
bot.postMessageToChannel('full-stack-web', question, joke, face);
bot.postMessageToChannel('bot_test', question, joke, face);
});
};
// Tell a yomama Joke
function yomamaJoke(){
axios.get('http://api.yomomma.info/')
.then(res =>{
const joke = res.data.joke;
const face = {
icon_emoji: ':laughing:'
};
bot.postMessageToChannel('everyone', `Yo mama: ${joke}`,face);
bot.postMessageToChannel('full-stack-web', `Yo mama: ${joke}`,face);
bot.postMessageToChannel('bot_test', `Yo mama: ${joke}`,face);
});
};
//Tell random joke
function runhelp(){
const face = {
icon_emoji: ':question:'
};
bot.postMessageToChannel('everyone', "Type @joker and write a joke that you would like\n ex- @joker random",face);
bot.postMessageToChannel('full-stack-web', "Type @joker and write a joke that you would like\n ex- @joker random",face);
};