简体   繁体   English

我如何向网站发出请求以获取我的 Discord 机器人的狗图片?

[英]How can i make a request to a website to get dog pictures for my discord bot?

I'm making a command that requests dog pictures.我正在发出一个请求狗图片的命令。 My code is strange so far, as I don't fully understand HTTP requests.到目前为止,我的代码很奇怪,因为我不完全理解 HTTP 请求。 Can someone point me in the right direction?有人可以指出我正确的方向吗?

const request = require('request');

    request.get('https://cdn.pixabay.com/photo/2016/11/26/23/45/dog-1861839__340.jpg', {

    }, function(error, response, body) {
        if(!error && response.statusCode == 200) {
            message.channel.send(response.request.uri.href);
        } else {
            console.log(error);
        }
    })

Thanks in advance提前致谢

ps: trying to request from https://pixabay.com/images/search/dog/ this link ps:试图从https://pixabay.com/images/search/dog/这个链接请求

If you already have the picture link, you don't need to do an HTTP request.如果您已经有了图片链接,则无需进行 HTTP 请求。 You only need to show the image in an embed message.您只需要在嵌入消息中显示图像。

const Discord = require("discord.js");

var embed = new Discord.RichEmbed()
.setTitle("Dog picture")
.setDescription("Here is a picture of a dog")
.setImage("https://cdn.pixabay.com/photo/2016/11/26/23/45/dog-1861839__340.jpg")
message.channel.send(embed)

If you don't want to use embeds, you can still post the image only with a simple message.如果您不想使用嵌入,您仍然可以仅通过一条简单的消息发布图像。

message.channel.send("Here is a picture of a dog!", {
    file: "https://cdn.pixabay.com/photo/2016/11/26/23/45/dog-1861839__340.jpg" // Or replace with FileOptions object
});

Edit:编辑:

If you want to get a random dog picture, find a web API.如果你想得到一张随机的狗图片,找一个 Web API。 I found this one.我找到了这个 So now, you put the endpoint link on your request and you parse the result.所以现在,您将端点链接放在您的请求中并解析结果。

const request = require('request');
//Send the request to the API website.
request.get('https://dog.ceo/api/breeds/image/random', {

}, function(error, response, body) {
    if(!error && response.statusCode == 200) {
      var parsedData = JSON.parse(body); //Parse the json data.
      var embed = new Discord.RichEmbed()
      .setTitle("Dog picture")
      .setDescription("Here is a picture of a dog")
      .setImage(parsedData.message)
      .setColor("AQUA")
      message.channel.send(embed);
    } else {
        console.log(error);
    }
})

Using https://www.npmjs.com/package/pixabay-api使用https://www.npmjs.com/package/pixabay-api

var pixabay = require("pixabay-api")
var Discord = require("discord.js")
var key = "your api key"//get key here: https://pixabay.com/api/docs/#api_search_images

with a callack:带有回调:

pixabay.searchImages(key, 'puppy').then((r) => {
       //create embed
        message.reply(new Discord.MessageEmbed()
            .setTitle("Random Puppy")
            //get random puppy image from response
            .setImage(r.hits[Math.floor(Math.random() * r.hits.length)].largeImageURL))
})

async:异步:

var r = await pixabay.searchImages(key, 'puppy')
message.reply(new Discord.MessageEmbed()
     .setTitle("Random Puppy")
     .setImage(r.hits[Math.floor(Math.random() * r.hits.length)].largeImageURL))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM