Example #1
0
        /// <summary>
        /// Creates an Instance of the TwitchAPI Class.
        /// </summary>
        /// <param name="clientId">Twitch Client Id.</param>
        /// <param name="accessToken">Twitch Access Token.</param>
        /// <param name="rateLimit">Should RateLimit Requests?</param>
        /// <param name="rateLimiter">Instance Of RateLimiter. Useful if using multiple API instances on one connection and you wish to share the requests ratelimiter.</param>
        /// <param name="callsPerPeriod">Number of Requests per Period to rate limit to</param>
        /// <param name="ratePeriod">Period for Rate Limit (In Seconds)</param>
        public TwitchAPI(string clientId = null, string accessToken = null, bool rateLimit = true, IRateLimiter rateLimiter = null, int callsPerPeriod = 1, int ratePeriod = 1)
        {
            _rateLimiter = rateLimit ?
                           (rateLimiter ?? TimeLimiter.GetFromMaxCountByInterval(callsPerPeriod, TimeSpan.FromSeconds(ratePeriod)))
                : BypassLimiter.CreateLimiterBypassInstance();

            Auth            = new Auth(this);
            Blocks          = new Blocks(this);
            Badges          = new Badges(this);
            Bits            = new Bits(this);
            ChannelFeeds    = new ChannelFeeds(this);
            Channels        = new Channels(this);
            Chat            = new Chat(this);
            Clips           = new Clips(this);
            Collections     = new Collections(this);
            Communities     = new Communities(this);
            Follows         = new Follows(this);
            Games           = new Games(this);
            Ingests         = new Ingests(this);
            Root            = new Root(this);
            Search          = new Search(this);
            Streams         = new Streams(this);
            Subscriptions   = new Subscriptions(this);
            Teams           = new Teams(this);
            ThirdParty      = new ThirdParty(this);
            Undocumented    = new Undocumented(this);
            Users           = new Users(this);
            Videos          = new Videos(this);
            Webhooks        = new Webhooks(this);
            Debugging       = new Debugging();
            Settings        = new ApiSettings(this);
            _jsonSerializer = new TwitchLibJsonSerializer();

            if (!string.IsNullOrWhiteSpace(clientId))
            {
                Settings.ClientId = clientId;
            }
            if (!string.IsNullOrWhiteSpace(accessToken))
            {
                Settings.AccessToken = accessToken;
            }
        }
Example #2
0
        /// <summary>
        /// Constructor for UserState.
        /// </summary>
        /// <param name="ircString"></param>
        public UserState(string ircString)
        {
            foreach (string part in ircString.Split(';'))
            {
                // The 'user-type' section does not have a ; suffix, we will account for this outside of for loop, we should exit loop immediately
                if (part.Contains(" :tmi.twitch.tv USERSTATE "))
                {
                    break;
                }
                if (!part.Contains("="))
                {
                    // This should never happen, unless Twitch changes their shit.
                    Console.WriteLine($"Unaccounted for [UserState]: {part}");
                    continue;
                }
                switch (part.Split('=')[0])
                {
                case "@badges":
                    string badges = part.Split('=')[1];
                    if (badges.Contains('/'))
                    {
                        if (!badges.Contains(","))
                        {
                            Badges.Add(new KeyValuePair <string, string>(badges.Split('/')[0], badges.Split('/')[1]));
                        }
                        else
                        {
                            foreach (string badge in badges.Split(','))
                            {
                                Badges.Add(new KeyValuePair <string, string>(badge.Split('/')[0], badge.Split('/')[1]));
                            }
                        }
                    }
                    break;

                case "color":
                    ColorHex = part.Split('=')[1];
                    break;

                case "display-name":
                    DisplayName = part.Split('=')[1];
                    break;

                case "emote-sets":
                    EmoteSet = part.Split('=')[1];
                    break;

                case "mod":
                    Moderator = part.Split('=')[1] == "1";
                    break;

                case "subscriber":
                    Subscriber = part.Split('=')[1] == "1";
                    break;

                default:
                    // This should never happen, unless Twitch changes their shit
                    Console.WriteLine($"Unaccounted for [UserState]: {part.Split('=')[0]}");
                    break;
                }
            }
            // Lets deal with that user-type
            switch (ircString.Split('=')[6].Split(' ')[0])
            {
            case "mod":
                UserType = Common.UserType.Moderator;
                break;

            case "global_mod":
                UserType = Common.UserType.GlobalModerator;
                break;

            case "admin":
                UserType = Common.UserType.Admin;
                break;

            case "staff":
                UserType = Common.UserType.Staff;
                break;

            default:
                UserType = Common.UserType.Viewer;
                break;
            }
            Channel = ircString.Split(' ')[3].Replace("#", "");
            if (DisplayName.ToLower() == Channel.ToLower())
            {
                UserType = Common.UserType.Broadcaster;
            }
        }