gpt4 book ai didi

javascript - 如何发送来自谷歌搜索的随机图像?

转载 作者:行者123 更新时间:2023-12-03 00:23:34 24 4
gpt4 key购买 nike

我对 JavaScript 非常陌生(上周开始),我找不到有效的答案。
如何将带有关键字的随机图像从 Google 图片发送到 Discord channel ?

这是我到目前为止的代码:

const GoogleImages = require('google-images');
const Discord = require('discord.js');
const client = new Discord.Client();
const client2 = new GoogleImages('', '');
client.on('ready', () => {
console.log('I am ready!');
});
client2.search('Riolu Pokemon')
.then(images => {});
client.on('message', message => {
if (message.content === 'more riolu') {
return message.channel.send(images);
}
});
client.login('');

最佳答案

解决方法:

const GoogleImages = require("google-images");
const { Client, Attachment } = require("discord.js");
const client = new Client;
const googleImages = new GoogleImages("", "");

async function onMessage(message) {
if (message.content !== "more riolu") return;
try {
const results = await googleImages.search("Riolu Pokemon");
const reply = !results.length ?
"No results" :
new Attachment(results[Math.floor(Math.random() * results.length)].url);
message.channel.send(reply);
}
catch (e) {
console.error(e);
message.channel.send("Error happened, see the console");
}
}

client
.on("ready", () => console.log("I am ready!"))
.on("message", onMessage)
.login("");

关于javascript - 如何发送来自谷歌搜索的随机图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54198495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com