예제 #1
0
        public static void InitFollowers()
        {
            List <ChannelFollow> followers = TwitchApi.FetchFollowers(Settings.Default.TwitchUsername).Result;

            bool fileExists = File.Exists(StreamFollowersPath);

            if (!fileExists)
            {
                File.WriteAllText(StreamFollowersPath, JsonConvert.SerializeObject(followers, Formatting.Indented));
                Console.WriteLine($"You have {followers.Count} Followers!");

                StreamFollowers = followers;
                return;
            }

            using (StreamReader sr = new StreamReader(StreamFollowersPath))
            {
                string contents = sr.ReadToEnd();

                sr.Dispose();
                List <ChannelFollow> fileFollowers = JsonConvert.DeserializeObject <List <ChannelFollow> >(contents);

                int followerGain = 0;
                int followerLost = 0;

                foreach (ChannelFollow channelFollow in followers)
                {
                    ChannelFollow fileFollower =
                        fileFollowers.FirstOrDefault(x => x.User.Name == channelFollow.User.Name);

                    if (fileFollower == null)
                    {
                        Console.WriteLine($"New Follower: {channelFollow.User.DisplayName}");
                        followerGain++;
                    }
                }

                foreach (ChannelFollow fileFollower in fileFollowers)
                {
                    ChannelFollow follower = followers.FirstOrDefault(x => x.User.Name == fileFollower.User.Name);

                    if (follower == null)
                    {
                        Console.WriteLine($"Lost Follower: {fileFollower.User.DisplayName}");
                        followerLost++;
                    }
                }

                Console.WriteLine($"You have gained {followerGain} and lost {followerLost} followers!");

                StreamFollowers = followers;

                File.WriteAllText(StreamFollowersPath, JsonConvert.SerializeObject(StreamFollowers, Formatting.Indented));
            }
        }
예제 #2
0
        public static async Task UpdateFollowers()
        {
            string username = Settings.Default.TwitchUsername;
            List <ChannelFollow> channelFollows = await TwitchApi.FetchFollowers(username);

            int difference = channelFollows.Count - StreamFollowers.Count;

            Debug.WriteIf(difference != 0, $"Difference: {difference}");

            if (difference > 0)
            {
                // New Follower
                foreach (ChannelFollow channelFollow in channelFollows)
                {
                    ChannelFollow currentFollower =
                        StreamFollowers.FirstOrDefault(x => x.User.Name == channelFollow.User.Name);

                    if (currentFollower == null)
                    {
                        // Not in list
                        Console.WriteLine($"New Follower: {channelFollow.User.DisplayName}");
                        TwitchBot.SendMessage(username, $"Thanks for the follow {channelFollow.User.DisplayName}!");
                    }
                }
            }

            if (difference < 0)
            {
                // Lost Follower
                foreach (ChannelFollow streamFollower in StreamFollowers)
                {
                    ChannelFollow follower =
                        channelFollows.FirstOrDefault(x => x.User.Name == streamFollower.User.Name);

                    if (follower == null)
                    {
                        // No longer in channel
                        Console.WriteLine($"Lost Follower: {streamFollower.User.DisplayName}");
                    }
                }
            }

            if (difference != 0)
            {
                StreamFollowers = channelFollows;

                File.WriteAllText(StreamFollowersPath, JsonConvert.SerializeObject(StreamFollowers, Formatting.Indented));
            }
        }
예제 #3
0
        private void Client_OnUserJoined(object sender, TwitchLib.Client.Events.OnUserJoinedArgs e)
        {
            if (e.Username != client.TwitchUsername)
            {
                Console.WriteLine($"{e.Username} has connected to {e.Channel}");
                if (e.Channel.ToLower() == Settings.Default.TwitchUsername)
                {
                    User user = TwitchApi.FetchUser(e.Username).Result;

                    if (user == null)
                    {
                        return;
                    }

                    Debug.WriteLine($"User {user.DisplayName} type: {user.Type}.");

                    //client.SendMessage(e.Channel, $"Welcome {e.Username}!");
                }
            }
        }
