示例#1
0
        /// <summary>
        /// IRC event: connection established.
        /// </summary>
        private void Client_Connected(object sender, EventArgs e)
        {
            TmiLog.Log("Connected to Twitch IRC server.");

            client.SendRawMessage("CAP REQ :twitch.tv/membership");
            client.SendRawMessage("CAP REQ :twitch.tv/commands");
            client.SendRawMessage("CAP REQ :twitch.tv/tags");
            client.SendRawMessage("CAP REQ :twitch.tv/tags twitch.tv/commands");
        }
示例#2
0
文件: IRC.cs 项目: Techial/TechBot
        public static void Init(string[] args)
        {
            if (args.Length < 2)
            {
                Log.Logger.OutputToConsole("Usage: twitchirc <username> <oauth>");
                Log.Logger.OutputToConsole(
                    "Use http://twitchapps.com/tmi/ to generate an <oauth> token!");
                return;
            }

            var server   = "irc.twitch.tv";
            var username = args[0];
            var password = args[1];

            Log.Logger.OutputToConsole("Starting to connect to twitch as {0}.",
                                       username);

            using (client = new TwitchIrcClient()) {
                client.FloodPreventer = new IrcStandardFloodPreventer(4, 2000);
                client.Disconnected  += IrcClient_Disconnected;
                client.Registered    += IrcClient_Registered;
                // Wait until connection has succeeded or timed out.
                using (var registeredEvent = new ManualResetEventSlim(false)) {
                    using (var connectedEvent = new ManualResetEventSlim(false)) {
                        client.Connected  += (sender2, e2) => connectedEvent.Set();
                        client.Registered += (sender2, e2) => registeredEvent.Set();
                        client.Connect(server, false,
                                       new IrcUserRegistrationInfo()
                        {
                            NickName = username.ToLower(), Password = password,
                            UserName = username
                        });
                        if (!connectedEvent.Wait(10000))
                        {
                            Log.Logger.OutputToConsole("Connection to '{0}' timed out.",
                                                       server);
                            return;
                        }
                    }
                    Console.Out.WriteLine("Now connected to '{0}'.", server);
                    if (!registeredEvent.Wait(10000))
                    {
                        Log.Logger.OutputToConsole("Could not register to '{0}'.", server);
                        return;
                    }
                }

                Console.Out.WriteLine("Now registered to '{0}' as '{1}'.", server,
                                      username);
                client.SendRawMessage(
                    "CAP REQ :twitch.tv/membership twitch.tv/commands twitch.tv/tags");
                HandleEventLoop(client);
            }
        }
