Get Badges's URLs for user

This code can be used to get 3 arguments : %badge_1% - %badge_2% - %badge_3%

From every triggers that return the badges variable : ( e.g. : TwitchFirstWord TwitchChatMessage )

badges System.Collections.Generic.List`1[Twitch.Common.Models.Badge]

You can then use them in a OBS Set Browser Source subaction

if there is only 2 badges , the variables return “about:blank” which remain the browser source to be empty

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class CPHInline
{
    public bool Execute()
    {
        string badge_1 = "about:blank";
        CPH.SetArgument("badge_1", badge_1);
        string badge_2 = "about:blank";
        CPH.SetArgument("badge_2", badge_2);
        string badge_3 = "about:blank";
        CPH.SetArgument("badge_3", badge_3);
        var badgesObj = args["badges"];
        string json = JsonConvert.SerializeObject(badgesObj);
        CPH.LogDebug(json);
        List<Badge> badges = JsonConvert.DeserializeObject<List<Badge>>(json);
        int numberOfBadges = badges.Count;
        switch (numberOfBadges)
        {
            case 3:
                badge_1 = badges[0].imageUrl;
                CPH.SetArgument("badge_1", badge_1);
                badge_2 = badges[1].imageUrl;
                CPH.SetArgument("badge_2", badge_2);
                badge_3 = badges[2].imageUrl;
                CPH.SetArgument("badge_3", badge_3);
                break;
            //
            case 2:
                badge_1 = badges[0].imageUrl;
                CPH.SetArgument("badge_1", badge_1);
                badge_2 = badges[1].imageUrl;
                CPH.SetArgument("badge_2", badge_2);
                break;
            //
            case 1:
                badge_1 = badges[0].imageUrl;
                CPH.SetArgument("badge_1", badge_1);
                break;
            //
            case 0:
                break;
        }

        return true;
    }
}

public class Badge
{
    public string name { get; set; }
    public string version { get; set; }
    public string imageUrl { get; set; }
}
1 Like