Ejemplo n.º 1
0
        public static StreamO GetStreamByUser(string channelId)
        {
            StreamO stream = new StreamO();

            string channelOauth = ConfigurationManager.AppSettings["channelOauth"];
            string helixChannelSubscriptionToken = ConfigurationManager.AppSettings["helixChannelSubscriptionToken"];
            string url = "https://api.twitch.tv/kraken/streams/" + channelId;

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                if (webRequest != null)
                {
                    webRequest.Method  = "GET";
                    webRequest.Timeout = 12000;
                    webRequest.Headers.Add("Client-ID", channelOauth);
                    webRequest.ContentType = "application/json";
                    webRequest.Accept      = "application/vnd.twitchtv.v5+json";
                    webRequest.Headers.Add("Authorization: " + helixChannelSubscriptionToken);
                }

                using (Stream s = webRequest.GetResponse().GetResponseStream())
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                    {
                        var jsonResponse = sr.ReadToEnd();
                        stream = JsonConvert.DeserializeObject <StreamO>(jsonResponse);
                    }
                }
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder(DateTime.Now.ToString("dd-MM-yyyy") + " : " + ex.Message);
                Console.WriteLine(sb);
            }


            return(stream);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            #region LoadChannel
            _channel   = ChannelD.GetChannel();
            _channelId = _channel._id;
            #endregion

            #region LoadStream
            _stream = StreamD.GetStreamByUser(_channelId);
            if (_stream.stream == null)
            {
                _streamUptime = new DateTime();
            }
            else
            {
                _streamUptime = _stream.stream.created_at.ToLocalTime();
            }

            #endregion

            _irc = new IrcClient("irc.twitch.tv", 6667, ChatBot.botName, _password, _channel.name);
            TwitchApi  api      = new TwitchApi();
            TmiApi     tmi      = new TmiApi();
            ViewerList chatters = new ViewerList();
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
            TwitchSocket twitchSocket = new TwitchSocket();
            ChatBot.init();

            #region LoadBroadCasterInfo
            _broadcaster = UserD.GetUser(_broadcasterName);
            #endregion

            _moderators.Add("gaelLevel");
            _moderators.Add("terror_seeds");
            _moderators.Add("nebulea");
            _moderators.Add("novaevermoon");

            #region LoadCommands
            List <CommandO> commands = new List <CommandO>();
            commands = CommandD.LoadCommands();
            LaunchTimer launchTimer = new LaunchTimer(_irc);
            foreach (CommandO command in commands)
            {
                if (command.type == "timed")
                {
                    launchTimer.createTimer(command);
                }
            }


            if (commands.Count != 0)
            {
                _commandsText = new StringBuilder();
                foreach (CommandO command in commands)
                {
                    if (command.userLevel == "everyone" && command.timer == 0)
                    {
                        _commandsText.Append("!" + command.keyword + ", ");
                    }
                }
                _commandsText.Length = _commandsText.Length - 2;
            }


            #endregion

            //chatters = tmi.getViewerList(_channel);

            //_viewers = tmi.getViewers(chatters);
            twitchSocket.ClientWebSocket = twitchSocket.WebSocketConnectAsync().Result;
            var testSocket = twitchSocket.WhisperSubscribeAsync(twitchSocket.ClientWebSocket, _broadcaster.users[0]._id);
            //ClientWebSocket webSocket = twitchSocket.WebSocketConnectAsync().Result;

            //var testWebSocket = twitchSocket.WhisperSubscribeAsync(twitchSocket.whisperWebSocket,_broadcaster.users[0]._id).Result;


            PingSender ping = new PingSender(_irc);
            ping.Start();

            while (true)
            {
                string   fullMessage  = _irc.ReadMessage();
                CommandO foundCommand = new CommandO();
                if (fullMessage.Contains("PRIVMSG"))
                {
                    _username = UserD.GetUsername(fullMessage);
                    string message = ChatBot.GetMessage(fullMessage);
                    _user = UserD.GetUser(_username);
                    UserD.InsertUser(_user);

                    bool isSubscriber = SubscriberD.IsSubscriber(_user.users[0]._id, _broadcaster.users[0]._id);


                    if (isSubscriber == false && _user.isPermit == false)
                    {
                        bool link = ChatBot.checkLink(message);
                        if (link == true)
                        {
                            _irc.WriteChatMessage(".timeout " + _username + " 15");
                            _irc.WriteChatMessage("Posting links is not allowed here for non-subs, if you think this link might interest me, just whisper me or one of my mods ♡");
                        }
                    }

                    char firstCharacter = message[0];

                    try
                    {
                        if (firstCharacter == '!')
                        {
                            string commandMessage = message.Substring(message.IndexOf('!') + 1);

                            bool kappamonCommand = CommandD.IsKappamonCommand(commandMessage);

                            if (!kappamonCommand)
                            {
                                if (commandMessage.Contains(" "))
                                {
                                    _command = commandMessage.Split(' ');
                                    if (_command.Length == 2)
                                    {
                                        commandMessage   = _command[0];
                                        _chatterUsername = _command[1];
                                    }
                                }
                                else
                                {
                                    _command[0] = commandMessage;
                                }

                                if (commands.Any(c => c.keyword == commandMessage))
                                {
                                    foundCommand = commands.Single(c => c.keyword == commandMessage);

                                    if (foundCommand.parameters != 0)
                                    {
                                        Type testType = typeof(Program);
                                        Dictionary <string, dynamic> newDic = new Dictionary <string, dynamic>();

                                        foreach (KeyValuePair <string, dynamic> dic in foundCommand.parameterList)
                                        {
                                            var fieldInfo = testType.GetField(dic.Key.ToString(), BindingFlags.Static | BindingFlags.Public).GetValue(testType);
                                            newDic.Add(dic.Key.ToString(), fieldInfo.ToString());
                                        }

                                        foundCommand.parameterList = newDic;
                                    }

                                    if (foundCommand.userLevel == "moderator" && !_moderators.Contains(_username))
                                    {
                                        _irc.WriteChatMessage("You are not allowed to use this command !");
                                    }
                                    else
                                    {
                                        DateTime date = DateTime.Now;

                                        if (foundCommand.startedTime.AddMilliseconds(foundCommand.cooldown) < DateTime.Now)

                                        {
                                            foundCommand.startedTime = DateTime.Now;
                                            if (foundCommand.keyword == "commands")
                                            {
                                                foundCommand.message += _commandsText;
                                            }

                                            switch (foundCommand.type)
                                            {
                                            case "request":
                                                string query = foundCommand.request;
                                                using (MySqlConnection mySqlConnection = new MySqlConnection("" /*connectionString*/))
                                                {
                                                    if (foundCommand.condition != "")
                                                    {
                                                        switch (foundCommand.condition)
                                                        {
                                                        case "userName":
                                                            foundCommand.request = string.Format(foundCommand.request, _username);
                                                            foundCommand.message = string.Format(foundCommand.message, _username);
                                                            break;
                                                        }
                                                    }

                                                    if (foundCommand.request.Contains("SELECT"))
                                                    {
                                                        Tuple <int, string> test = CommandD.ExecuteSelectCommand(foundCommand.request);
                                                        if (foundCommand.message.Contains("@"))
                                                        {
                                                            _irc.WriteChatMessage(foundCommand.message.Replace("@", test.Item1.ToString()));
                                                        }
                                                        else
                                                        {
                                                            _irc.WriteChatMessage(test.Item2);
                                                        }
                                                    }
                                                    else if (foundCommand.request.Contains("UPDATE"))
                                                    {
                                                        Tuple <int, string> result = CommandD.ExecuteUpdateCommand(foundCommand.request, foundCommand.message);
                                                        mySqlConnection.Close();
                                                        if (result.Item1 < 0)
                                                        {
                                                        }
                                                        else
                                                        {
                                                            _irc.WriteChatMessage(result.Item2);
                                                        }
                                                    }
                                                }
                                                break;

                                            case "regular":
                                                if (foundCommand.message.Contains("{"))
                                                {
                                                    _irc.WriteChatMessage(string.Format(foundCommand.message, _username));
                                                }
                                                else
                                                {
                                                    _irc.WriteChatMessage(foundCommand.message);
                                                }
                                                break;

                                            case "api":
                                                MethodInfo mInfo;
                                                Type       type = Assembly.Load("MoonBot_Data").GetType(foundCommand.assembly, false, true);
                                                mInfo = type.GetMethod(foundCommand.message);
                                                object[] parameters;

                                                if (foundCommand.parameters == 0)
                                                {
                                                    parameters = new object[] {};
                                                }
                                                else
                                                {
                                                    //Type testType = typeof(Program);
                                                    parameters = new object[] { foundCommand.parameterList };
                                                }

                                                object apiAnswer = mInfo.Invoke(null, parameters);
                                                _irc.WriteChatMessage(apiAnswer.ToString());
                                                break;

                                            case "moonlights":
                                                _irc.WriteChatMessage("Switching color to : " + foundCommand.keyword);
                                                _port.Open();
                                                _port.Write(foundCommand.message);
                                                _port.Close();
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            TimeSpan span = date - foundCommand.startedTime;
                                            int      ms   = (int)span.TotalMilliseconds;
                                            if (ms <= foundCommand.cooldown)
                                            {
                                                _irc.WriteChatMessage("This command is in cooldown right now, be patient !");
                                            }
                                            else
                                            {
                                                _irc.WriteChatMessage(foundCommand.message);
                                                foundCommand.startedTime = DateTime.Now;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    _irc.WriteChatMessage("This command does not exist, type !commands to know what commands are available");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        StringBuilder sb = new StringBuilder(DateTime.Now.ToString("dd-MM-yyyy") + " : " + ex.Message);
                        Console.WriteLine(sb);
                    }
                }
            }
        }