示例#3
0
        private async void ParseMessage(BotType target, IrcRawMessageEventArgs e)
        {
            switch (e.Message.Command)
            {
            // messages to ignore
            case "002":     // twitch
            case "003":
            case "004":
            case "375":
            case "376":
            case "353":
            case "366":
            case "372":     // osu! motd
            case "QUIT":
                break;

            case "PRIVMSG":
#if DEBUG
                Console.WriteLine($"{target.ToString()}: {e.Message.Source.Name}: {e.Message.Parameters[1]}");
#endif

                if (target == BotType.Twitch)
                {
                    string   source  = e.Message.Source.Name;
                    string   message = e.Message.Parameters[1];
                    string[] request = message.Split(' ');

                    if (message.ToLowerInvariant().StartsWith("!request") || message.ToLowerInvariant().StartsWith("!req"))
                    {
                        if (!request[1].Contains("osu.ppy.sh/b/") && !request[1].Contains("osu.ppy.sh/s/"))
                        {
                            SendMessage(BotType.Twitch, $"@{source} that is an invalid beatmap.");
                            return;
                        }

                        string[] map = await Globals.OsuApiHelper.GetBeatmap(request[1]);

                        if (map[0] == "404")
                        {
                            SendMessage(BotType.Twitch, $"@{source} map not found.");
                        }

                        SendMessage(BotType.Osu, $"{source} requests {map[0]} to be played!");
                        SendMessage(BotType.Twitch, $"@{source} sent request for {map[1]} to be played");
                    }
                }
                break;

            case "PING":
            {
                switch (target)
                {
                case BotType.Osu:
                    m_osuClient.SendRawMessage("PONG");
                    break;

                case BotType.Twitch:
                    m_twitchClient.SendRawMessage("PONG");
                    break;
                }
            }
            break;

            case "JOIN":
                if (target == BotType.Twitch)
                {
                    CommandLog(target, $"Joined channel #{m_twitchChat}");
                }
                break;

            case "001":
                CommandLog(target, "Auth Success");

                if (target == BotType.Twitch)
                {
                    m_twitchClient.SendRawMessage($"JOIN #{m_twitchChat}\r\n");
                }

                break;

            case "464":
                CommandLog(target, "Auth Fail");
                break;

            default:
#if DEBUG
                CommandLog(target, $"{e.Message.Source.Name}: {e.Message.Command} - {String.Join(", ", e.Message.Parameters)}");
#endif
                break;
            }
        }
 static void SendMessageInChannel(TwitchIrcClient client, string channel, string message)
 {
     client.SendRawMessage($"privmsg #{channel} : {message}");
 }
 private static void JoinChannel(TwitchIrcClient client, string channel)
 {
     client.SendRawMessage($"join #{channel}");
 }
 public void SendMessage(string msg)
 {
     client.SendRawMessage(msg);
 }
示例#7
0
        public void StartBot()
        {
            string server = BotSettings.TwitchIRC;

            logger.Debug("Connecting to IRC...");
            Console.WriteLine("Connecting...");
            Console.WriteLine("");
            using (var client = new TwitchIrcClient())
            {
                client.FloodPreventer = new IrcStandardFloodPreventer(4, 2000);
                client.Registered    += IrcClient_Registered;
                // Wait until connection has succeeded or timed out.
                using (var registeredEvent = new ManualResetEventSlim(false))
                {
                    //Group chat - for whisper (not using)
                    //byte[]ip = {199,9,253,119};
                    //IPAddress p = new IPAddress(ip);
                    //IPEndPoint i = new IPEndPoint(p, 443);

                    using (var connectedEvent = new ManualResetEventSlim(false))
                    {
                        client.Connected  += (sender2, e2) => connectedEvent.Set();
                        client.Registered += (sender2, e2) => registeredEvent.Set();
                        client.Connect(server, false,
                                       new IrcUserRegistrationInfo()
                        {
                            NickName = BotSettings.UserName,
                            Password = BotSettings.OAuthChat,
                            UserName = BotSettings.UserName
                        });
                        if (!connectedEvent.Wait(3000))
                        {
                            isConnectedToIRC = false;
                            DisplayConnectionError(server);
                            OpenSettingsWindow();
                            Console.WriteLine();
                            Console.WriteLine();
                            Console.WriteLine("Press Enter to restart Bot and apply new settings..");
                            Console.WriteLine();
                            Console.ReadLine();
                            Restart = true;
                        }
                    }

                    if (!registeredEvent.Wait(3000))
                    {
                        if (isConnectedToIRC)
                        {
                            isConnectedToIRC = false;
                            DisplayConnectionError(server);
                            OpenSettingsWindow();
                            Console.WriteLine();
                            Console.WriteLine();
                            Console.WriteLine("Press Enter to restart Bot and apply new settings.");
                            Console.WriteLine();
                            Console.ReadLine();
                            Restart = true;
                        }
                    }
                }

                if (isConnectedToIRC)
                {
                    logger.Debug("Connected, about to join channel.");
                    twitchAPI = new TwitchAPI(BotSettings.BotOAuth, BotSettings.BotClientID);
                    client.SendRawMessage("CAP REQ :twitch.tv/membership");  //request to have Twitch IRC send join/part & modes.
                    client.Join(MAINCHANNEL);
                    HandleEventLoop(client);
                }
            }
        }