김연준

Figures out the user's channel automatically/ added detailed comments for description

...@@ -16,8 +16,19 @@ exports.startbot = ()=>{ ...@@ -16,8 +16,19 @@ exports.startbot = ()=>{
16 const face = { 16 const face = {
17 icon_emoji: ':bowtie:' 17 icon_emoji: ':bowtie:'
18 }; 18 };
19 - bot.postMessageToChannel('everyone', 'Have some fun with @Joker!\nFor commands write @joker --help' 19 + //Figure out user's all current channels and send the starting message
20 - , face, '/play secret'); 20 + channel = bot.getChannels();
21 + channel.then((data)=>{
22 + channel_length = data.channels.length;
23 + for(i=0; i< channel_length; ++i){
24 + bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker --help'
25 + , face);
26 + }
27 + return data;
28 + })
29 + .then((data)=>{
30 + console.log('Sucessfully started app to all channels');
31 + })
21 }); 32 });
22 // Error Handler 33 // Error Handler
23 bot.on('error', (err) => console.log(err)); 34 bot.on('error', (err) => console.log(err));
...@@ -27,43 +38,80 @@ bot.on('message', (data) => { ...@@ -27,43 +38,80 @@ bot.on('message', (data) => {
27 if(data.type !== 'message'){ 38 if(data.type !== 'message'){
28 return; 39 return;
29 } 40 }
41 + status = data;
42 + message_recieved = 0;
43 + //If it's first input from the user, go through this loop to store the data of channel names and ids
44 + if(message_recieved == 0){
45 + channel_length;
46 + channel_names = [];
47 + channel_ids = [];
48 +
49 + channel = bot.getChannels();
50 + channel.then((data)=>{
51 + channel_length = data.channels.length;
52 + for(i=0; i< channel_length; ++i){
53 + channel_names.push(data.channels[i].name);
54 + channel_ids.push(data.channels[i].id);
55 + }
56 + return data;
57 + })
58 + .then((result)=>{
59 + ++message_recieved;
60 + console.log("User Channel list: " + channel_names)
61 + for(i=0; i< channel_length; ++i){
62 + if(channel_ids[i] == status.channel)
63 + handleMessage(status.text, channel_names[i]);
64 + }
65 + })
66 + }
67 + console.log(status);
30 68
31 - console.log(data); 69 + //If it's not the first user input, goes through simple loop to shorten response time
32 - handleMessage(data.text, data.channel, data.user); 70 + if(message_recieved > 0){
71 + for(i=0; i< channel_length; ++i){
72 + if(channel_ids[i] == status.channel)
73 + handleMessage(status.text, channel_names[i]);
74 + }
75 + }
76 + //handleMessage(data.text);
33 }); 77 });
34 78
35 79
36 // Responding to Data 80 // Responding to Data
37 -function handleMessage(message, channel, user){ 81 +function handleMessage(message, current_channel){
38 console.log(message); 82 console.log(message);
39 83
84 +//Handles message response depending on the user message
40 if(message.includes(' tell me')){ 85 if(message.includes(' tell me')){
41 if(message.includes(' knock')){ 86 if(message.includes(' knock')){
42 - knockknockJoke(); 87 + knockknockJoke(current_channel);
43 } 88 }
44 else if(message.includes(' general')){ 89 else if(message.includes(' general')){
45 - generalJoke(); 90 + generalJoke(current_channel);
46 } 91 }
47 92
48 else if(message.includes(' random')){ 93 else if(message.includes(' random')){
49 - randomJoke(); 94 + randomJoke(current_channel);
50 } 95 }
51 else if(message.includes(' a joke')){ 96 else if(message.includes(' a joke')){
52 - randomJoke(); 97 + randomJoke(current_channel);
53 } 98 }
54 99
55 else if(message.includes(' programming')){ 100 else if(message.includes(' programming')){
56 - programmingJoke(); 101 + programmingJoke(current_channel);
57 } 102 }
58 else if(message.includes(' me ')){ 103 else if(message.includes(' me ')){
59 - bot.postMessageToChannel('everyone', "Tell you what??? :nomouth:", embarrased); 104 + const quiet = {
105 + icon_emoji: ':no_mouth:'
106 + }
107 + bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", quiet);
60 } 108 }
61 else{ 109 else{
62 const embarrased = { 110 const embarrased = {
63 icon_emoji: ':flushed:' 111 icon_emoji: ':flushed:'
64 }; 112 };
65 comment = "Sorry I dont' have that kind of joke.....:droplet::droplet::droplet:\nPlease use @joker --help to know what I can do!"; 113 comment = "Sorry I dont' have that kind of joke.....:droplet::droplet::droplet:\nPlease use @joker --help to know what I can do!";
66 - bot.postMessageToChannel('everyone', comment, embarrased); 114 + bot.postMessageToChannel(current_channel, comment, embarrased);
67 115
68 } 116 }
69 117
...@@ -76,7 +124,7 @@ function handleMessage(message, channel, user){ ...@@ -76,7 +124,7 @@ function handleMessage(message, channel, user){
76 const face = { 124 const face = {
77 icon_emoji: ':thumbsup:' 125 icon_emoji: ':thumbsup:'
78 }; 126 };
79 - bot.postMessageToChannel("everyone", `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]} jokes!! :thumbsup: :thumbsup:`, face); 127 + bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]} jokes!! :thumbsup: :thumbsup:`, face);
80 return; 128 return;
81 } 129 }
82 // else{ 130 // else{
...@@ -91,18 +139,22 @@ function handleMessage(message, channel, user){ ...@@ -91,18 +139,22 @@ function handleMessage(message, channel, user){
91 139
92 // } 140 // }
93 } 141 }
94 -randomJoke= ()=>{ 142 +
143 +//Gets a random integer
144 +function getRandomInt(max_num) {
145 + min = Math.ceil(1);
146 + max = Math.floor(max_num);
147 + return Math.floor(Math.random() * (max - min + 1)) + min;
148 +}
149 +
150 +//Function for giving out random joke
151 +randomJoke= (user_channel)=>{
95 MongoClient.connect('mongodb://localhost:27017', function (err, client){ 152 MongoClient.connect('mongodb://localhost:27017', function (err, client){
96 if (err) throw err; 153 if (err) throw err;
97 var db = client.db('jokeapi'); 154 var db = client.db('jokeapi');
98 155
99 json_max = 376; 156 json_max = 376;
100 - function getRandomInt() { 157 + random = getRandomInt(json_max);
101 - min = Math.ceil(1);
102 - max = Math.floor(376);
103 - return Math.floor(Math.random() * (max - min + 1)) + min;
104 - }
105 - random = getRandomInt();
106 result = db.collection('jokes').findOne({id: random}); 158 result = db.collection('jokes').findOne({id: random});
107 159
108 user = result; 160 user = result;
...@@ -112,20 +164,14 @@ randomJoke= ()=>{ ...@@ -112,20 +164,14 @@ randomJoke= ()=>{
112 icon_emoji: ':laughing:' 164 icon_emoji: ':laughing:'
113 }; 165 };
114 166
115 - function firstFunction(channel){ 167 + function firstFunction(){
116 - bot.postMessageToChannel(channel, question, face); 168 + bot.postMessageToChannel(user_channel, question, face);
117 } 169 }
118 firstFunction('everyone'); 170 firstFunction('everyone');
119 console.log('질문 불려짐'); 171 console.log('질문 불려짐');
120 return total; 172 return total;
121 173
122 174
123 - // bot.postMessageToChannel('everyone', question, face);
124 - // bot.postMessageToChannel('full-stack-web', question, joke, face);
125 - // bot.postMessageToChannel('bot_test', question, face);
126 - // bot.postMessageToChannel('everyone', joke, face);
127 - // bot.postMessageToChannel('full-stack-web', joke, face);
128 - // bot.postMessageToChannel('bot_test', joke, face);
129 }) 175 })
130 .then((all)=>{ 176 .then((all)=>{
131 joke = all.punchline; 177 joke = all.punchline;
...@@ -133,7 +179,7 @@ randomJoke= ()=>{ ...@@ -133,7 +179,7 @@ randomJoke= ()=>{
133 icon_emoji: ':laughing:' 179 icon_emoji: ':laughing:'
134 }; 180 };
135 setTimeout(function secondFunction(){ 181 setTimeout(function secondFunction(){
136 - bot.postMessageToChannel('everyone', `${joke}:stuck_out_tongue_winking_eye::laughing:`, face, '/play secret') 182 + bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, face, '/play secret')
137 console.log( "허무개그 전송~~~~!") 183 console.log( "허무개그 전송~~~~!")
138 }, 3000); 184 }, 3000);
139 185
...@@ -141,18 +187,15 @@ randomJoke= ()=>{ ...@@ -141,18 +187,15 @@ randomJoke= ()=>{
141 client.close(); 187 client.close();
142 }) 188 })
143 } 189 }
144 -generalJoke= ()=>{ 190 +
191 +//Function for giving out random joke after filtering only general type jokes
192 +generalJoke= (user_channel)=>{
145 MongoClient.connect(url, function (err, client){ 193 MongoClient.connect(url, function (err, client){
146 if (err) throw err; 194 if (err) throw err;
147 var db = client.db('jokeapi'); 195 var db = client.db('jokeapi');
148 196
149 json_max = 376; 197 json_max = 376;
150 - function getRandomInt() { 198 + random = getRandomInt(json_max);
151 - min = Math.ceil(1);
152 - max = Math.floor(json_max);
153 - return Math.floor(Math.random() * (max - min + 1)) + min;
154 - }
155 - random = getRandomInt();
156 result = db.collection('jokes').findOne({id: random}); 199 result = db.collection('jokes').findOne({id: random});
157 user = result; 200 user = result;
158 user.then(function(total){ 201 user.then(function(total){
...@@ -168,13 +211,13 @@ generalJoke= ()=>{ ...@@ -168,13 +211,13 @@ generalJoke= ()=>{
168 } 211 }
169 else if(total.type != "general"){ 212 else if(total.type != "general"){
170 client.close(); 213 client.close();
171 - generalJoke(); 214 + generalJoke(user_channel);
172 } 215 }
173 216
174 }) 217 })
175 .then((joke_info)=>{ 218 .then((joke_info)=>{
176 function askQuestion(){ 219 function askQuestion(){
177 - bot.postMessageToChannel(joke_info[3], joke_info[0], joke_info[2]); 220 + bot.postMessageToChannel(user_channel, joke_info[0], joke_info[2]);
178 console.log("일반 질문 불려짐"); 221 console.log("일반 질문 불려짐");
179 } 222 }
180 askQuestion(); 223 askQuestion();
...@@ -182,25 +225,22 @@ generalJoke= ()=>{ ...@@ -182,25 +225,22 @@ generalJoke= ()=>{
182 }) 225 })
183 .then((info)=>{ 226 .then((info)=>{
184 setTimeout(function secondFunction(){ 227 setTimeout(function secondFunction(){
185 - bot.postMessageToChannel(info[3], `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, info[2]) 228 + bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, info[2])
186 console.log( "허무개그 전송~~~~!") 229 console.log( "허무개그 전송~~~~!")
187 }, 3000); 230 }, 3000);
188 }) 231 })
189 client.close(); 232 client.close();
190 }) 233 })
191 }; 234 };
192 -programmingJoke= ()=>{ 235 +
236 +//Function for giving out random joke after filtering only programming type jokes
237 +programmingJoke= (user_channel)=>{
193 MongoClient.connect(url, function (err, client){ 238 MongoClient.connect(url, function (err, client){
194 if (err) throw err; 239 if (err) throw err;
195 var db = client.db('jokeapi'); 240 var db = client.db('jokeapi');
196 241
197 json_max = 376; 242 json_max = 376;
198 - function getRandomInt() { 243 + random = getRandomInt(json_max);
199 - min = Math.ceil(1);
200 - max = Math.floor(376);
201 - return Math.floor(Math.random() * (max - min + 1)) + min;
202 - }
203 - random = getRandomInt();
204 result = db.collection('jokes').findOne({id: random}); 244 result = db.collection('jokes').findOne({id: random});
205 user = result; 245 user = result;
206 user.then(function(total){ 246 user.then(function(total){
...@@ -216,19 +256,13 @@ programmingJoke= ()=>{ ...@@ -216,19 +256,13 @@ programmingJoke= ()=>{
216 } 256 }
217 else if(total.type != "programming"){ 257 else if(total.type != "programming"){
218 client.close(); 258 client.close();
219 - programmingJoke(); 259 + programmingJoke(user_channel);
220 } 260 }
221 261
222 - // bot.postMessageToChannel('everyone', question, face);
223 - // bot.postMessageToChannel('full-stack-web', question, joke, face);
224 - // bot.postMessageToChannel('bot_test', question, face);
225 - // bot.postMessageToChannel('everyone', joke, face);
226 - // bot.postMessageToChannel('full-stack-web', joke, face);
227 - // bot.postMessageToChannel('bot_test', joke, face);
228 }) 262 })
229 .then((joke_info)=>{ 263 .then((joke_info)=>{
230 function askQuestion(){ 264 function askQuestion(){
231 - bot.postMessageToChannel(joke_info[3], joke_info[0], joke_info[2]); 265 + bot.postMessageToChannel(user_channel, joke_info[0], joke_info[2]);
232 console.log("프로그래밍 질문 불려짐"); 266 console.log("프로그래밍 질문 불려짐");
233 } 267 }
234 askQuestion(); 268 askQuestion();
...@@ -236,7 +270,7 @@ programmingJoke= ()=>{ ...@@ -236,7 +270,7 @@ programmingJoke= ()=>{
236 }) 270 })
237 .then((info)=>{ 271 .then((info)=>{
238 setTimeout(function secondFunction(){ 272 setTimeout(function secondFunction(){
239 - bot.postMessageToChannel(info[3], `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, info[2]) 273 + bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, info[2])
240 console.log( "허무개그 전송~~~~!") 274 console.log( "허무개그 전송~~~~!")
241 }, 3000); 275 }, 3000);
242 }) 276 })
...@@ -244,18 +278,15 @@ programmingJoke= ()=>{ ...@@ -244,18 +278,15 @@ programmingJoke= ()=>{
244 }) 278 })
245 }; 279 };
246 280
247 -knockknockJoke= ()=>{ 281 +//Function for giving out random joke after filtering only knock-knock type jokes
282 +knockknockJoke= (user_channel)=>{
248 MongoClient.connect(url, function (err, client){ 283 MongoClient.connect(url, function (err, client){
249 if (err) throw err; 284 if (err) throw err;
250 var db = client.db('jokeapi'); 285 var db = client.db('jokeapi');
251 286
252 json_max = 61; 287 json_max = 61;
253 - function getRandomInt() { 288 +
254 - min = Math.ceil(1); 289 + random = getRandomInt(json_max);
255 - max = Math.floor(json_max);
256 - return Math.floor(Math.random() * (max - min + 1)) + min;
257 - }
258 - random = getRandomInt();
259 result = db.collection('jokes').findOne({id: random}); 290 result = db.collection('jokes').findOne({id: random});
260 user = result; 291 user = result;
261 user.then(function(total){ 292 user.then(function(total){
...@@ -271,13 +302,13 @@ knockknockJoke= ()=>{ ...@@ -271,13 +302,13 @@ knockknockJoke= ()=>{
271 } 302 }
272 else if(total.type != "knock-knock"){ 303 else if(total.type != "knock-knock"){
273 client.close(); 304 client.close();
274 - knockknockJoke(); 305 + knockknockJoke(user_channel);
275 } 306 }
276 307
277 }) 308 })
278 .then((joke_info)=>{ 309 .then((joke_info)=>{
279 function askQuestion(){ 310 function askQuestion(){
280 - bot.postMessageToChannel(joke_info[3], joke_info[0], joke_info[2]); 311 + bot.postMessageToChannel(user_channel, joke_info[0], joke_info[2]);
281 console.log("똑똑 질문 불려짐"); 312 console.log("똑똑 질문 불려짐");
282 } 313 }
283 askQuestion(); 314 askQuestion();
...@@ -285,13 +316,15 @@ knockknockJoke= ()=>{ ...@@ -285,13 +316,15 @@ knockknockJoke= ()=>{
285 }) 316 })
286 .then((info)=>{ 317 .then((info)=>{
287 setTimeout(function secondFunction(){ 318 setTimeout(function secondFunction(){
288 - bot.postMessageToChannel(info[3], `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, info[2]) 319 + bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, info[2])
289 console.log( "허무개그 전송~~~~!") 320 console.log( "허무개그 전송~~~~!")
290 }, 3000); 321 }, 3000);
291 }) 322 })
292 client.close(); 323 client.close();
293 }) 324 })
294 } 325 }
326 +
327 +//Function for giving out information to user to control the bot
295 runHelp = () =>{ 328 runHelp = () =>{
296 329
297 const face = { 330 const face = {
......