コード例 #1
0
        /// <summary>
        /// Monitor chat box for commands
        /// </summary>
        private async Task GetChatBoxAsync()
        {
            try
            {
                /* Master loop */
                while (true)
                {
                    // Read any message inside the chat room
                    string rawMessage = await _irc.ReadMessageAsync();

                    Console.WriteLine(rawMessage); // Print raw irc message

                    if (!string.IsNullOrEmpty(rawMessage))
                    {
                        /*
                         * Get user name and message from chat
                         * and check if user has access to certain functions
                         */
                        if (rawMessage.Contains("PRIVMSG"))
                        {
                            // Modify message to only show user and message
                            // Reference: https://dev.twitch.tv/docs/irc/tags/#privmsg-twitch-tags
                            int    indexParseSign  = rawMessage.IndexOf(" :");
                            string modifiedMessage = rawMessage.Remove(0, indexParseSign + 2);

                            indexParseSign = modifiedMessage.IndexOf('!');
                            string username = modifiedMessage.Substring(0, indexParseSign);

                            indexParseSign = modifiedMessage.IndexOf(" :");
                            string message = modifiedMessage.Substring(indexParseSign + 2);

                            TwitchChatter chatter = new TwitchChatter
                            {
                                Username    = username,
                                Message     = message,
                                DisplayName = PrivMsgParameterValue(rawMessage, "display-name"),
                                Badges      = PrivMsgParameterValue(rawMessage, "badges"),
                                TwitchId    = PrivMsgParameterValue(rawMessage, "user-id"),
                                MessageId   = PrivMsgParameterValue(rawMessage, "id")
                            };

                            message = message.ToLower(); // make commands case-insensitive

                            try
                            {
                                // Purge any clips that aren't from the broadcaster that a viewer posts
                                if (_botConfig.Broadcaster.ToLower() != chatter.Username &&
                                    !chatter.Badges.Contains("moderator") &&
                                    !await IsAllowedChatMessageAsync(chatter))
                                {
                                    _irc.ClearMessage(chatter);
                                    _irc.SendPublicChatMessage($"Please refrain from posting a message that isn't for this channel @{chatter.DisplayName}");
                                    continue;
                                }

                                await GreetUserAsync(chatter);

                                await _commandSystem.ExecRequest(chatter);

                                FindCustomCommand(chatter);
                            }
                            catch (Exception ex)
                            {
                                await _errHndlrInstance.LogError(ex, "TwitchBotApplication", "GetChatBox()", false, "N/A", chatter.Message);
                            }
                        }
                        else if (rawMessage.Contains("NOTICE"))
                        {
                            if (rawMessage.Contains("Error logging in"))
                            {
                                Console.WriteLine("\n------------> URGENT <------------");
                                Console.WriteLine("Please check your credentials and try again.");
                                Console.WriteLine("If this error persists, please check if you can access your channel's chat.");
                                Console.WriteLine("If not, then contact Twitch support.");
                                Console.WriteLine("Exiting bot application now...");
                                Thread.Sleep(7500);
                                Environment.Exit(0);
                            }
                        }
                    }
                } // end master while loop
            }
            catch (Exception ex)
            {
                await _errHndlrInstance.LogError(ex, "TwitchBotApplication", "GetChatBox()", true);
            }
        }