오원석

Merge branch 'ows'

...@@ -107,7 +107,7 @@ function handleMessage(message, current_channel){ ...@@ -107,7 +107,7 @@ function handleMessage(message, current_channel){
107 Funnystory(current_channel); 107 Funnystory(current_channel);
108 } 108 }
109 else if(message.includes(' userjoke')){ 109 else if(message.includes(' userjoke')){
110 - UserMakeJoke(current_channel); 110 + UserMakeJoke(message,current_channel);
111 } 111 }
112 else if(message.includes(' me')){ 112 else if(message.includes(' me')){
113 comment = "Please use @joker --help to know what I can do!:smile::smile::smile:\n You can write type of joke[knock-knock, general, programming, funny story, reddit]"; 113 comment = "Please use @joker --help to know what I can do!:smile::smile::smile:\n You can write type of joke[knock-knock, general, programming, funny story, reddit]";
...@@ -126,22 +126,27 @@ function handleMessage(message, current_channel){ ...@@ -126,22 +126,27 @@ function handleMessage(message, current_channel){
126 } 126 }
127 else if(message.includes(' make joke : ')) 127 else if(message.includes(' make joke : '))
128 { 128 {
129 - MakeJoke(message); 129 + MakeJoke(message,current_channel);
130 } 130 }
131 } 131 }
132 132
133 133
134 -function MakeJoke(message){ 134 +function MakeJoke(message,user_channel){
135 var obj={ 135 var obj={
136 table:[] 136 table:[]
137 } 137 }
138 var input=message.split(':'); 138 var input=message.split(':');
139 var msg=input[1].split(','); 139 var msg=input[1].split(',');
140 - 140 + var temp=message.split(' ');
141 - fs.exists('./joke_data/user.json',function(exists){ 141 + var user=temp[0].substring(2,temp[0].length-1);
142 + console.log('유저 => '+user);
143 +
144 + var path='./joke_data/'+user+'.json';
145 + console.log(path);
146 + fs.exists(path,function(exists){
142 if(exists){ 147 if(exists){
143 console.log("yes file exists"); 148 console.log("yes file exists");
144 - fs.readFile('./joke_data/user.json',function readFileCallback(err,data){ 149 + fs.readFile(path,function readFileCallback(err,data){
145 if(err){ 150 if(err){
146 console.log(err); 151 console.log(err);
147 } 152 }
...@@ -150,7 +155,7 @@ function MakeJoke(message){ ...@@ -150,7 +155,7 @@ function MakeJoke(message){
150 var length=obj.table.length; 155 var length=obj.table.length;
151 obj.table.push({id : length+1, type : 'userjoke', setup : msg[0], punchline : msg[1]}); 156 obj.table.push({id : length+1, type : 'userjoke', setup : msg[0], punchline : msg[1]});
152 var json=JSON.stringify(obj); 157 var json=JSON.stringify(obj);
153 - fs.writeFile('./joke_data/user.json',json,'utf8',function(err){ 158 + fs.writeFile(path,json,'utf8',function(err){
154 if(err){ 159 if(err){
155 console.log(err); 160 console.log(err);
156 } 161 }
...@@ -158,17 +163,21 @@ function MakeJoke(message){ ...@@ -158,17 +163,21 @@ function MakeJoke(message){
158 }); 163 });
159 } 164 }
160 }); 165 });
166 + comment="Sucess making joke!!:+1::thumbsup:\nWhen you want to show your joke, please enter @jokebot tell-me-userjoke";
167 + bot.postMessageToChannel(user_channel,`${comment}:kissing_heart:`,emoji.emojis('nerd_face'));
161 } 168 }
162 else{ 169 else{
163 console.log("file not exists"); 170 console.log("file not exists");
164 obj.table.push({id : 1, type : 'userjoke', setup : msg[0], punchline : msg[1]}); 171 obj.table.push({id : 1, type : 'userjoke', setup : msg[0], punchline : msg[1]});
165 var json=JSON.stringify(obj); 172 var json=JSON.stringify(obj);
166 - fs.writeFile('./joke_data/user.json',json,'utf8',function(err){ 173 + fs.writeFile(path,json,'utf8',function(err){
167 if(err){ 174 if(err){
168 console.log(err); 175 console.log(err);
169 } 176 }
170 console.log('완료'); 177 console.log('완료');
171 }); 178 });
179 + comment="Sucess making joke!!:+1::thumbsup:\nWhen you want to show your joke, please enter @jokebot tell-me-userjoke";
180 + bot.postMessageToChannel(user_channel,`${comment}:kissing_heart:`,emoji.emojis('nerd_face'));
172 } 181 }
173 }) 182 })
174 183
...@@ -199,11 +208,17 @@ randomJoke= (user_channel)=>{ ...@@ -199,11 +208,17 @@ randomJoke= (user_channel)=>{
199 //After finding one joke, use promise to run codes synchronously 208 //After finding one joke, use promise to run codes synchronously
200 user.then(function(total){ 209 user.then(function(total){
201 question = total.setup; 210 question = total.setup;
202 - joke=total.punchline; 211 + bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
203 - ques_and_joke=question+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:'; 212 + console.log("질문 불려짐");
204 - //Ask the question first by extracting 'setup' section from api format 213 + return total;
205 - bot.postMessageToChannel(user_channel, ques_and_joke, emoji.emojis('laughing')); 214 + })
206 - console.log('Random joke~~~'); 215 + .then((all)=>{
216 + joke=all.punchline;
217 + //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
218 + setTimeout(function secondFunction(){
219 + bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
220 + console.log("허무개그 전송~~~~~~!");
221 + },3000);
207 }) 222 })
208 //close mongodb 223 //close mongodb
209 client.close(); 224 client.close();
...@@ -211,8 +226,15 @@ randomJoke= (user_channel)=>{ ...@@ -211,8 +226,15 @@ randomJoke= (user_channel)=>{
211 } 226 }
212 227
213 // Function for giving out users making joke 228 // Function for giving out users making joke
214 -UserMakeJoke= (user_channel)=>{ 229 +UserMakeJoke= (message,user_channel)=>{
215 - var data=fs.readFileSync('./joke_data/user.json'); 230 +
231 + var temp=message.split(' ');
232 + var user=temp[0].substring(2,temp[0].length-1);
233 + console.log('유저 => '+user);
234 +
235 + var path='./joke_data/'+user+'.json';
236 +
237 + var data=fs.readFileSync(path);
216 var jsondata=JSON.parse(data); 238 var jsondata=JSON.parse(data);
217 random=getRandomInt(1,jsondata.table.length+1); 239 random=getRandomInt(1,jsondata.table.length+1);
218 240
...@@ -221,9 +243,11 @@ UserMakeJoke= (user_channel)=>{ ...@@ -221,9 +243,11 @@ UserMakeJoke= (user_channel)=>{
221 if(jsondata.table[i].id==random){ 243 if(jsondata.table[i].id==random){
222 user=jsondata.table[i]; 244 user=jsondata.table[i];
223 question=user.setup; 245 question=user.setup;
246 + bot.postMessageToChannel(user_channel,question, emoji.emojis('laughing'));
224 joke=user.punchline; 247 joke=user.punchline;
225 - result=question+'\n'+joke; 248 + setTimeout(function secondfunction(){
226 - bot.postMessageToChannel(user_channel,result, emoji.emojis('laughing')); 249 + bot.postMessageToChannel(user_channel,joke, emoji.emojis('laughing'));
250 + },3000);
227 console.log("User joke~~~"); 251 console.log("User joke~~~");
228 break; 252 break;
229 } 253 }
...@@ -242,10 +266,17 @@ generalJoke= (user_channel)=>{ ...@@ -242,10 +266,17 @@ generalJoke= (user_channel)=>{
242 //if the random picked api type is not general execute the function from the start to get another format for general type 266 //if the random picked api type is not general execute the function from the start to get another format for general type
243 user.then(function(total){ 267 user.then(function(total){
244 question = total.setup; 268 question = total.setup;
245 - joke = total.punchline; 269 + bot.postMessageToChannel(user_channel,question,emoji.emojis('laughing'));
246 - ques_and_joke=question+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:'; 270 + console.log('질문 불려짐');
247 - bot.postMessageToChannel(user_channel,ques_and_joke,emoji.emojis('laughing')); 271 + return total;
248 - console.log("General Joke~~~~"); 272 + })
273 + .then((all)=>{
274 + joke=all.punchline;
275 + //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
276 + setTimeout(function secondFunction(){
277 + bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
278 + console.log("허무개그 전송~~~~~~!");
279 + },3000);
249 }) 280 })
250 client.close(); 281 client.close();
251 }) 282 })
...@@ -262,10 +293,17 @@ programmingJoke= (user_channel)=>{ ...@@ -262,10 +293,17 @@ programmingJoke= (user_channel)=>{
262 user = result; 293 user = result;
263 user.then(function(total){ 294 user.then(function(total){
264 question = total.setup; 295 question = total.setup;
265 - joke = total.punchline; 296 + bot.postMessageToChannel(user_channel,question,emoji.emojis('laughing'));
266 - ques_and_joke = question+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:'; 297 + console.log("질문 불려짐");
267 - bot.postMessageToChannel(user_channel,ques_and_joke,emoji.emojis('laughing')); 298 + return total;
268 - console.log("Programming Joke~~~"); 299 + })
300 + .then((all)=>{
301 + joke=all.punchline;
302 + //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
303 + setTimeout(function secondFunction(){
304 + bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
305 + console.log("허무개그 전송~~~~~~!");
306 + },3000);
269 }) 307 })
270 client.close(); 308 client.close();
271 }) 309 })
...@@ -279,17 +317,16 @@ Funnystory= (user_channel)=>{ ...@@ -279,17 +317,16 @@ Funnystory= (user_channel)=>{
279 if (err) throw err; 317 if (err) throw err;
280 var db = client.db('FunnyStoryapi'); 318 var db = client.db('FunnyStoryapi');
281 319
282 -
283 random = getRandomInt(1,201); 320 random = getRandomInt(1,201);
284 result = db.collection('FunnyStory').findOne({id: random}); 321 result = db.collection('FunnyStory').findOne({id: random});
285 user = result; 322 user = result;
286 //if the random picked api type is not general execute the function from the start to get another format for general type 323 //if the random picked api type is not general execute the function from the start to get another format for general type
287 user.then(function(total){ 324 user.then(function(total){
288 - category = total.category 325 + cate = total.category;
289 - story=total.body; 326 + story = total.body;
290 - category_story=category+'\n'+story+':stuck_out_tongue_winking_eye::laughing:'; 327 + category_story=cate+'\n'+story+':stuck_out_tongue_winking_eye::laughing:';
291 - bot.postMessageToChannel(user_channel, category, emoji.emojis('smiliey')); 328 + bot.postMessageToChannel(user_channel, category_story, emoji.emojis('smiliey'));
292 - console.log("Funny story~~~"); 329 + console.log("Funny story~~~");
293 }) 330 })
294 client.close(); 331 client.close();
295 }) 332 })
...@@ -308,10 +345,17 @@ redditJoke= (user_channel)=>{ ...@@ -308,10 +345,17 @@ redditJoke= (user_channel)=>{
308 //if the random picked api type is not general execute the function from the start to get another format for general type 345 //if the random picked api type is not general execute the function from the start to get another format for general type
309 user.then(function(total){ 346 user.then(function(total){
310 title = total.title; 347 title = total.title;
311 - joke=total.body; 348 + bot.postMessageToChannel(user_channel, title, emoji.emojis('smiliey'));
312 - title_joke=title+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:'; 349 + console.log("질문 불려짐");
313 - bot.postMessageToChannel(user_channel, title_joke, emoji.emojis('smiliey')); 350 + return total;
314 - console.log("reddit Joke~~~"); 351 + })
352 + .then((all)=>{
353 + joke=all.body;
354 + //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
355 + setTimeout(function secondFunction(){
356 + bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
357 + console.log("허무개그 전송~~~~~~!");
358 + },3000);
315 }) 359 })
316 client.close(); 360 client.close();
317 }) 361 })
...@@ -327,10 +371,16 @@ knockknockJoke= (user_channel)=>{ ...@@ -327,10 +371,16 @@ knockknockJoke= (user_channel)=>{
327 user = result; 371 user = result;
328 user.then(function(total){ 372 user.then(function(total){
329 question = total.setup; 373 question = total.setup;
330 - joke = total.punchline; 374 + bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
331 - ques_and_joke = question+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:'; 375 + console.log("질문 불려짐");
332 - bot.postMessageToChannel(user_channel, ques_and_joke, emoji.emojis('laughing')); 376 + return total;
333 - console.log("Knock-Knock joke~~~"); 377 + })
378 + .then((all)=>{
379 + joke=all.punchline;
380 + setTimeout(function secondFunction(){
381 + bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
382 + console.log("허무개그 전송~~~~~~!");
383 + },3000);
334 }) 384 })
335 client.close(); 385 client.close();
336 }) 386 })
......
1 +{"table":[{"id":1,"type":"userjoke","setup":" one plus one equals?","punchline":" window!!"}]}
...\ No newline at end of file ...\ No newline at end of file
1 -{"table":[{"id":1,"type":"userjoke","setup":" one plus one equals? ","punchline":" window!"},{"id":2,"type":"userjoke","setup":" what 6 x 3 ? ","punchline":" 63 building!"},{"id":3,"type":"userjoke","setup":" what 2 x 9 ?","punchline":" iquana!"},{"id":4,"type":"userjoke","setup":" where are you?","punchline":" your heart"},{"id":5,"type":"userjoke","setup":" jogutae ","punchline":" babo"}]}
...\ No newline at end of file ...\ No newline at end of file