SendRawMessage() 공개 메소드

Sends the specified raw message to the server.
The current instance has already been disposed. is .
public SendRawMessage ( string message ) : void
message string The text (single line) of the message to send the server.
리턴 void
예제 #1
0
        private static void HandleEventLoop(IrcDotNet.IrcClient client)
        {
            bool isExit = false;

            while (!isExit)
            {
                Console.Write("> ");
                var command = Console.ReadLine();
                switch (command)
                {
                case "exit":
                    isExit = true;
                    break;

                default:
                    if (!string.IsNullOrEmpty(command))
                    {
                        if (command.StartsWith("/") && command.Length > 1)
                        {
                            client.SendRawMessage(command.Substring(1));
                        }
                        else
                        {
                            Console.WriteLine("unknown command '{0}'", command);
                        }
                    }
                    break;
                }
            }
            client.Disconnect();
        }
예제 #2
0
파일: Program.cs 프로젝트: destinygg/bot
        private static void HandleEventLoop(IrcClient client)
        {
            _ircLocaluser = client.LocalUser;
              _hostNameToWebSockets.Add("", new WebSocketListenerClient(PrivateConstants.TestAccountWebsocketAuth));
              _hostNameToWebSockets[""].Run(sendToIrcProcessor);

              bool isExit = false;
              while (!isExit) {
            Console.Write("> ");
            var command = Console.ReadLine();
            switch (command) {
              case "exit":
            isExit = true;
            break;
              default:
            if (!string.IsNullOrEmpty(command)) {
              if (command.StartsWith("/") && command.Length > 1) {
                client.SendRawMessage(command.Substring(1));
              } else {
                Console.WriteLine($"Unknown command '{command}'");
              }
            }
            break;
            }
              }
              client.Disconnect();
        }
예제 #3
0
        public Bot()
        {
            commands = Command.GetCommands(this);
            IsInChannel = false;
            IsIdentified = false;
            IsRunning = true;

            // Initialize commands
            foreach(Command command in commands)
                command.Initialize();

            client = new IrcClient
            {
                FloodPreventer = new IrcStandardFloodPreventer(4, 2000)
            };

            // TODO Nasty...
            connectedEvent = new ManualResetEventSlim(false);
            client.Connected += (sender, e) => connectedEvent.Set();

            client.Connected += (sender, args) => Console.WriteLine("Connected!");
            client.Disconnected += (sender, args) =>
            {
                const int MaxRetries = 16;
                int tries = 0;

                if (!IsRunning)
                {
                    Console.WriteLine("Disconnected and IsRunning == false, assuming graceful shutdown...");
                    return;
                }

                IsInChannel = false;
                IsIdentified = false;

                // Wait a little so the server doesn't block us from rejoining (flood control)
                Console.WriteLine("Lost connection, attempting to reconnect in 6 seconds...");
                client.Disconnect();
                Thread.Sleep(6000);

                // Reconnect
                Console.Write("Reconnecting... ");
                while (!Connect(Configuration.Server) && tries++ < MaxRetries)
                {
                    Console.Write("\rReconnecting, attempt {0}...", tries);
                    Thread.Sleep(1);
                }

                if (tries == MaxRetries)
                {
                    Console.WriteLine("Failed.");
                    Quit("Failed to reconnect.");
                    return;
                }

                Console.WriteLine("Connected");
                Console.Write("Joining channel... ");

                if (JoinChannel(Configuration.Channel))
                    Console.WriteLine("Success");
                else
                {
                    Console.WriteLine("Failed");
                    Quit("Failed to rejoin channel.");
                }
            };

            client.Registered += (sender, args) =>
            {
                IrcClient localClient = (IrcClient)sender;

                // Identify with server
                client.SendRawMessage(String.Format("ns identify {0}", Configuration.Password));

                localClient.LocalUser.NoticeReceived += (o, eventArgs) =>
                {
                    if (eventArgs.Text.StartsWith("Password accepted"))
                        IsIdentified = true;

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(eventArgs.Text);
                    Console.ForegroundColor = ConsoleColor.Gray;
                };

                localClient.LocalUser.JoinedChannel += (o, eventArgs) =>
                {
                    IrcChannel channel = localClient.Channels.FirstOrDefault();
                    if (channel == null)
                        return;

                    Console.WriteLine("Joined channel!");

                    channel.MessageReceived += HandleMessage;
                    channel.UserJoined += (sender1, userEventArgs) =>
                    {
                        string joinMessage = String.Format("Used joined: {0}", userEventArgs.Comment ?? "No comment");

                        foreach (Command command in commands)
                            command.HandlePassive(joinMessage, userEventArgs.ChannelUser.User.NickName);
                    };

                    channel.UserLeft += (sender1, userEventArgs) =>
                    {
                        string leftMessage = String.Format("Used left: {0}", userEventArgs.Comment ?? "No comment");

                        foreach (Command command in commands)
                            command.HandlePassive(leftMessage, userEventArgs.ChannelUser.User.NickName);
                    };

                    IsInChannel = true;
                };

                localClient.LocalUser.LeftChannel += (o, eventArgs) =>
                {
                    Console.Write("Rejoining channel... ");
                    if (JoinChannel(Configuration.Channel))
                        Console.WriteLine("Success");
                    else
                    {
                        Console.WriteLine("Failed");
                        Quit("Failed to rejoin channel.");
                    }
                };

                Console.WriteLine("Registered!");
            };

            client.Error += (sender, args) =>
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("IRC Error {0}", args.Error.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            };
        }
