Twitch livestream preview in discord.js

`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 => === `789277245617209354`)
        const twitchChannel = new Discord.MessageEmbed()
        .setTitle(`${newPresence.user.tag} is now live on twitch`)
        .setDescription(`**Stream Started**`)
        twitchAnnouncementChannel.send(`${newPresence.user.tag} IS NOW LIVE ON TWITCH GO CHECK HIM OUT! @everyone`, twitchChannel)

I have this code to notify my server that someone is live streaming. When I set the image to the stream url it just doesn’t load it for some reason. Is there a diferent way to get the image of the livestream?

I’m not sure what Discord library you’re using, but I would imagine that .setImage is expecting an image, not their Twitch channel page URL. If you want to manually specify an image of their stream then you should use the Get Streams endpoint and use the provided thumbnail.

Also to note, creating streaming announcements based on presence changes on Discord is not a reliable way to know if a channel is live or not, as it’s possible to show as ‘streaming’ on Discord even when the channel isn’t live.

@Dist is there a way to import twitch api into my code ?

The API endpoints for what you’d need are all HTTP requests, so you’d need to use a HTTP library first to get an OAuth token, and with that authorizaiton token you can then make requests to the Get Streams endpoint that I previously linked.

For this use case you’d be better off using EventSub or Webhooks to be notified when a stream goes live, then reformatting the data into a Discord Webhook compatible payload.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.