오원석

usermakejoke

...@@ -4,7 +4,6 @@ const dbname = 'jokeapi'; ...@@ -4,7 +4,6 @@ const dbname = 'jokeapi';
4 const emoji = require('../slack_emoji'); 4 const emoji = require('../slack_emoji');
5 const url = 'mongodb://localhost:27017/'; 5 const url = 'mongodb://localhost:27017/';
6 const fs=require('fs'); 6 const fs=require('fs');
7 -num=1;
8 //const userjoke = require('./joke_data/user.json'); 7 //const userjoke = require('./joke_data/user.json');
9 8
10 exports.startbot = ()=>{ 9 exports.startbot = ()=>{
...@@ -107,14 +106,15 @@ function handleMessage(message, current_channel){ ...@@ -107,14 +106,15 @@ function handleMessage(message, current_channel){
107 else if(message.includes(' funny story')){ 106 else if(message.includes(' funny story')){
108 Funnystory(current_channel); 107 Funnystory(current_channel);
109 } 108 }
109 + else if(message.includes(' userjoke')){
110 + UserMakeJoke(current_channel);
111 + }
110 else if(message.includes(' me')){ 112 else if(message.includes(' me')){
111 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]";
112 bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth')); 114 bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth'));
113 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); 115 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
114 } 116 }
115 - else if(message.includes(' userjoke')){ 117 +
116 - UserMakeJoke(current_channel);
117 - }
118 } 118 }
119 else if(message.includes(' help')){ 119 else if(message.includes(' help')){
120 comment = "If you want to start @joker then write [tell me] and write type of joke [knock-knock, general, programming, funny story, reddit]:smiley:!!!"; 120 comment = "If you want to start @joker then write [tell me] and write type of joke [knock-knock, general, programming, funny story, reddit]:smiley:!!!";
...@@ -148,8 +148,7 @@ function MakeJoke(message){ ...@@ -148,8 +148,7 @@ function MakeJoke(message){
148 } 148 }
149 var input=message.split(':'); 149 var input=message.split(':');
150 var msg=input[1].split(','); 150 var msg=input[1].split(',');
151 - // obj.table.push({id : msg[1], type : msg[2], setup : msg[3], punchling : msg[4]}); 151 +
152 - // var json=JSON.stringify(obj);
153 fs.exists('./joke_data/user.json',function(exists){ 152 fs.exists('./joke_data/user.json',function(exists){
154 if(exists){ 153 if(exists){
155 console.log("yes file exists"); 154 console.log("yes file exists");
...@@ -159,9 +158,9 @@ function MakeJoke(message){ ...@@ -159,9 +158,9 @@ function MakeJoke(message){
159 } 158 }
160 else{ 159 else{
161 obj=JSON.parse(data); 160 obj=JSON.parse(data);
162 - obj.table.push({id : num, type : 'userjoke', setup : msg[0], punchling : msg[1]}); 161 + var length=obj.table.length;
162 + obj.table.push({id : length+1, type : 'userjoke', setup : msg[0], punchline : msg[1]});
163 var json=JSON.stringify(obj); 163 var json=JSON.stringify(obj);
164 - num++;
165 fs.writeFile('./joke_data/user.json',json,'utf8',function(err){ 164 fs.writeFile('./joke_data/user.json',json,'utf8',function(err){
166 if(err){ 165 if(err){
167 console.log(err); 166 console.log(err);
...@@ -173,9 +172,8 @@ function MakeJoke(message){ ...@@ -173,9 +172,8 @@ function MakeJoke(message){
173 } 172 }
174 else{ 173 else{
175 console.log("file not exists"); 174 console.log("file not exists");
176 - obj.table.push({id : num, type : 'userjoke', setup : msg[0], punchling : msg[1]}); 175 + obj.table.push({id : 1, type : 'userjoke', setup : msg[0], punchline : msg[1]});
177 var json=JSON.stringify(obj); 176 var json=JSON.stringify(obj);
178 - num++;
179 fs.writeFile('./joke_data/user.json',json,'utf8',function(err){ 177 fs.writeFile('./joke_data/user.json',json,'utf8',function(err){
180 if(err){ 178 if(err){
181 console.log(err); 179 console.log(err);
...@@ -233,8 +231,23 @@ randomJoke= (user_channel)=>{ ...@@ -233,8 +231,23 @@ randomJoke= (user_channel)=>{
233 } 231 }
234 232
235 // Function for giving out users making joke 233 // Function for giving out users making joke
236 -UserMakeJoke= (users_channel)=>{ 234 +UserMakeJoke= (user_channel)=>{
237 - 235 + var data=fs.readFileSync('./joke_data/user.json');
236 + var jsondata=JSON.parse(data);
237 + random=getRandomInt(1,jsondata.table.length+1);
238 +
239 + console.log(random);
240 + for(var i=0;i<jsondata.table.length;i++){
241 + if(jsondata.table[i].id==random){
242 + user=jsondata.table[i];
243 + console.log(user);
244 + question=user.setup;
245 + joke=user.punchline;
246 + result=question+'\n'+joke;
247 + bot.postMessageToChannel(user_channel,result, emoji.emojis('laughing'));
248 + break;
249 + }
250 + }
238 } 251 }
239 252
240 //Function for giving out random joke after filtering only general type jokes 253 //Function for giving out random joke after filtering only general type jokes
...@@ -274,7 +287,7 @@ programmingJoke= (user_channel)=>{ ...@@ -274,7 +287,7 @@ programmingJoke= (user_channel)=>{
274 if (err) throw err; 287 if (err) throw err;
275 var db = client.db('jokeapi'); 288 var db = client.db('jokeapi');
276 289
277 - random = getRandomInt(0,19); 290 + random = getRandomInt(1,19);
278 result = db.collection('jokes').findOne({id: random}); 291 result = db.collection('jokes').findOne({id: random});
279 user = result; 292 user = result;
280 user.then(function(total){ 293 user.then(function(total){
......
1 -{"table":[{"id":1,"type":"users","setup":"hello","punchling":" hi"},{"id":2,"type":"users","setup":"hi","punchling":"hello"},{"id":3,"type":"userjoke","setup":" what 6 x 3?","punchling":" 63 buliding!"}]}
...\ 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"}]}
...\ No newline at end of file ...\ No newline at end of file
......