首页 > 解决方案 > 如果我从 .json 中提取具有自动重定向的 url,如何使输出成为我将被重定向到的页面?- Javascript

问题描述

有问题的网址不是来自我自己的网站。

此代码使用 discord.js,用于让机器人从 reddits .json 中提取帖子的 url。它不适用于 nsfw gfycat 链接,因为它们现在都自动重定向到它们在 gifdeliverynetwork 中的新位置。

    if(cmd == `${prefix}r`){
    if(message.channel.nsfw){
        request("https://www.reddit.com/r/" + args + "/top/.json?t=all&limit=100", function(error, response, body){
            let msg = JSON.parse(body);
                try{
                    let imgurl = msg["data"]["children"][Math.floor((Math.random() * 100) + 0)]["data"]["url"];
                    if (imgurl.includes('gfycat')) {
                        return message.channel.send(imgurl + "\nr **" + args + "** requested by **" + message.author.tag + "**" + "\n***can't see the post?** - discord doesn't support most gfycat links but you can still click it!*");
                    } else {
                        return message.channel.send(imgurl + "\nr **" + args + "** requested by **" + message.author.tag + "**");}}                        
                catch{
                    return message.channel.send("Error getting data from the subreddit - **" + args + "**");
                }
            });
    } else {
        return message.channel.send("This channel is not set to nsfw. Please use <rp");
    }
}

标签: javascript

解决方案


推荐阅读