giorgos empiros
giorgos empiros

Reputation: 1

How can I get the twitch stream preview in discord.js?

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

Upvotes: 0

Views: 1324

Answers (1)

JaeDeloper
JaeDeloper

Reputation: 46

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

Upvotes: 2

Related Questions