김민욱

devlop user joke to write database

...@@ -3,6 +3,7 @@ const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack- ...@@ -3,6 +3,7 @@ const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack-
3 const dbname = 'jokeapi'; 3 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 +var url2 = 'mongodb://localhost:27017/userjoke'
6 const fs = require('fs'); 7 const fs = require('fs');
7 //const userjoke = require('./joke_data/user.json'); 8 //const userjoke = require('./joke_data/user.json');
8 9
...@@ -21,7 +22,7 @@ exports.startbot = ()=>{ ...@@ -21,7 +22,7 @@ exports.startbot = ()=>{
21 channel_length = data.channels.length; 22 channel_length = data.channels.length;
22 for(i=0; i< channel_length; ++i){ 23 for(i=0; i< channel_length; ++i){
23 //postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name. 24 //postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name.
24 - bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker -help' 25 + bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker [-help]'
25 26
26 , emoji.emojis('bowtie')); 27 , emoji.emojis('bowtie'));
27 } 28 }
...@@ -110,7 +111,7 @@ function handleMessage(message, current_channel){ ...@@ -110,7 +111,7 @@ function handleMessage(message, current_channel){
110 UserMakeJoke(current_channel); 111 UserMakeJoke(current_channel);
111 } 112 }
112 else if(message.includes(' me')){ 113 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]"; 114 + 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]";
114 bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth')); 115 bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth'));
115 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); 116 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
116 } 117 }
...@@ -159,6 +160,18 @@ function MakeJoke(message){ ...@@ -159,6 +160,18 @@ function MakeJoke(message){
159 obj=JSON.parse(data); 160 obj=JSON.parse(data);
160 var length=obj.table.length; 161 var length=obj.table.length;
161 obj.table.push({id : length+1, type : 'userjoke', setup : msg[0], punchline : msg[1]}); 162 obj.table.push({id : length+1, type : 'userjoke', setup : msg[0], punchline : msg[1]});
163 + var myobj = {id : length+1, type : 'userjoke', setup : msg[0], punchline : msg[1]};
164 + MongoClient.connect(url2,function(err,db){
165 + if(err) throw err;
166 + var dbo = db.db("userdb");
167 + dbo.collection("user").insertOne(myobj, function(err,res){
168 + if(err) throw err;
169 + console.log("1 insert!!");
170 + db.close;
171 + })
172 +
173 + })
174 +
162 var json=JSON.stringify(obj); 175 var json=JSON.stringify(obj);
163 fs.writeFile('./joke_data/user.json',json,'utf8',function(err){ 176 fs.writeFile('./joke_data/user.json',json,'utf8',function(err){
164 if(err){ 177 if(err){
...@@ -172,6 +185,34 @@ function MakeJoke(message){ ...@@ -172,6 +185,34 @@ function MakeJoke(message){
172 else{ 185 else{
173 console.log("file not exists"); 186 console.log("file not exists");
174 obj.table.push({id : 1, type : 'userjoke', setup : msg[0], punchline : msg[1]}); 187 obj.table.push({id : 1, type : 'userjoke', setup : msg[0], punchline : msg[1]});
188 + var myobj = {id : 1, type : 'userjoke', setup : msg[0], punchline : msg[1]};
189 + //Create database
190 + MongoClient.connect(url2, { useNewUrlParser: true }, function(err, db) {
191 + if (err) throw err;
192 + console.log("Database created!");
193 + db.close();
194 + });
195 +
196 + //Create collection
197 + MongoClient.connect(url2, { useNewUrlParser: true }, function(err, db) {
198 + if (err) throw err;
199 + var dbo = db.db("userdb");
200 + dbo.createCollection("user", function(err, res) {
201 + if (err) throw err;
202 + console.log("Collection created!");
203 + db.close();
204 + });
205 + });
206 + MongoClient.connect(url2,{ useNewUrlParser: true },function(err,db){
207 + if(err) throw err;
208 + var dbo = db.db("userdb");
209 + dbo.collection("user").insertOne(myobj, function(err,res){
210 + if(err) throw err;
211 + console.log("1 insert!!");
212 + db.close;
213 + })
214 +
215 + });
175 var json=JSON.stringify(obj); 216 var json=JSON.stringify(obj);
176 fs.writeFile('./joke_data/user.json',json,'utf8',function(err){ 217 fs.writeFile('./joke_data/user.json',json,'utf8',function(err){
177 if(err){ 218 if(err){
...@@ -400,7 +441,7 @@ knockknockJoke= (user_channel)=>{ ...@@ -400,7 +441,7 @@ knockknockJoke= (user_channel)=>{
400 //Function for giving out information to user to control the bot 441 //Function for giving out information to user to control the bot
401 runHelp = (user_channel) =>{ 442 runHelp = (user_channel) =>{
402 443
403 - comment = "Thanks for using Joker bot!:ghost::ghost:laugh:\nBot info: type '@joker -help' for infos\nBot functions: '@joker tell me [something] joke' will send related jokes, if I don't have what you mentioned, I will tell you I don't have that joke:smile:\n" 444 + comment = "Thanks for using Joker bot!:ghost::ghost:laugh:\nBot info: type '@joker [-help]' for infos\nBot functions: '@joker tell me [something] joke' will send related jokes, if I don't have what you mentioned, I will tell you I don't have that joke:smile:\n"
404 current_jokes = "Joke types I have: general , knock-knock , programming , reddit, funny story" 445 current_jokes = "Joke types I have: general , knock-knock , programming , reddit, funny story"
405 bot.postMessageToChannel(user_channel, comment + current_jokes ,emoji.emojis('question')); 446 bot.postMessageToChannel(user_channel, comment + current_jokes ,emoji.emojis('question'));
406 } 447 }
......