예제 #1
0
        /// <summary>
        /// Check broadcaster's info via API for newest followers or subscribers
        /// </summary>
        /// <returns></returns>
        private async Task CheckNewFollowersSubscribers()
        {
            try
            {
                // Get broadcaster type and check if they can have subscribers
                ChannelJSON channelJson = await _twitchInfo.GetBroadcasterChannelById();

                string broadcasterType = channelJson.BroadcasterType;

                if (broadcasterType.Equals("partner") || broadcasterType.Equals("affiliate"))
                {
                    // Check for new subscribers
                    RootSubscriptionJSON rootSubscriptionJson = await _twitchInfo.GetSubscribersByChannel();

                    IEnumerable <string> freshSubscribers = rootSubscriptionJson.Subscriptions
                                                            ?.Where(u => Convert.ToDateTime(u.CreatedAt).ToLocalTime() > DateTime.Now.AddSeconds(-60))
                                                            .Select(u => u.User.Name);

                    if (freshSubscribers?.Count() > 0)
                    {
                        string subscriberUsername     = "";
                        string subscriberRoleplayName = "";

                        if (freshSubscribers.Count() == 1)
                        {
                            subscriberUsername     = $"@{freshSubscribers.First()}";
                            subscriberRoleplayName = "a NaCl agent";
                        }
                        else if (freshSubscribers.Count() > 1)
                        {
                            subscriberRoleplayName = "NaCl agents";

                            foreach (string subscriber in freshSubscribers)
                            {
                                subscriberUsername += $"{subscriber}, ";
                            }

                            subscriberUsername = subscriberUsername.ReplaceLastOccurrence(", ", "");    // replace trailing ","
                            subscriberUsername = subscriberUsername.ReplaceLastOccurrence(", ", " & "); // replacing joining ","
                        }

                        string welcomeMessage = $"Thank you so much {subscriberUsername} on becoming {subscriberRoleplayName}! "
                                                + $"That dedication will give @{_botConfig.Broadcaster} the edge they need to lead the charge "
                                                + "into the seven salty seas of Twitch! SwiftRage";

                        _irc.SendPublicChatMessage(welcomeMessage);
                    }
                }

                // Check for new followers
                RootFollowerJSON rootFollowerJson = await _twitchInfo.GetFollowersByChannel();

                IEnumerable <string> freshFollowers = rootFollowerJson.Followers
                                                      ?.Where(u => Convert.ToDateTime(u.CreatedAt).ToLocalTime() > DateTime.Now.AddSeconds(-60))
                                                      .Select(u => u.User.Name);

                if (freshFollowers?.Count() > 0)
                {
                    string followerUsername = "";

                    if (freshFollowers.Count() == 1)
                    {
                        followerUsername = $"@{freshFollowers.First()}";
                    }
                    else if (freshFollowers.Count() > 1)
                    {
                        foreach (string follower in freshFollowers)
                        {
                            followerUsername += $"{follower}, ";
                        }

                        followerUsername = followerUsername.ReplaceLastOccurrence(", ", "");    // replace trailing ","
                        followerUsername = followerUsername.ReplaceLastOccurrence(", ", " & "); // replacing joining ","
                    }

                    _irc.SendPublicChatMessage($"Welcome {followerUsername} to the Salt Army! "
                                               + $"With @{_botConfig.Broadcaster} we will pillage the seven salty seas of Twitch together!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error inside FollowerSubscriberListener.CheckNewFollowersSubscribers(): " + ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
                }
            }
        }
        /// <summary>
        /// Check broadcaster's info via API for newest followers or subscribers
        /// </summary>
        /// <returns></returns>
        private async Task CheckNewFollowersSubscribersAsync()
        {
            try
            {
                // Get broadcaster type and check if they can have subscribers
                ChannelJSON channelJson = await _twitchInfo.GetBroadcasterChannelByIdAsync();

                string broadcasterType = channelJson.BroadcasterType;

                if (broadcasterType == "partner" || broadcasterType == "affiliate")
                {
                    /* Check for new subscribers */
                    RootSubscriptionJSON rootSubscriptionJson = await _twitchInfo.GetSubscribersByChannelAsync();

                    IEnumerable <string> freshSubscribers = rootSubscriptionJson.Subscriptions
                                                            ?.Where(u => Convert.ToDateTime(u.CreatedAt).ToLocalTime() > DateTime.Now.AddSeconds(-60))
                                                            .Select(u => u.User.Name);

                    if (freshSubscribers?.Count() > 0)
                    {
                        string subscriberUsername     = "";
                        string subscriberRoleplayName = "";

                        if (freshSubscribers.Count() == 1)
                        {
                            subscriberUsername     = $"@{freshSubscribers.First()}";
                            subscriberRoleplayName = "a NaCl agent";
                        }
                        else if (freshSubscribers.Count() > 1)
                        {
                            subscriberRoleplayName = "NaCl agents";

                            foreach (string subscriber in freshSubscribers)
                            {
                                subscriberUsername += $"{subscriber}, ";
                            }

                            subscriberUsername = subscriberUsername.ReplaceLastOccurrence(", ", "");    // replace trailing ","
                            subscriberUsername = subscriberUsername.ReplaceLastOccurrence(", ", " & "); // replace last ","
                        }

                        string welcomeMessage = $"Congrats {subscriberUsername} on becoming @{_botConfig.Broadcaster} 's {subscriberRoleplayName}!";

                        // Break up welcome message if it's too big
                        if (welcomeMessage.Count() > 500)
                        {
                            string[]      separators  = new string[] { ", ", " & " };
                            List <string> subscribers = subscriberUsername.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToList();

                            while (subscribers.Count > 0)
                            {
                                int popCount = 14;

                                if (subscribers.Count < popCount)
                                {
                                    popCount = subscribers.Count;
                                }

                                subscriberUsername = string.Join(", ", subscribers.Take(popCount));
                                subscriberUsername = subscriberUsername.ReplaceLastOccurrence(", ", "");    // replace trailing ","
                                subscriberUsername = subscriberUsername.ReplaceLastOccurrence(", ", " & "); // replace last ","

                                subscribers.RemoveRange(0, popCount);

                                _irc.SendPublicChatMessage($"Congrats {subscriberUsername} on becoming @{_botConfig.Broadcaster} 's {subscriberRoleplayName}!");
                                await Task.Delay(750);
                            }
                        }
                        else
                        {
                            _irc.SendPublicChatMessage(welcomeMessage);
                        }
                    }
                }

                /* Check for new followers */
                RootFollowerJSON rootFollowerJson = await _twitchInfo.GetFollowersByChannelAsync();

                IEnumerable <string> freshFollowers = rootFollowerJson.Followers
                                                      ?.Where(u => Convert.ToDateTime(u.CreatedAt).ToLocalTime() > DateTime.Now.AddSeconds(-60))
                                                      .Select(u => u.User.Name);

                if (freshFollowers?.Count() > 0)
                {
                    string followerUsername = "";

                    if (freshFollowers.Count() == 1)
                    {
                        followerUsername = $"@{freshFollowers.First()}";
                    }
                    else if (freshFollowers.Count() > 1)
                    {
                        foreach (string follower in freshFollowers)
                        {
                            followerUsername += $"{follower}, ";
                        }

                        followerUsername = followerUsername.ReplaceLastOccurrence(", ", "");    // replace trailing ","
                        followerUsername = followerUsername.ReplaceLastOccurrence(", ", " & "); // replacing joining ","
                    }

                    _irc.SendPublicChatMessage($"Welcome {followerUsername} to the Salt Army! "
                                               + $"With @{_botConfig.Broadcaster} we will pillage the seven salty seas of Twitch together!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error inside FollowerSubscriberListener.CheckNewFollowersSubscribers(): " + ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
                }
            }
        }