Last active
January 24, 2017 23:34
-
-
Save Phineas/76115d6356adaab2d2489fc5fe5cdf78 to your computer and use it in GitHub Desktop.
Bot command someone on DiscordAPI's guild asked for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'), | |
bot = require("discord.js"); | |
const baseUrl = 'https://api.imgur.com/3/', | |
clientID = ''; | |
bot.on("message", msg => { | |
if(msg.content.startsWith("!img")) { | |
tagToSeatch = msg.content.split("")[1]; | |
randImg(tagToSearch, function(res) { | |
msg.channel.sendMessage(res); | |
}); | |
} | |
}); | |
function randImg(tag, callback) { | |
request({ | |
url: baseUrl + 'gallery/t/' + tag + '/viral/0', | |
method: 'GET', | |
headers: { | |
'Authorization': 'Client-ID ' + clientID | |
} | |
}, function(error, response, body){ | |
if(error) { | |
console.log("Error: "+error); | |
callback(error); | |
} else { | |
dataset = JSON.parse(body); | |
if(dataset.data.items.length > 0) { | |
callback('No results ):'); | |
} else { | |
callback(dataset.data.items[0].link); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment