2

I'm working on a Discordbot that takes links to music posted in a channel and stores them in an array. I'm using it to create a queue of all the submitted tracks.

Most people link SoundCloud or YouTube links. Which is working fine so far. But sometimes people post a link starting with https://soundcloud.app.goo.gl Which redirects to the actual https://soundcloud.com/username/track

Same thing with https://youtu.be which redirects to https://youtube.com/etcetc

const client_2 = new Discord.Client();    
client_2.on('message', async (msg) => {
const linksubmit = "https://";
if (msg.content.startsWith(linksubmit)) {
let submit = msg.toString();
    }
}

This stores any posted link into the variable submit. But is there a way to find the redirect url, and then store it into the variable? (with JavaScript)

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • You can check the headers for a location header, You will have to fetch it first, if you can fetch the headers only this will be more light weight. – Michael Mano Jul 15 '21 at 01:33

1 Answers1

0

What I can think of is using the replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced [read more]. so you can do script to rewrite your links were it changes to fit your format, like this:

var URLRewriter = function(a, b) {
    $('a[href*="' + a + '"]').each(function() {
        $(this).attr('href', $(this).attr('href').replace(a, b));
    });
};

URLRewriter('youtu.be', 'youtube.com');
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Themodmin
  • 425
  • 2
  • 8
  • 20
  • 2
    Ah yeah that will definitely work for youtube.be links. cause the youtu.be links already have the relevant data in the url for the redirect. example: https://youtu.be/Hjci0x-ZnhY redirects to https://www.youtube.com/watch?v=Hjci0x-ZnhY but the soundcloud.app.goo.gl are a complete redirect. example: https://soundcloud.app.goo.gl/ZNGsRFCn5WsWUBj38 redirects to https://soundcloud.com/jason-pecina/sober/s-qnnQiqEqev3 – Silvester Roseboom Jul 15 '21 at 01:42
  • @SilvesterRoseboom it seems like soundcloude is using the goo_gl api to do the shortening, i recommend to take a look at this post https://stackoverflow.com/questions/4201062/how-can-i-unshorten-a-url and if my answer helped please checkout https://stackoverflow.com/help/someone-answers – Themodmin Jul 15 '21 at 02:00