我是JavaScript的新手(从上周开始),我找不到有效的答案。
如何将带有关键字的随机图像准确地从Google图片发送到Discord频道?
到目前为止,这是我的代码:
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('');
答案 0 :(得分:1)
解决了:
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("");