简体   繁体   中英

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. 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

If you already have the picture link, you don't need to do an HTTP request. 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. 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

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))

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.

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