Example #1
0
        /// <summary>
        ///     A method to fetch the song that is currently playing via NightBot Song Request.
        ///     Returns null if unsuccessful and custom pause text is not set.
        ///     Returns Error Message if NightBot ID is not set
        /// </summary>
        /// <returns>Returns String with currently playing NB Song Request</returns>

        //public string FetchNightBot()
        //{
        //    // Checking if the user has set the setting for Nightbot
        //    if (string.IsNullOrEmpty(Settings.Settings.NbUserId)) return "No NightBot ID set.";
        //    // Getting JSON from the nightbot API
        //    string jsn;
        //    using (WebClient wc = new WebClient
        //    {
        //        Encoding = Encoding.UTF8
        //    })
        //    {
        //        string url = "https://api.nightbot.tv/1/song_requests/queue/?channel="+Settings.Settings.NbUserId;
        //        jsn = wc.DownloadString("https://api.nightbot.tv/1/song_requests/queue/?channel=" +
        //                                Settings.Settings.NbUserId);
        //    }

        //    // Deserialize JSON and get the current song
        //    NBObj json = JsonConvert.DeserializeObject<NBObj>(jsn);
        //    return json._currentsong == null ? null : (string)json._currentsong.track.title;
        //}

        public TrackInfo FetchSpotifyWeb()
        {
            // If the spotify object hast been created (successfully authed)
            if (ApiHandler.Spotify == null)
            {
                Logger.LogStr("SPOTIFY API: Spotify API Object is NULL");
                return(null);
            }

            // gets the current playing songinfo
            TrackInfo songInfo = ApiHandler.GetSongInfo();

            // if no song is playing and custompausetext is enabled
            return(songInfo ?? new TrackInfo {
                isPlaying = false
            });
            // return a new stringarray containing artist, title and so on
        }
