diff --git a/bot.js b/bot.js index 9f6547b..ff16609 100644 --- a/bot.js +++ b/bot.js @@ -11,7 +11,9 @@ var queueExists = false; var channel = null; // grab config -var bot_config = asyncSiteRequest('http://127.0.0.1/folder/bot-config.json', config_data => {return config_data;}); +var bot_config = asyncSiteRequest('http://127.0.0.1/folder/bot-config.json', config_data => { + return config_data; +}); // available commands // todo use config file @@ -56,8 +58,7 @@ function commandsHelp(requestedCommand) { if (commands[requestedCommand] === undefined) { console.log('requestedCommand not found in map.'); return "Command not found, try entering **b.commands** for a full list of commands"; - } - else { + } else { console.log('requestedCommand found, returning requestedCommand map entry.'); return commands[requestedCommand]; } @@ -124,14 +125,16 @@ function noQuestion() { // 3. RedditBooru function bestEffortRequest(subpage, page_max) { - let reddit_response = asyncSiteRequest("https://www.reddit.com/r/" + subpage + "/.json?show=all&count=25&limit=100", handleRedditJson); + let reddit_url = "https://www.reddit.com/r/" + subpage + "/.json?show=all&count=25&limit=100"; + let reddit_response = asyncSiteRequest(reddit_url, handleRedditJson); if (reddit_response) { return reddit_response; } + let imgur_url = 'https://api.imgur.com/3/gallery/r/' + multiSubreddit[getRandomInt(0, multiSubreddit.length - 1)] + '/time/' + getRandomInt(1, page_max); let imgur_response; if (page_max) { - imgur_response = imgurRequest(subpage, page_max); + imgur_response = asyncSiteRequest(imgur_url, handleImgurJson); if (imgur_response) { return imgur_response } @@ -142,6 +145,7 @@ function bestEffortRequest(subpage, page_max) { return redditbooru_response } + // leaving this here for debug. sendToBotTestingChannel('reddit:' + reddit_response + '\nimgur:' + imgur_response + '\nredditbooru:' + redditbooru_response); return "I couldn't find that, sauce?"; @@ -194,6 +198,16 @@ function handleRedditJson(data) { return ""; } +// Abstracted Imgur Json media grab. +function handleImgurJson(data) { + + let json = data.json(); + let json_data = json.data[getRandomInt(0, json.data.length - 1)]; + if (json_data) { + return json_data.link; + } +} + // Makes an async url request with the provided function: 'process'. function asyncSiteRequest(url, process) { @@ -232,8 +246,7 @@ function subredditRequest(subreddit) { returnText = json.data.children[i].data.url; break; } - } - catch (error) // error parsing json + } catch (error) // error parsing json { // Ignore the error and continue. // continue; <-- last line in the loop... so does nothing. @@ -313,8 +326,7 @@ function setupVoice(file, volume, voice_channel) { handlePlayQueue(connection); }); } - } - else { + } else { playQueue.pop(); playVolume.pop(); channel = null; @@ -339,8 +351,7 @@ function handlePlayQueue(connection) { channel.leave(); channel = null; } - } - else { + } else { handlePlayQueue(connection); } }) @@ -397,8 +408,7 @@ client.on('message', message => { let help_content = message.content.substring(2).split(' '); if (message.content.substring(2).split(' ')[1] === undefined) { message.channel.send(availableCommands()); - } - else { + } else { message.channel.send(commandsHelp(help_content[1])); } break; @@ -410,8 +420,7 @@ client.on('message', message => { if (message.content.substring(8).length > 0) { message.channel.send(message.author + ' asked: *' + message.content.substring(8) + '*\n' + 'The magic 8 ball says: **' + eightBall() + '**'); - } - else { + } else { message.reply(noQuestion()); } break; @@ -424,12 +433,10 @@ client.on('message', message => { let range_break = roll_content[1].split('-'); message.channel.send(message.author + ' rolls a number between ' + range_break[0] + ' and ' + range_break[1] + '. They roll **' + numberRoll(range_break[0], range_break[1]) + '**.'); - } - else { + } else { message.channel.send('Unknown roll format. Use "**b.help roll**" for more information.'); } - } - else // default roll (1-100) + } else // default roll (1-100) { message.channel.send(message.author + ' rolls a number between 1 and 100. They roll **' + numberRoll(1, 100) + '**.'); } @@ -445,8 +452,7 @@ client.on('message', message => { } message.channel.send(message.author + ' wants to compute: *' + message.content.substring(message.content.indexOf(' ') + 1) + '*\n' + 'The result is: **' + response + '**') - } - else { + } else { message.reply(noQuestion()); } break;