예제 #4
0
        public TwitchBot()
        {
            bool connected = false;

            Console.WriteLine($"Connecting to Twitch..");

            ConnectionCredentials credentials = new ConnectionCredentials(Settings.Default.TwitchUsername, Settings.Default.TwitchClientToken);

            ClientOptions clientOptions = new ClientOptions
            {
                MessagesAllowedInPeriod = 750,
                ThrottlingPeriod        = TimeSpan.FromSeconds(30)
            };

            WebSocketClient customClient = new WebSocketClient(clientOptions);

            client = new TwitchClient(customClient);
            client.Initialize(credentials, Settings.Default.TwitchUsername);

            client.OnLog             += Client_OnLog;
            client.OnConnected       += Client_OnConnected;
            client.OnJoinedChannel   += Client_OnJoinedChannel;
            client.OnUserJoined      += Client_OnUserJoined;
            client.OnBeingHosted     += Client_OnBeingHosted;
            client.OnMessageReceived += Client_OnMessageReceived;
            client.OnWhisperReceived += Client_OnWhisperReceived;
            client.OnNewSubscriber   += Client_OnNewSubscriber;

            client.Connect();

            Timer timer = new Timer(5)
            {
                AutoReset = true
            };

            timer.Start();

            timer.Elapsed += (sender, args) =>
            {
                Console.WriteLine("..");
            };

            while (!connected)
            {
                connected = client.IsConnected;
            }

            timer.Dispose();

            Console.WriteLine($"Connected to Twitch.");

            timer = new Timer(1000)
            {
                AutoReset = false
            };
            timer.Start();
            timer.Elapsed += (sender, args) =>
            {
                timer.Dispose();
#if RELEASE
                JoinChannels();
#endif
            };

            TwitchApi api = new TwitchApi();

            TwitchApi.StartApi();

            while (!Program.QuitFlag)
            {
                string consoleInput = Console.ReadLine();

                if (string.IsNullOrEmpty(consoleInput))
                {
                    continue;
                }

                if (consoleInput.StartsWith('!'))
                {
                    string[] commandSplit = consoleInput.Split(' ');

                    if (commandSplit[0].ToLower() == "!help" || commandSplit[0].ToLower() == "!commands")
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("");
                        Console.WriteLine($"--- Unsociable's Bot Help ---");
                        Console.WriteLine("!say [Message] - Says a message in your chat!");
                        Console.WriteLine($"!addnotification [StreamerName] - Adds a streamer to your system when they start streaming");
                        Console.WriteLine("!removenotification [StreamerName] - Removes a streamer from your system");
                        Console.WriteLine("");
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                    }

                    if (commandSplit[0].ToLower() == "!say")
                    {
                        if (commandSplit.Length < 2)
                        {
                            Console.WriteLine($"You need to input a message!");
                            continue;
                        }

                        string joined = string.Join(' ', commandSplit.Skip(1));

                        client.SendMessage(Settings.Default.TwitchUsername, joined);

                        Console.WriteLine($"Message sent to {Settings.Default.TwitchUsername} channel: {joined}");
                    }

                    if (commandSplit[0].ToLower() == "!addnotification")
                    {
                        if (commandSplit.Length < 2)
                        {
                            Console.WriteLine($"You need to input a streamers channel name!");
                            continue;
                        }

                        string joined = string.Join(' ', commandSplit.Skip(1));

                        if (TwitchApi.StreamNotifications.Contains(joined.ToLower()))
                        {
                            Console.WriteLine($"You already have {joined} in your notifications!");
                            continue;
                        }

                        TwitchApi.StreamNotifications.Add(joined.ToLower());
                        TwitchApi.SaveStreamNotifications();
                        Console.WriteLine($"You've added {joined} to your notifications!");
                    }

                    if (commandSplit[0].ToLower() == "!removenotification")
                    {
                        if (commandSplit.Length < 2)
                        {
                            Console.WriteLine($"You need to input a streamers channel name!");
                            continue;
                        }

                        string joined = string.Join(' ', commandSplit.Skip(1));

                        if (!TwitchApi.StreamNotifications.Contains(joined.ToLower()))
                        {
                            Console.WriteLine($"You don't have {joined} in your notifications!");
                            continue;
                        }

                        TwitchApi.StreamNotifications.Remove(joined.ToLower());
                        TwitchApi.SaveStreamNotifications();
                        Console.WriteLine($"You've removed {joined} from your notifications!");
                    }
                }

                if (consoleInput.ToLower() == "quit")
                {
                    Program.QuitFlag = true;
                }
            }
        }