Example #2
0
        private static void AddSong(string trackId, OnMessageReceivedArgs e)
        {
            // loads the blacklist from settings
            string[] blacklist = Settings.Settings.ArtistBlacklist.Split(new[] { "|||" }, StringSplitOptions.None);
            string   response;
            // gets the track information using spotify api
            FullTrack track = ApiHandler.GetTrack(trackId);

            // checks if one of the artist in the requested song is on the blacklist
            foreach (string s in blacklist)
            {
                if (Array.IndexOf(track.Artists.Select(x => x.Name).ToArray(), s) != -1)
                {
                    // if artist is on blacklist, skip and inform requester
                    response = Settings.Settings.BotRespBlacklist;
                    response = response.Replace("{user}", e.ChatMessage.DisplayName);
                    response = response.Replace("{artist}", s);
                    response = response.Replace("{title}", "");
                    response = response.Replace("{maxreq}", "");
                    response = response.Replace("{errormsg}", "");
                    response = CleanFormatString(response);

                    Client.SendMessage(e.ChatMessage.Channel, response);
                    return;
                }
            }

            // checks if song length is longer or equal to 10 minutes
            if (track.DurationMs >= TimeSpan.FromMinutes(Settings.Settings.MaxSongLength).TotalMilliseconds)
            {
                // if track length exceeds 10 minutes skip and inform requster
                response = Settings.Settings.BotRespLength;
                response = response.Replace("{user}", e.ChatMessage.DisplayName);
                response = response.Replace("{artist}", "");
                response = response.Replace("{title}", "");
                response = response.Replace("{maxreq}", "");
                response = response.Replace("{errormsg}", "");
                response = CleanFormatString(response);

                Client.SendMessage(e.ChatMessage.Channel, response);
                return;
            }

            // checks if the song is already in the queue
            if (IsInQueue(track.Id))
            {
                // if the song is already in the queue skip and inform requester
                response = Settings.Settings.BotRespIsInQueue;
                response = response.Replace("{user}", e.ChatMessage.DisplayName);
                response = response.Replace("{artist}", "");
                response = response.Replace("{title}", "");
                response = response.Replace("{maxreq}", "");
                response = response.Replace("{errormsg}", "");
                response = CleanFormatString(response);

                Client.SendMessage(e.ChatMessage.Channel, response);
                return;
            }

            // checks if the user has already the max amount of songs in the queue
            if (MaxQueueItems(e.ChatMessage.DisplayName))
            {
                // if the user reached max requests in the queue skip and inform requester
                response = Settings.Settings.BotRespMaxReq;
                response = response.Replace("{user}", e.ChatMessage.DisplayName);
                response = response.Replace("{artist}", "");
                response = response.Replace("{title}", "");
                response = response.Replace("{maxreq}", Settings.Settings.TwSrMaxReq.ToString());
                response = response.Replace("{errormsg}", "");
                response = CleanFormatString(response);
                Client.SendMessage(e.ChatMessage.Channel, response);
                return;
            }

            // generate the spotifyURI using the track id
            string spotifyUri = "spotify:track:" + trackId;

            // try adding the song to the queue using the URI
            ErrorResponse error = ApiHandler.AddToQ(spotifyUri);

            if (error.Error != null)
            {
                // if an error has been encountered, log it, inform the requester and skip
                Logger.LogStr(error.Error.Message + "\n" + error.Error.Status);
                response = Settings.Settings.BotRespError;
                response = response.Replace("{user}", e.ChatMessage.DisplayName);
                response = response.Replace("{artist}", "");
                response = response.Replace("{title}", "");
                response = response.Replace("{maxreq}", "");
                response = response.Replace("{errormsg}", error.Error.Message);

                Client.SendMessage(e.ChatMessage.Channel, response);
                return;
            }

            // if everything workes so far, inform the user that the song has been added to the queue
            response = Settings.Settings.BotRespSuccess;
            response = response.Replace("{user}", e.ChatMessage.DisplayName);
            response = response.Replace("{artist}", track.Artists[0].Name);
            response = response.Replace("{title}", track.Name);
            response = response.Replace("{maxreq}", "");
            response = response.Replace("{errormsg}", "");
            Client.SendMessage(e.ChatMessage.Channel, response);

            // Upload the track and who requested it to the queue on the server
            UploadToQueue(track, e.ChatMessage.DisplayName);

            // Add the song to the internal queue and update the queue window if its open
            Application.Current.Dispatcher.Invoke(() =>
            {
                Window mw = null, qw = null;
                foreach (Window window in Application.Current.Windows)
                {
                    if (window.GetType() == typeof(MainWindow))
                    {
                        mw = window;
                    }
                    if (window.GetType() == typeof(Window_Queue))
                    {
                        qw = window;
                    }
                }
                if (mw != null)
                {
                    (mw as MainWindow)?.ReqList.Add(new RequestObject
                    {
                        Requester = e.ChatMessage.DisplayName,
                        TrackID   = track.Id,
                        Title     = track.Name,
                        Artists   = track.Artists[0].Name,
                        Length    = FormattedTime(track.DurationMs)
                    });
                }

                if (qw != null)
                {
                    //(qw as Window_Queue).dgv_Queue.ItemsSource.
                    (qw as Window_Queue)?.dgv_Queue.Items.Refresh();
                }
            });
        }
