const request = require('request');
const cheerio = require('cheerio');
const Discord = require('discord.js');
const client = new Discord.Client();
const token = ' .....';
const prefix = '-';
client.login(token);
client.on('message', (message) => {
const image2 = message.content.slice(1);
let args = message.content.substring(prefix.length).split(' ');
switch (args[0]) {
case 'random':
image(message);
break;
}
});
function image(message) {
const image2 = message.content.slice(7);
var options = {
url: 'http://results.dogpile.com/serp?qc=images&q=' + image2,
method: 'GET',
headers: {
Accept: 'text/html',
'User-Agent': 'Chrome',
},
};
if (!image2)
var options = {
url: 'http://www.google.com/images?q=' + 'cursed+images',
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)]);
});
}
Okay so i tried to get a random searched image to be shown in discord by my bot, the function to get an Array for "results.dogpile.com" works example : -image dog ( i get a dog image from choosing a random link from Array of result.dogpile.com
If i try : -image ( it doesn't show anything , due to the fact that the Array was made for results.dogpile not for google images user interface )
The question is ... how should i make the Array for google images to get a random image from search ?
I don't believe that's possible, the issue with this is that you would need a database set up yourself with all the current google images you want available, since bots tend to struggle pretty often when connected to the web for browsing, I would recommend you look for a different dependency other than your dogpile thing. I will clarify though, I am no professional, so please, don't take my advice too much, it is only a suggestion from my point of view :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.