예제 #4
0
        /// <summary>
        /// Connect to the given stream, returns true if we successfully connected.  Note
        /// that this function executes synchronously, and will block until fully connected
        /// to the IRC server.
        /// </summary>
        /// <param name="stream">The stream to connect to.</param>
        /// <param name="user">The twitch username this connection will use.</param>
        /// <param name="auth">The twitch API token used to log in.  This must begin with 'oauth:'.</param>
        public bool Connect(string stream, string user, string auth)
        {
            user = user.ToLower();
            m_stream = stream.ToLower();

            // Create client and hook up events.
            string server = "irc.twitch.tv";
            int port = 6667;

            WriteDiagnosticMessage("Attempting to connect to server...");

            m_client = new IrcClient();
            m_client.FloodPreventer = new IrcStandardFloodPreventer(4, 2000);

            m_client.Connected += client_Connected;
            m_client.ConnectFailed += client_ConnectFailed;
            m_client.Disconnected += client_Disconnected;
            m_client.Error += client_Error;
            m_client.Registered += client_Registered;
            m_client.ErrorMessageReceived += client_ErrorMessageReceived;

            // Connect to server.
            IPHostEntry hostEntry = Dns.GetHostEntry(server);
            m_client.Connect(new IPEndPoint(hostEntry.AddressList[0], port), false, new IrcUserRegistrationInfo()
            {
                NickName = user,
                UserName = user,
                RealName = user,
                Password = auth
            });

            // Wait for the server to connect.  The connect function on client operates asynchronously, so we
            // wait on s_connectedEvent which is set when client_Connected is called.
            if (!m_connectedEvent.Wait(10000))
            {
                WriteDiagnosticMessage("Connection to '{0}' timed out.", server);
                return false;
            }

            /// Wait for the client to be registered.
            if (!m_registeredEvent.Wait(10000))
            {
                WriteDiagnosticMessage("Registration timed out.", server);
                return false;
            }

            // Attempt to join the channel.  We'll try for roughly 10 seconds to join.  This really shouldn't ever fail.
            m_client.Channels.Join("#" + m_stream);
            int max = 40;
            while (m_client.Channels.Count == 0 && !m_joinedEvent.Wait(250))
            {
                max--;
                if (max < 0)
                {
                    WriteDiagnosticMessage("Failed to connect to {0}  Please press Reconnect.", m_stream);
                    return false;
                }
            }

            WriteDiagnosticMessage("Connected to channel {0}.", m_stream);

            // This command tells twitch that we are a chat bot capable of understanding subscriber/turbo/etc
            // messages.  Without sending this raw command, we would not get that data.
            m_client.SendRawMessage("TWITCHCLIENT 2");

            UpdateMods();
            return true;
        }