Example #3
0
        private static void _client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
        {
            if (Settings.Settings.MsgLoggingEnabled)
            {
                // If message logging is enabled and the reward was triggered, save it to the settings (if settings window is open, write it to the textbox)
                if (e.ChatMessage.CustomRewardId != null)
                {
                    Settings.Settings.TwRewardId = e.ChatMessage.CustomRewardId;

                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        foreach (Window window in Application.Current.Windows)
                        {
                            if (window.GetType() == typeof(Window_Settings))
                            {
                                ((Window_Settings)window).txtbx_RewardID.Text = e.ChatMessage.CustomRewardId;
                            }
                        }
                    });
                }
            }

            // if the reward is the same with the desired reward for the requests
            if (Settings.Settings.TwSrReward && e.ChatMessage.CustomRewardId == Settings.Settings.TwRewardId)
            {
                if (IsUserBlocked(e.ChatMessage.DisplayName))
                {
                    Client.SendWhisper(e.ChatMessage.Username, "You are blocked from making Songrequests");
                    return;
                }

                if (ApiHandler.Spotify == null)
                {
                    Client.SendMessage(e.ChatMessage.Channel, "It seems that Spotify is not connected right now.");
                    return;
                }

                // if Spotify is connected and working manipulate the string and call methods to get the song info accordingly
                if (e.ChatMessage.Message.StartsWith("spotify:track:"))
                {
                    // search for a track with the id
                    string trackId = e.ChatMessage.Message.Replace("spotify:track:", "");

                    // add the track to the spotify queue and pass the OnMessageReceivedArgs (contains user who requested the song etc)
                    AddSong(trackId, e);
                }

                else if (e.ChatMessage.Message.StartsWith("https://open.spotify.com/track/"))
                {
                    string trackid = e.ChatMessage.Message.Replace("https://open.spotify.com/track/", "");
                    trackid = trackid.Split('?')[0];
                    AddSong(trackid, e);
                }

                else
                {
                    // search for a track with a search string from chat
                    SearchItem searchItem = ApiHandler.FindTrack(e.ChatMessage.Message);
                    if (searchItem.Tracks.Items.Count > 0)
                    {
                        // if a track was found convert the object to FullTrack (easier use than searchItem)
                        FullTrack fullTrack = searchItem.Tracks.Items[0];

                        // add the track to the spotify queue and pass the OnMessageReceivedArgs (contains user who requested the song etc)
                        AddSong(fullTrack.Id, e);
                    }
                    else
                    {
                        string response;
                        // if no track has been found inform the requester
                        response = Settings.Settings.BotRespError;
                        response = response.Replace("{user}", e.ChatMessage.DisplayName);
                        response = response.Replace("{artist}", "");
                        response = response.Replace("{title}", "");
                        response = response.Replace("{maxreq}", "");
                        response = response.Replace("{errormsg}", "");

                        Client.SendMessage(e.ChatMessage.Channel, response);
                        return;
                    }
                }
                return;
            }

            // Same code from above but it reacts to a command instead of rewards
            if (Settings.Settings.TwSrCommand && e.ChatMessage.Message.StartsWith("!ssr"))
            {
                // Do nothing if the user is blocked, don't even reply
                if (IsUserBlocked(e.ChatMessage.DisplayName))
                {
                    Client.SendWhisper(e.ChatMessage.DisplayName, "You are blocked from making Songrequests");
                    return;
                }

                // if onCooldown skip
                if (OnCooldown)
                {
                    return;
                }

                if (ApiHandler.Spotify == null)
                {
                    Client.SendMessage(e.ChatMessage.Channel, "It seems that Spotify is not connected right now.");
                    return;
                }

                // if Spotify is connected and working manipulate the string and call methods to get the song info accordingly
                string[] msgSplit = e.ChatMessage.Message.Split(' ');

                // Prevent crash on command without args
                if (msgSplit.Length <= 1)
                {
                    string response = Settings.Settings.BotRespNoSong;
                    response = response.Replace("{user}", e.ChatMessage.DisplayName);
                    Client.SendMessage(e.ChatMessage.Channel, response);

                    StartCooldown();
                    return;
                }
                if (msgSplit[1].StartsWith("spotify:track:"))
                {
                    // search for a track with the id
                    string trackId = msgSplit[1].Replace("spotify:track:", "");
                    // add the track to the spotify queue and pass the OnMessageReceivedArgs (contains user who requested the song etc)
                    AddSong(trackId, e);
                }

                else if (msgSplit[1].StartsWith("https://open.spotify.com/track/"))
                {
                    string trackid = msgSplit[1].Replace("https://open.spotify.com/track/", "");
                    trackid = trackid.Split('?')[0];
                    AddSong(trackid, e);
                }
                else
                {
                    string searchString = e.ChatMessage.Message.Replace("!ssr ", "");
                    // search for a track with a search string from chat
                    SearchItem searchItem = ApiHandler.FindTrack(searchString);
                    if (searchItem.Tracks.Items.Count > 0)
                    {
                        // if a track was found convert the object to FullTrack (easier use than searchItem)
                        FullTrack fullTrack = searchItem.Tracks.Items[0];
                        // add the track to the spotify queue and pass the OnMessageReceivedArgs (contains user who requested the song etc)
                        AddSong(fullTrack.Id, e);
                    }
                    else
                    {
                        string response;
                        // if no track has been found inform the requester
                        response = Settings.Settings.BotRespError;
                        response = response.Replace("{user}", e.ChatMessage.DisplayName);
                        response = response.Replace("{artist}", "");
                        response = response.Replace("{title}", "");
                        response = response.Replace("{maxreq}", "");
                        response = response.Replace("{errormsg}", "No Song was found.");

                        Client.SendMessage(e.ChatMessage.Channel, response);
                        return;
                    }
                }
                // start the command cooldown
                StartCooldown();
            }
        }