I am trying to make a discord bot where I search for "example image" and it takes a random image from google under that search term and sends it.
This is what I have so far:
bot.on('message', message => {
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case 'image':
image(message);
break;
}
});
function image(message){
var options = {
url: "http://results.dogpile.com/serp?qc=images&q=" + "example image",
method: "GET",
headers: {
"Accept": "text/html",
"User-Agent": "Chrome"
}
};
request(options, function(error, response, responseBody) {
if (error) {
return;
}
$ = cheerio.load(responseBody);
var links = $(".image a.link");
var urls = new Array(links.length).fill(0).map((v, i) => links.eq(i).attr("href"));
console.log(urls);
if (!urls.length) {
return;
}
// Send result
message.channel.send( urls[Math.floor(Math.random() * urls.length)]);
});
and it works but dogpile doesn't give some images for some reason, so basically I tried everything to make it search in google instead of dogpile but it doesn't work.
TLDR: How do I search in google instead of dogpile.