Esempio n. 1
0
        private static string _twitchOAuth     = "oauth:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // get chat bot's oauth from www.twitchapps.com/tmi/

        static void Main(string[] args)
        {
            // Initialize and connect to Twitch chat
            IrcClient irc = new IrcClient("irc.twitch.tv", 6667,
                                          _botName, _twitchOAuth, _broadcasterName);

            // Ping to the server to make sure this bot stays connected to the chat
            // Server will respond back to this bot with a PONG (without quotes):
            // Example: ":tmi.twitch.tv PONG tmi.twitch.tv :irc.twitch.tv"
            //PingSender ping;
            PingSender ping = new PingSender(irc);

            ping.Start();

            // Listen to the chat until program exits
            while (true)
            {
                // Read any message from the chat room
                string message = irc.ReadMessage();
                Console.WriteLine(message); // Print raw irc messages

                if (message.Contains("PRIVMSG"))
                {
                    // Messages from the users will look something like this (without quotes):
                    // Format: ":[user]![user]@[user].tmi.twitch.tv PRIVMSG #[channel] :[message]"

                    // Modify message to only retrieve user and message
                    int    intIndexParseSign = message.IndexOf('!');
                    string userName          = message.Substring(1, intIndexParseSign - 1); // parse username from specific section (without quotes)
                                                                                            // Format: ":[user]!"
                                                                                            // Get user's message
                    intIndexParseSign = message.IndexOf(" :");
                    message           = message.Substring(intIndexParseSign + 2);

                    //Console.WriteLine(message); // Print parsed irc message (debugging only)

                    // Broadcaster commands
                    if (userName.Equals(_broadcasterName))
                    {
                        if (message.Equals("!exitbot"))
                        {
                            irc.SendPublicChatMessage("Bye! Have a beautiful time!");
                            Environment.Exit(0); // Stop the program
                        }
                    }

                    // General commands anyone can use
                    if (message.Equals("!hello"))
                    {
                        irc.SendPublicChatMessage("Hello World!");
                    }
                }
            }
        }
Esempio n. 2
0
        public void Start()
        {
            IrcClient irc = new IrcClient("irc.twitch.tv", 6667,
                                          _nick, oauth, _channel);
            PingSender ping = new PingSender(irc);

            ping.Start();
            Debug.LogWarning("START CHAT BOT");
            // Listen to the chat until program exits
            while (true)
            {
                // Read any message from the chat room
                string message = irc.ReadMessage();
                Console.WriteLine(message); // Print raw irc messages

                if (message.Contains("PRIVMSG"))
                {
                    // Messages from the users will look something like this (without quotes):
                    // Format: ":[user]![user]@[user].tmi.twitch.tv PRIVMSG #[channel] :[message]"

                    // Modify message to only retrieve user and message
                    int    intIndexParseSign = message.IndexOf('!');
                    string userName          = message.Substring(1, intIndexParseSign - 1); // parse username from specific section (without quotes)
                                                                                            // Format: ":[user]!"
                                                                                            // Get user's message
                    intIndexParseSign = message.IndexOf(" :");
                    message           = message.Substring(intIndexParseSign + 2);

                    //Console.WriteLine(message); // Print parsed irc message (debugging only)

                    // Broadcaster commands
                    if (userName.Equals(_channel))
                    {
                        if (message.Equals("!exitbot"))
                        {
                            irc.SendPublicChatMessage("Bye! Have a beautiful time!");
                            Environment.Exit(0); // Stop the program
                        }
                    }

                    // General commands anyone can use
                    if (message.Equals("!hello"))
                    {
                        irc.SendPublicChatMessage("Hello World!");
                    }
                    if (message.Equals("!taxes"))
                    {
                        irc.SendPublicChatMessage("The Mitrochrondria is the powerhouse of the cell.");
                    }
                    if (message.Equals("!balance"))
                    {
                        var bal = DataService.Instance.GetBalance(userName);
                        irc.SendPublicChatMessage(string.Format("{0}'s Balance is {1}. ", userName, bal));
                    }
                    if (message.StartsWith("!bet"))
                    {
                        var msg = message.Split(' ');
                        if (!int.TryParse(msg[1], out int team))
                        {
                            irc.SendPublicChatMessage(string.Format("Could not parse  team of {0}", message));
                            continue;
                        }
                        if (!int.TryParse(msg[2], out int amount))
                        {
                            irc.SendPublicChatMessage(string.Format("Could not parse  amount of {0}", message));
                            continue;
                        }
                        var bal = DataService.Instance.GetBalance(userName);
                        if (bal < amount)
                        {
                            irc.SendPublicChatMessage(string.Format("Sorry {0} you don't have enough money, your balance is {1}. ", userName, bal));
                        }
                        BettingService.Instance.AddBetToTeam(userName, amount, team);
                    }
                }
            }
        }