0

Currently I have this code to notify my discord members that someone is streaming. When I set the image to twitch url it just doesn't load it for some reason... How can I get the proper twitch stream preview?

client.on("presenceUpdate", (oldPresence, newPresence) => {
    if (!newPresence.activities) return false;
    newPresence.activities.forEach(activity => {
        if (activity.type == "STREAMING") {
            console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
            const twitchAnnouncementChannel = newPresence.guild.channels.cache.find(ch => ch.id === `789277245617209354`)
            const twitchChannel = new Discord.MessageEmbed()
            .setColor("#400080")
            .setTitle(`${newPresence.user.tag} is now live on twitch`)
            .setURL(activity.url)
            .setDescription(`**Stream Started**`)
            .setImage(activity.url)
            .setTimestamp()
            .setFooter("Enigma")
            twitchAnnouncementChannel.send(`${newPresence.user.tag} IS NOW LIVE ON TWITCH GO CHECK HIM OUT! @everyone`, twitchChannel)
        };
    });
});
  • Well the twitch url is just a link to a stream, it isn't an image link. You'll need to use the twitch API itself to fetch the preview image. You can try using [this answer](https://stackoverflow.com/questions/46722459/how-to-get-twitch-video-thumbnail-url) to do so (it's from 2018, so it might be a bit different from how it is now, but overall it should be the same basic process). – Cannicide Dec 21 '20 at 17:16

1 Answers1

2

In your case, this should do it. To get the preview of a Twitch stream you need: https://static-cdn.jtvnw.net/previews-ttv/live_user_USERNAME-{width}x{height}.jpg, "activity.url" is only the channel url.

client.on("presenceUpdate", (oldPresence, newPresence) => {
    if (!newPresence.activities) return false;
    newPresence.activities.forEach(activity => {
        if (activity.type == "STREAMING") {
            console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
            const twitchAnnouncementChannel = newPresence.guild.channels.cache.find(ch => ch.id === `789277245617209354`)
            const twitchChannel = new Discord.MessageEmbed()
                .setColor("#400080")
                .setTitle(`${newPresence.user.tag} is now live on twitch`)
                .setURL(activity.url)
                .setDescription(`**Stream Started**`)
                .setImage(`https://static-cdn.jtvnw.net/previews-ttv/live_user_${activity.url.split("twitch.tv/").pop()}-1920x1080.jpg`)
                .setTimestamp()
                .setFooter("Enigma");
            twitchAnnouncementChannel.send(`${newPresence.user.tag} IS NOW LIVE ON TWITCH GO CHECK HIM OUT! @everyone`, twitchChannel)
        };
    });
});
JaeDeloper
  • 46
  • 4