[英]Discord.JS Random Image Search
I'm trying to code a bot in discord.js
that generates random images.我正在尝试在
discord.js
中编写一个生成随机图像的机器人。 So far, I did this with cursed images, but when I copy and paste the code and change the case for 'mcimage' and the url to + "minecraft image", mcimage does work.到目前为止,我用被诅咒的图像做了这个,但是当我复制和粘贴代码并将“mcimage”的大小写和url更改为+“minecraft image”时,mcimage确实有效。 However, cursedimage also brings up a minecraft image.
然而,curvedimage 也带来了一个 minecraft 图像。 How do I make it so that cursedimage brings up only cursed images and mcimage brings up only minecraft?
我如何做到让curedimage 只显示被诅咒的图片而mcimage 只显示minecraft? This is my code:
这是我的代码:
client.on('message', (message) => {
let args = message.content.substring(prefix.length).split(' ');
switch (args[0]) {
case 'cursedimage':
image(message);
break;
}
});
function image(message) {
var options = {
url: 'http://results.dogpile.com/serp?qc=images&q=' + 'cursed 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;
}
message.channel.send(urls[Math.floor(Math.random() * urls.length)]);
});
}
You can just pass a variable with the search term for both commands like so:您可以只为两个命令传递一个带有搜索词的变量,如下所示:
switch (args[0]) {
case "cursedimage":
image(message, "cursed image");
break;
case "mcimage":
image(message, "minecraft image");
break;
}
And then on your image function you can pass the search term to the url in options然后在您的图像功能上,您可以将搜索词传递给选项中的网址
function image(message, searchTerm) {
var options = {
url: "http://results.dogpile.com/serp?qc=images&q=" + searchTerm,
method: "GET",
headers: {
Accept: "text/html",
"User-Agent": "Chrome",
},
};
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.