示例#1
0
        /// <summary>
        /// Main bot's 'heart'. Timer tick event
        /// </summary>
        private void Reader_Timer_Tick(Object s)
        {
            if (!IsConnectionAlive)
            {
                Connect();
            }

            // TODO: Bot should be initialized to online users as fast as possible
            // Current JOIN event is quite slow and can result into bot's insufficient permissions
            try
            {
                if (tcpClient.Available > 0 || reader.Peek() >= 0)
                {
                    string chatLine = reader.ReadLine();
                    ConsoleAppendText(chatLine);

                    // User sent a message
                    if (chatLine.Contains("PRIVMSG"))
                    {
                        // Get user, add him in OnlineUsers
                        User sender = GetUserFromChat(chatLine);

                        // Check if user has Display Name
                        CheckUserDisplayName(sender, chatLine);

                        sender.LastMessage = DateTime.Now;

                        // Parse chat message
                        string message = Parsing.ParseChatMessage(chatLine);
                        OnMessageReceived?.Invoke(this, new OnMessageArgs {
                            Message = message, Sender = sender
                        });

                        if (message.ContainsInsensitive("@" + _botDataManager.Login.BotFullTwitchName))
                        { // Bot is called by it's name, respond somehow
                            SendChatMessage(_botDataManager.BotDictionary.BotRespondMessages.SelectOneRandom());
                        }
                        else if (message.StartsWith("!"))
                        { // Someone is trying to call an command
                            RespondToCommand(sender, message);
                        }
                    }
                    else if (chatLine.Contains("JOIN"))
                    {
                        // User joined, add him in OnlineUsers, apply user's badges
                        GetUserFromChat(chatLine);
                    }
                    else if (chatLine.Contains("PART"))
                    {
                        // User disconnected
                        _usersManager.DisconnectUser(Parsing.ParseUsername(chatLine));
                    }
                    else if (chatLine.Contains("USERSTATE"))
                    {
                        // TODO: What this does?!
                        GetUserFromChat(chatLine);
                        var botUser = _usersManager.FindOnlineUser(_botDataManager.Login.BotName);
                        _botDataManager.UpdateBotPermissions(botUser);
                    }
                    else if (chatLine.Contains("PING :tmi.twitch.tv"))
                    {
                        // Request bot response on ping command, keeps connection alive
                        writer.WriteLine("PONG :tmi.twitch.tv");
                        writer.Flush();
                        ConsoleAppendText("PONG :tmi.twitch.tv");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Fatal error occured in bot timer mechanism!\n{ex}", ex);
            }
        }