Showing
2 changed files
with
19 additions
and
45 deletions
... | @@ -7,8 +7,8 @@ const url = 'mongodb://localhost:27017/'; | ... | @@ -7,8 +7,8 @@ const url = 'mongodb://localhost:27017/'; |
7 | exports.startbot = ()=>{ | 7 | exports.startbot = ()=>{ |
8 | // Get authorization to use the slackbot | 8 | // Get authorization to use the slackbot |
9 | const bot = new SlackBot({ | 9 | const bot = new SlackBot({ |
10 | - token : "xoxb-651692943605-645515979745-sfNJBLVbIIb86Bz1gfFc7oT2", | 10 | + token : "xoxb-635297512738-635283536771-A03RoBh4Dv8qG1EiLudMpk1f", |
11 | - name : "joker" | 11 | + name : "jokebot" |
12 | }); | 12 | }); |
13 | 13 | ||
14 | // Start the slackbot | 14 | // Start the slackbot |
... | @@ -135,10 +135,8 @@ function MakeJoke(message){ | ... | @@ -135,10 +135,8 @@ function MakeJoke(message){ |
135 | }) | 135 | }) |
136 | } | 136 | } |
137 | //Gets a random integer | 137 | //Gets a random integer |
138 | -function getRandomInt(max_num) { | 138 | +function getRandomInt(start,end) { |
139 | - min = Math.ceil(1); | 139 | + return Math.floor((Math.random() * 100)%(end-start))+start; |
140 | - max = Math.floor(max_num); | ||
141 | - return Math.floor(Math.random() * (max - min + 1)) + min; | ||
142 | } | 140 | } |
143 | 141 | ||
144 | //Function for giving out random joke | 142 | //Function for giving out random joke |
... | @@ -184,23 +182,15 @@ generalJoke= (user_channel)=>{ | ... | @@ -184,23 +182,15 @@ generalJoke= (user_channel)=>{ |
184 | if (err) throw err; | 182 | if (err) throw err; |
185 | var db = client.db('jokeapi'); | 183 | var db = client.db('jokeapi'); |
186 | 184 | ||
187 | - json_max = 376; | 185 | + random = getRandomInt(24,377); |
188 | - random = getRandomInt(json_max); | ||
189 | result = db.collection('jokes').findOne({id: random}); | 186 | result = db.collection('jokes').findOne({id: random}); |
190 | user = result; | 187 | user = result; |
191 | //if the random picked api type is not general execute the function from the start to get another format for general type | 188 | //if the random picked api type is not general execute the function from the start to get another format for general type |
192 | user.then(function(total){ | 189 | user.then(function(total){ |
193 | - if(total.type === "general"){ | 190 | + question = total.setup; |
194 | - question = total.setup; | 191 | + joke = total.punchline; |
195 | - joke = total.punchline; | 192 | + ques_and_joke = [question, joke]; |
196 | - ques_and_joke = [question, joke]; | 193 | + return ques_and_joke; |
197 | - return ques_and_joke; | ||
198 | - } | ||
199 | - else if(total.type != "general"){ | ||
200 | - client.close(); | ||
201 | - generalJoke(user_channel); | ||
202 | - } | ||
203 | - | ||
204 | }) | 194 | }) |
205 | .then((joke_info)=>{ | 195 | .then((joke_info)=>{ |
206 | bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing')); | 196 | bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing')); |
... | @@ -223,22 +213,14 @@ programmingJoke= (user_channel)=>{ | ... | @@ -223,22 +213,14 @@ programmingJoke= (user_channel)=>{ |
223 | if (err) throw err; | 213 | if (err) throw err; |
224 | var db = client.db('jokeapi'); | 214 | var db = client.db('jokeapi'); |
225 | 215 | ||
226 | - json_max = 376; | 216 | + random = getRandomInt(0,19); |
227 | - random = getRandomInt(json_max); | ||
228 | result = db.collection('jokes').findOne({id: random}); | 217 | result = db.collection('jokes').findOne({id: random}); |
229 | user = result; | 218 | user = result; |
230 | user.then(function(total){ | 219 | user.then(function(total){ |
231 | - if(total.type === "programming"){ | 220 | + question = total.setup; |
232 | - question = total.setup; | 221 | + joke = total.punchline; |
233 | - joke = total.punchline; | 222 | + ques_and_joke = [question, joke]; |
234 | - ques_and_joke = [question, joke]; | 223 | + return ques_and_joke; |
235 | - return ques_and_joke; | ||
236 | - } | ||
237 | - else if(total.type != "programming"){ | ||
238 | - client.close(); | ||
239 | - programmingJoke(user_channel); | ||
240 | - } | ||
241 | - | ||
242 | }) | 224 | }) |
243 | .then((joke_info)=>{ | 225 | .then((joke_info)=>{ |
244 | bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing')); | 226 | bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing')); |
... | @@ -316,23 +298,15 @@ knockknockJoke= (user_channel)=>{ | ... | @@ -316,23 +298,15 @@ knockknockJoke= (user_channel)=>{ |
316 | MongoClient.connect(url, function (err, client){ | 298 | MongoClient.connect(url, function (err, client){ |
317 | if (err) throw err; | 299 | if (err) throw err; |
318 | var db = client.db('jokeapi'); | 300 | var db = client.db('jokeapi'); |
319 | - json_max = 61; | ||
320 | 301 | ||
321 | - random = getRandomInt(json_max); | 302 | + random = getRandomInt(19,24); |
322 | result = db.collection('jokes').findOne({id: random}); | 303 | result = db.collection('jokes').findOne({id: random}); |
323 | user = result; | 304 | user = result; |
324 | user.then(function(total){ | 305 | user.then(function(total){ |
325 | - if(total.type === "knock-knock"){ | 306 | + question = total.setup; |
326 | - question = total.setup; | 307 | + joke = total.punchline; |
327 | - joke = total.punchline; | 308 | + ques_and_joke = [question, joke]; |
328 | - ques_and_joke = [question, joke]; | 309 | + return ques_and_joke; |
329 | - return ques_and_joke; | ||
330 | - } | ||
331 | - else if(total.type != "knock-knock"){ | ||
332 | - client.close(); | ||
333 | - knockknockJoke(user_channel); | ||
334 | - } | ||
335 | - | ||
336 | }) | 310 | }) |
337 | .then((joke_info)=>{ | 311 | .then((joke_info)=>{ |
338 | bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing')); | 312 | bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing')); | ... | ... |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment