Sort retrieved YouTube videos from recent to old with postedAt

时间:2018-03-25 19:01:21

标签: javascript youtube-api bots youtube-data-api discord.js

I want my Discord bot to retrieve few YouTube videos from a specific channel. Until there, that works fine, but I want it to retrieve from the recent one to the old one.

Here is my code:

    if(command === "meps") {
    var opts = {
        maxResults: 5,
        key: 'API KEY',
        channelId: 'UCcgvwZgeQIRQ-g9WDqgwXFw',
        type: 'video'
    };
    const lawMeps = new Discord.RichEmbed()
        .setAuthor("lawAMV", "https://yt3.ggpht.com/-464n3t5gsuw/AAAAAAAAAAI/AAAAAAAAAAA/csBjGhz51gY/s176-c-k-no-mo-rj-c0xffffff/photo.jpg")
        .setDescription("Recent MEPs")
        .setThumbnail("https://yt3.ggpht.com/-464n3t5gsuw/AAAAAAAAAAI/AAAAAAAAAAA/csBjGhz51gY/s176-c-k-no-mo-rj-c0xffffff/photo.jpg")
        .setColor("#553B82");
    search('lawAMVs mep', opts, function (err, results) {
        if (err) return console.log(err);
        for (var resultCount = 0; resultCount < opts.maxResults; resultCount++) {
            var publishedAt = new Date(results[resultCount].publishedAt);
            if (results[resultCount].title.startsWith("MEP"))
            {
                lawMeps.addField(results[resultCount].title, results[resultCount].link + "\nPublished at: " + publishedAt, true);
            }
        }
        message.channel.send(lawMeps);
    });
}

And here the screen on how it looks: how it looks

1 个答案:

答案 0 :(得分:0)

这是固定的家伙。

我刚刚在order: 'date',和关键行之间添加了maxResults: 5,

由于某些原因它以前没有用过,但现在进展顺利! :d