示例#1
0
 public void Cleanup()
 {
     _player.Pause();
     _player.Dispose();
     _player = null;
 }
示例#2
0
 public void Cleanup()
 {
     
     _player.Pause();
     _player.Dispose();
     _player = null;
 }
示例#3
0
        public void Register(Robot robot)
        {
            _robot  = robot;
            _player = new SpotifyPlayer(robot);

            Observable.FromEventPattern <Track>(e => _player.TrackChanged += e, e => _player.TrackChanged -= e)
            .Select(s => Unit.Default)
            .Merge(
                Observable.FromEventPattern <SpotifyPlayer.PlayerState>(e => _player.StateChanged += e,
                                                                        e => _player.StateChanged -= e).Select(s => Unit.Default))
            .Throttle(TimeSpan.FromMilliseconds(300))
            .Subscribe(a => UpdateLoungeTopic());

            _player.LoungeRoom = robot.GetConfigVariable("MMBOT_SPOTIFY_LOUNGE");


            robot.Respond(@"spotify shuffle( on| off)?", async msg =>
            {
                var off = msg.Match[1].ToLowerInvariant().Trim() == "off";
                if (off)
                {
                    await _player.SetShuffleOff();
                }
                else
                {
                    await _player.SetShuffleOn();
                }

                await msg.Send(string.Format("Shuffle is {0}", off ? "OFF" : "ON"));
            });

            robot.Respond(@"spotify play( album)?( .*)?", async msg =>
            {
                if (!await Login(msg))
                {
                    return;
                }

                bool isAlbum = !string.IsNullOrEmpty(msg.Match[1]);
                string query = msg.Match[2].Trim();

                if (string.IsNullOrWhiteSpace(query))
                {
                    await _player.Play();
                }
                else
                {
                    string message = null;
                    if (_spotifyLinkRegex.IsMatch(query))
                    {
                        // We have a link so process as such
                        message = await _player.PlayLink(query);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(query))
                        {
                            await msg.Send("Nothing to search for");
                            return;
                        }

                        if (isAlbum)
                        {
                            var album = await _player.SearchForAlbum(query);
                            if (album != null)
                            {
                                message = await _player.PlayAlbum(album);
                            }
                            else
                            {
                                await msg.Send(string.Format("Could not find any albums matching '{0}'", query));
                                msg.Message.Done = true;
                            }
                        }
                        else
                        {
                            // Search for a matching track
                            var track = await _player.SearchForTrack(query);
                            if (track != null)
                            {
                                await _player.Play(track);
                            }
                            else
                            {
                                await msg.Send(string.Format("Could not find any tracks matching '{0}'", query));
                                msg.Message.Done = true;
                            }
                        }
                    }
                    // Output the user message from the play request, if any
                    if (!string.IsNullOrWhiteSpace(message))
                    {
                        await msg.Send(message);
                    }
                }
            });

            robot.Respond(@"spotify (en)?queue( album)? (.*)", async msg =>
            {
                if (!await Login(msg))
                {
                    return;
                }
                bool isAlbum   = !string.IsNullOrEmpty(msg.Match[2]);
                string query   = msg.Match[3];
                string message = null;
                if (_spotifyLinkRegex.IsMatch(query))
                {
                    // We have a link so process as such
                    message = await _player.QueueLink(query);
                }
                else
                {
                    if (string.IsNullOrEmpty(query))
                    {
                        await msg.Send("Nothing to search for");
                        return;
                    }

                    // We just have a query so search
                    if (isAlbum)
                    {
                        var album = await _player.SearchForAlbum(query);
                        if (album != null)
                        {
                            message = await _player.QueueUpAlbum(album);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any albums matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                    else
                    {
                        var track = await _player.SearchForTrack(query);
                        if (track != null)
                        {
                            message = await _player.QueueUpTrack(track, true);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any tracks matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                }
                // Output the user message from the play request, if any
                if (!string.IsNullOrWhiteSpace(message))
                {
                    await msg.Send(message);
                }
            });

            robot.Respond(@"spotify show( album| artist| playlist)? (.*)", async msg =>
            {
                if (!await Login(msg))
                {
                    return;
                }

                if (msg.Match[2].Trim().ToLowerInvariant() == "queue")
                {
                    return;
                }

                bool isAlbum    = msg.Match[1].Trim().ToLowerInvariant() == "album";
                bool isArtist   = msg.Match[1].Trim().ToLowerInvariant() == "artist";
                bool isPlaylist = msg.Match[1].Trim().ToLowerInvariant() == "playlist";

                string query = msg.Match[2].Trim();


                if (_spotifyLinkRegex.IsMatch(query))
                {
                    // We have a link so process as such
                    var link = await _player.ParseLink(query);
                    if (link.Type == LinkType.Album)
                    {
                        await ListAlbumTracks(await link.AsAlbum(), msg);
                    }
                    else if (link.Type == LinkType.Playlist)
                    {
                        await ListPlaylistTracks(await link.AsPlaylist(), msg);
                    }
                    else if (link.Type == LinkType.Track)
                    {
                        await msg.Send("Track: " + (await link.AsTrack()).GetDisplayName());
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(query))
                    {
                        await msg.Send("Nothing to search for");
                        return;
                    }
                    if (isAlbum)
                    {
                        var album = await _player.SearchForAlbum(query);
                        if (album != null)
                        {
                            await ListAlbumTracks(album, msg);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any albums matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                    else if (isArtist)
                    {
                        var artist = await _player.SearchForArtist(query);
                        if (artist != null)
                        {
                            await ListArtistAlbums(artist, msg);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any artists matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                    else if (isPlaylist)
                    {
                        var playList = await _player.SearchForPlaylist(query);
                        if (playList != null)
                        {
                            await ListPlaylistTracks(playList, msg);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any playlists matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                    else
                    {
                        await msg.Send("Please specify album/artist/playlist. e.g. spotify show artist Journey");
                    }
                }
            });

            robot.Respond(@"spotify clear queue", async msg =>
            {
                var count = await _player.ClearQueue();

                await msg.Send(string.Format("{0} items have been cleared from the queue.", count));
            });


            robot.Respond(@"spotify next", async msg =>
            {
                if (!await CheckForPlayingSession(msg))
                {
                    return;
                }

                if (!_player.Queue.Any())
                {
                    await msg.Send("There is no next track");
                    return;
                }

                await _player.Pause();
                var next = await _player.PlayNextInQueue();
                await msg.Send(string.Format("Playing {0}", next.GetDisplayName()));
            });

            robot.Respond(@"(spotify )?(stop|pause)", async msg =>
            {
                if (!await CheckForPlayingSession(msg))
                {
                    return;
                }
                await _player.Pause();
            });

            robot.Respond(@"mute", async msg =>
            {
                if (!await CheckForPlayingSession(msg))
                {
                    return;
                }
                _player.Mute();
            });

            robot.Respond(@"unmute", async msg =>
            {
                if (!await CheckForPlayingSession(msg))
                {
                    return;
                }
                _player.Unmute();
            });

            robot.Respond(@"spotify show queue", async msg =>
            {
                if (!await Login(msg))
                {
                    return;
                }
                if (!_player.Queue.Any())
                {
                    await msg.Send("There are no tracks in the queue");
                    return;
                }


                IEnumerable <string> queue = _player.Queue.Take(20).Select(item => item.GetDisplayName()).ToArray();
                if (_player.Queue.Count() > 20)
                {
                    queue = queue.Concat(new[] { string.Format("+ {0} not listed", _player.Queue.Count() - 20) });
                }
                await msg.Send(string.Join(Environment.NewLine, queue.ToArray()));
            });

            robot.Respond(@"spotify remove (.*) from( the)? queue", async msg =>
            {
                if (!await Login(msg))
                {
                    return;
                }
                if (_player.Queue == null || !_player.Queue.Any())
                {
                    await msg.Send("There are no tracks in the queue");
                    return;
                }

                int count = await _player.RemoveFromQueue(msg.Match[1]);


                if (count == 0)
                {
                    await msg.Send("There were no matching tracks in the queue");
                }
                else
                {
                    await msg.Send(string.Format("{0} tracks were removed from the queue", count));
                }
            });

            robot.Respond(@"(turn|crank) it (up|down)( to (\d+))?", async msg =>
            {
                if (!await CheckForPlayingSession(msg))
                {
                    return;
                }

                string direction = msg.Match[2].Trim();
                string amount    = msg.Match[4].Trim();

                if (!string.IsNullOrWhiteSpace(amount))
                {
                    _player.SetVolume(int.Parse(amount));
                }
                if (direction.ToLowerInvariant() == "up")
                {
                    _player.TurnUpVolume(10);
                }
                else
                {
                    _player.TurnUpVolume(10);
                }
            });

            robot.Respond(@"spotify ship it", async msg =>
            {
                if (!await Login(msg))
                {
                    return;
                }

                await _player.PlayLink("spotify:track:77NNZQSqzLNqh2A9JhLRkg");
            });

            robot.RegisterCleanup(Cleanup);
        }
示例#4
0
        public void Register(Robot robot)
        {
            _robot = robot;
            _player = new SpotifyPlayer(robot);

            Observable.FromEventPattern<Track>(e => _player.TrackChanged += e, e => _player.TrackChanged -= e)
                .Select(s => Unit.Default)
                .Merge(
                    Observable.FromEventPattern<SpotifyPlayer.PlayerState>(e => _player.StateChanged += e,
                        e => _player.StateChanged -= e).Select(s => Unit.Default))
                        .Throttle(TimeSpan.FromMilliseconds(300))
                .Subscribe(a => UpdateLoungeTopic());

            _player.LoungeRoom = robot.GetConfigVariable("MMBOT_SPOTIFY_LOUNGE");
            

            robot.Respond(@"spotify shuffle( on| off)?", async msg =>
            {
                var off = msg.Match[1].ToLowerInvariant().Trim() == "off";
                if (off)
                {
                    await _player.SetShuffleOff();
                }
                else
                {
                    await _player.SetShuffleOn();
                }

                await msg.Send(string.Format("Shuffle is {0}", off ? "OFF" : "ON"));
            });

            robot.Respond(@"spotify play( album)?( .*)?", async msg =>
            {
                if(!await Login(msg)) return;

                bool isAlbum = !string.IsNullOrEmpty(msg.Match[1]);
                string query = msg.Match[2].Trim();

                if (string.IsNullOrWhiteSpace(query))
                {
                    await _player.Play();
                }
                else
                {
                    string message = null;
                    if (_spotifyLinkRegex.IsMatch(query))
                    {
                        // We have a link so process as such
                        message = await _player.PlayLink(query);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(query))
                        {
                            await msg.Send("Nothing to search for");
                            return;
                        }

                        if (isAlbum)
                        {
                            var album = await _player.SearchForAlbum(query);
                            if (album != null)
                            {
                                message = await _player.PlayAlbum(album);
                            }
                            else
                            {
                                await msg.Send(string.Format("Could not find any albums matching '{0}'", query));
                                msg.Message.Done = true;
                            }
                        }
                        else
                        {
                            // Search for a matching track
                            var track = await _player.SearchForTrack(query);
                            if (track != null)
                            {
                                await _player.Play(track);
                            }
                            else
                            {
                                await msg.Send(string.Format("Could not find any tracks matching '{0}'", query));
                                msg.Message.Done = true;
                            }
                        }
                    }
                    // Output the user message from the play request, if any
                    if (!string.IsNullOrWhiteSpace(message))
                    {
                        await msg.Send(message);
                    }
                }

                
            });

            robot.Respond(@"spotify (en)?queue( album)? (.*)", async msg =>
            {
                if (!await Login(msg)) return;
                bool isAlbum = !string.IsNullOrEmpty(msg.Match[2]);
                string query = msg.Match[3];
                string message = null;
                if (_spotifyLinkRegex.IsMatch(query))
                {
                    // We have a link so process as such
                    message = await _player.QueueLink(query);
                }
                else
                {
                    if (string.IsNullOrEmpty(query))
                    {
                        await msg.Send("Nothing to search for");
                        return;
                    }

                    // We just have a query so search
                    if (isAlbum)
                    {
                        var album = await _player.SearchForAlbum(query);
                        if (album != null)
                        {
                            message = await _player.QueueUpAlbum(album);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any albums matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                    else
                    {
                        var track = await _player.SearchForTrack(query);
                        if (track != null)
                        {
                            message = await _player.QueueUpTrack(track, true);
                        }
                        else
                        {                             
                            await msg.Send(string.Format("Could not find any tracks matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                }
                // Output the user message from the play request, if any
                if (!string.IsNullOrWhiteSpace(message))
                {
                    await msg.Send(message);
                }
            });

            robot.Respond(@"spotify show( album| artist| playlist)? (.*)", async msg =>
            {
                if (!await Login(msg)) return;

                if (msg.Match[2].Trim().ToLowerInvariant() == "queue")
                {
                    return;
                }

                bool isAlbum = msg.Match[1].Trim().ToLowerInvariant() == "album";
                bool isArtist = msg.Match[1].Trim().ToLowerInvariant() == "artist";
                bool isPlaylist = msg.Match[1].Trim().ToLowerInvariant() == "playlist";

                string query = msg.Match[2].Trim();


                if (_spotifyLinkRegex.IsMatch(query))
                {
                    // We have a link so process as such
                    var link = await _player.ParseLink(query);
                    if (link.Type == LinkType.Album)
                    {
                        await ListAlbumTracks(await link.AsAlbum(), msg);
                    }
                    else if (link.Type == LinkType.Playlist)
                    {
                        await ListPlaylistTracks(await link.AsPlaylist(), msg);
                    }
                    else if (link.Type == LinkType.Track)
                    {
                        await msg.Send("Track: " + (await link.AsTrack()).GetDisplayName());
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(query))
                    {
                        await msg.Send("Nothing to search for");
                        return;
                    }
                    if (isAlbum)
                    {
                        var album = await _player.SearchForAlbum(query);
                        if (album != null)
                        {
                            await ListAlbumTracks(album, msg);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any albums matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                    else if(isArtist)
                    {
                        var artist = await _player.SearchForArtist(query);
                        if (artist != null)
                        {
                            await ListArtistAlbums(artist, msg);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any artists matching '{0}'", query));
                            msg.Message.Done = true;
                        }

                    }
                    else if (isPlaylist)
                    {
                        var playList = await _player.SearchForPlaylist(query);
                        if (playList != null)
                        {
                            await ListPlaylistTracks(playList, msg);
                        }
                        else
                        {
                            await msg.Send(string.Format("Could not find any playlists matching '{0}'", query));
                            msg.Message.Done = true;
                        }
                    }
                    else
                    {
                        await msg.Send("Please specify album/artist/playlist. e.g. spotify show artist Journey");
                    }

                }
            });

            robot.Respond(@"spotify clear queue", async msg =>
            {

                var count = await _player.ClearQueue();

                await msg.Send(string.Format("{0} items have been cleared from the queue.", count));
            });


            robot.Respond(@"spotify next", async msg =>
            {
                if (!await CheckForPlayingSession(msg)) return;
                
                if (!_player.Queue.Any())
                {
                    await msg.Send("There is no next track");
                    return;
                }

                await _player.Pause();
                var next = await _player.PlayNextInQueue();
                await msg.Send(string.Format("Playing {0}", next.GetDisplayName()));
            });

            robot.Respond(@"(spotify )?(stop|pause)", async msg =>
            {
                if (!await CheckForPlayingSession(msg)) return;
                await _player.Pause();
            });
            
            robot.Respond(@"mute", async msg =>
            {
                if (!await CheckForPlayingSession(msg)) return;
                _player.Mute();
            });

            robot.Respond(@"unmute", async msg =>
            {
                if (!await CheckForPlayingSession(msg)) return;
                _player.Unmute();
            });

            robot.Respond(@"spotify show queue", async msg =>
            {
                if (!await Login(msg)) return;
                if (!_player.Queue.Any())
                {
                    await msg.Send("There are no tracks in the queue");
                    return;
                }


                IEnumerable<string> queue = _player.Queue.Take(20).Select(item => item.GetDisplayName()).ToArray();
                if (_player.Queue.Count() > 20)
                {
                    queue = queue.Concat(new[] { string.Format("+ {0} not listed", _player.Queue.Count() - 20) });
                }
                await msg.Send(string.Join(Environment.NewLine, queue.ToArray()));
                
            });
            
            robot.Respond(@"spotify remove (.*) from( the)? queue", async msg =>
            {
                if (!await Login(msg)) return;
                if (_player.Queue == null || !_player.Queue.Any())
                {
                    await msg.Send("There are no tracks in the queue");
                    return;
                }

                int count = await _player.RemoveFromQueue(msg.Match[1]);
                
                
                if (count == 0)
                {
                    await msg.Send("There were no matching tracks in the queue");
                }
                else
                {
                    await msg.Send(string.Format("{0} tracks were removed from the queue", count));
                }
            });

            robot.Respond(@"(turn|crank) it (up|down)( to (\d+))?", async msg =>
            {
                if (!await CheckForPlayingSession(msg)) return;
                
                string direction = msg.Match[2].Trim();
                string amount = msg.Match[4].Trim();

                if (!string.IsNullOrWhiteSpace(amount))
                {
                    _player.SetVolume(int.Parse(amount));
                }
                if (direction.ToLowerInvariant() == "up")
                {
                    _player.TurnUpVolume(10);
                }
                else
                {
                    _player.TurnUpVolume(10);
                }
                
            });

            robot.Respond(@"spotify ship it", async msg =>
            {
                if (!await Login(msg)) return;

                await _player.PlayLink("spotify:track:77NNZQSqzLNqh2A9JhLRkg");
                
                await Giphy.GifMe(_robot, "winning", msg);
            });

            robot.RegisterCleanup(Cleanup);
        }