Skip to content

Instantly share code, notes, and snippets.

@Phineas
Last active January 24, 2017 23:34
Show Gist options
  • Save Phineas/76115d6356adaab2d2489fc5fe5cdf78 to your computer and use it in GitHub Desktop.
Save Phineas/76115d6356adaab2d2489fc5fe5cdf78 to your computer and use it in GitHub Desktop.
Bot command someone on DiscordAPI's guild asked for
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