async reddit test

This commit is contained in:
Dalton
2018-12-11 22:47:03 -08:00
parent 8d5532459c
commit 8bbf4baa3c

42
bot.js
View File

@@ -227,19 +227,22 @@ function handleRedditBooruJson(data) {
} }
// Makes an async url request with the provided function: 'process'. // Makes an async url request with the provided function: 'process'.
function asyncSiteRequest(url, process) { async function asyncReddit(message) {
fetch(url) // test command for async site request
.then(data => {
return process(data); let messageSplit = message.content.substring(2).split(' ');
}) if (messageSplit.length === 2) {
.then(res => { let reddit_url = "https://www.reddit.com/r/"
return res; + messageSplit[1].replace(/[^a-zA-Z0-9_\-]+/g, '')
}) + "/.json?show=all&count=25&limit=100";
.catch(error => {
sendToBotTestingChannel('async:' + error.message); let reddit_json = await loadJson(reddit_url);
return ""; let reddit_response = handleRedditJson(reddit_json);
}) sendToBotTestingChannel("reddit_url:" + reddit_url + "\nreddit_json:" + reddit_json + "\nreddit_response:" + reddit_response);
} else {
sendToBotTestingChannel("message split length:" + messageSplit.length);
}
} }
async function loadJson(url) { async function loadJson(url) {
@@ -655,20 +658,7 @@ client.on('message', message => {
break; break;
case 'async': case 'async':
// test command for async site request asyncReddit(message);
let messageSplit = message.content.substring(2).split(' ');
if (messageSplit.length === 2) {
let reddit_url = "https://www.reddit.com/r/"
+ messageSplit[1].replace(/[^a-zA-Z0-9_\-]+/g, '')
+ "/.json?show=all&count=25&limit=100";
let reddit_json = loadJson(reddit_url);
let reddit_response = handleRedditJson(reddit_json);
sendToBotTestingChannel("reddit_url:" + reddit_url + "\nreddit_json:" + reddit_json + "\nreddit_response:" + reddit_response);
} else {
sendToBotTestingChannel("message split length:" + messageSplit.length);
}
break; break;
// Voice commands // Voice commands