예제 #5
0
        /// <summary>
        /// Connect to the given stream, returns true if we successfully connected.  Note
        /// that this function executes synchronously, and will block until fully connected
        /// to the IRC server.
        /// </summary>
        /// <param name="stream">The stream to connect to.</param>
        /// <param name="user">The twitch username this connection will use.</param>
        /// <param name="auth">The twitch API token used to log in.  This must begin with 'oauth:'.</param>
        public ConnectResult Connect(string stream, string user, string auth, int timeout = 10000)
        {
            if (m_shutdown)
                throw new InvalidOperationException("Attempted to connect while disconnecting.");

            user = user.ToLower();
            m_stream = stream.ToLower();

            if (m_data == null)
                m_data = new TwitchUsers(m_stream);

            // Create client and hook up events.
            m_client = new IrcClient();

            m_client.Connected += client_Connected;
            m_client.UnsuccessfulLogin += m_client_UnsuccessfulLogin;
            m_client.ConnectFailed += client_ConnectFailed;
            m_client.Error += client_Error;
            m_client.Registered += client_Registered;
            m_client.ErrorMessageReceived += client_ErrorMessageReceived;
            m_client.PongReceived += m_client_PongReceived;
            m_client.PingReceived += m_client_PingReceived;

            m_flood = new FloodPreventer(this);
            m_flood.RejectedMessage += m_flood_RejectedMessage;
            m_client.FloodPreventer = m_flood;

            int currTimeout = timeout;
            DateTime started = DateTime.Now;

            m_connectedEvent.Reset();
            m_registeredEvent.Reset();
            m_joinedEvent.Reset();

            // Connect to server.
            m_client.Connect("irc.twitch.tv", 6667, false, new IrcUserRegistrationInfo()
            {
                NickName = user,
                UserName = user,
                RealName = user,
                Password = auth
            });

            // Wait for the server to connect.  The connect function on client operates asynchronously, so we
            // wait on s_connectedEvent which is set when client_Connected is called.
            if (!m_connectedEvent.Wait(currTimeout))
            {
                WriteDiagnosticMessage("Connecting to the Twitch IRC server timed out.");
                return ConnectResult.NetworkFailed;
            }

            currTimeout = timeout - (int)started.Elapsed().TotalMilliseconds;
            /// Wait for the client to be registered.
            if (!m_registeredEvent.Wait(currTimeout))
            {
                // Shouldn't really happen
                WriteDiagnosticMessage("Registration timed out.");
                return ConnectResult.Failed;
            }

            if (m_loginFailed)
                return ConnectResult.LoginFailed;

            // Attempt to join the channel.  We'll try for roughly 10 seconds to join.  This really shouldn't ever fail.
            m_client.Channels.Join("#" + m_stream);
            currTimeout = timeout - (int)started.Elapsed().TotalMilliseconds;
            if (!m_joinedEvent.Wait(currTimeout))
            {
                // Shouldn't really happen
                WriteDiagnosticMessage("Failed to join channel {0}.", m_stream);
                return ConnectResult.Failed;
            }

            TwitchSource.Log.Connected(stream);

            // This command tells twitch that we are a chat bot capable of understanding subscriber/turbo/etc
            // messages.  Without sending this raw command, we would not get that data.
            m_client.SendRawMessage("TWITCHCLIENT 3");

            UpdateMods();
            return ConnectResult.Success;
        }