示例#1
0
 private async void StopRadio()
 {
     _runRadio = false;
     await MusicFile.ResetAnd();
 }
示例#2
0
        //Project Butler command hook
        public override async Task OnCommandRecieved(Command cmd)
        {
            if (_instance == null)
            {
                UiThread.Invoke(() => { _instance = new SongList(); });
                cmd.Respond("Loading songs..");
                await _instance.LoadAllSongs();

                cmd.Respond("Songs loaded!");
            }

            if (!_instance.Ready)
            {
                cmd.Respond("Music player not ready.");
                return;
            }

            if (cmd.LocalCommand == "List")
            {
                if (cmd.IsLocalCommand)
                {
                    PlayOrShowSongList(SearchQuery.Empty);
                }
                else
                {
                    var output = $"Songs:- \n\n\n";
                    for (var i = 0; i < MusicFile.LoadedFiles.Count; i++)
                    {
                        output += $"[{i.ToString().PadLeft(3)}] {MusicFile.LoadedFiles[i]}\n";
                    }
                    cmd.Respond(output);
                }

                return;
            }

            if (cmd.LocalCommand == "Choose")
            {
                var numInt =
                    int.Parse(RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["choice"].Value);

                if (numInt > MusicFile.LoadedFiles.Count - 1 || numInt < 0)
                {
                    cmd.Respond($"Song #{numInt} does not exist.");
                    return;
                }

                await MusicFile.LoadedFiles[numInt].PlayAsync();
            }

            if (cmd.LocalCommand == "Random")
            {
                await RandomSong.PlayAsync();

                return;
            }

            if (cmd.LocalCommand == "Refresh")
            {
                cmd.Respond("Refreshing..");
                await _instance.RefreshSongs();

                cmd.Respond("Refreshed!");

                return;
            }

            if (cmd.LocalCommand == "Replay")
            {
                if (LastPlayedSong == null)
                {
                    return;
                }

                if (_runRadio)
                {
                    await MusicFile.ResetAnd(async() => { await LastPlayedSong.PlayAsync(); });

                    return;
                }

                await LastPlayedSong.PlayAsync();

                cmd.Respond($"Replaying {LastPlayedSong}");
                return;
            }

            if (cmd.LocalCommand == "Search")
            {
                var query = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["search"].Value;
                var sq    = SearchQuery.Generate(query);

                UiThread.Invoke(() => {
                    _instance.SetSearchQueryString(sq);
                });

                if (cmd.IsLocalCommand)
                {
                    PlayOrShowSongList(sq);
                    return;
                }
                else
                {
                    var results = await MusicFile.ExecuteSearchQuery(sq);

                    if (results.Count == 0)
                    {
                        cmd.Respond($"No results found for {query}");
                    }
                    else
                    {
                        var mf = results[0];
                        cmd.Respond($"Playing {mf}");
                        await mf.PlayAsync();
                    }
                    return;
                }
            }

            if (cmd.LocalCommand == "Play Song")
            {
                var song = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["name"].Value.ToLower();

                var mf = MusicFile.LoadedFiles.FirstOrDefault(f => Regex.Match(f.Title.ToLower(), song).Success);
                if (mf == null)
                {
                    cmd.Respond("Song not found.");
                    return;
                }

                await mf.PlayAsync();

                return;
            }

            if (cmd.LocalCommand == "List Artist")
            {
                var artist = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["artist"].Value.ToLower();

                if (cmd.IsLocalCommand)
                {
                    PlayOrShowSongList(new SearchQuery()
                    {
                        Artist = artist
                    });
                }
                else
                {
                    var output = $"Songs by {artist}:-\n";
                    var files  = await MusicFile.ExecuteSearchQuery(new SearchQuery()
                    {
                        Artist = artist
                    });

                    for (var i = 0; i < files.Count; i++)
                    {
                        output += $"[{i.ToString().PadLeft(3)}] {files[i]}\n";
                    }
                    cmd.Respond(output);
                }

                return;
            }

            if (cmd.LocalCommand == "List Album")
            {
                var album = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["album"].Value.ToLower();

                if (cmd.IsLocalCommand)
                {
                    PlayOrShowSongList(new SearchQuery()
                    {
                        Album = album
                    });
                }
                else
                {
                    var output = $"Songs on {album}:-\n";
                    var files  = await MusicFile.ExecuteSearchQuery(new SearchQuery()
                    {
                        Artist = album
                    });

                    for (var i = 0; i < files.Count; i++)
                    {
                        output += $"[{i.ToString().PadLeft(3)}] {files[i]}\n";
                    }
                    cmd.Respond(output);
                }

                return;
            }

            if (cmd.LocalCommand == "Pause")
            {
                await LastPlayedSong?.PauseAsync();

                return;
            }

            if (cmd.LocalCommand == "Resume")
            {
                await LastPlayedSong?.ResumeAsync();

                return;
            }

            if (cmd.LocalCommand == "Stop")
            {
                await LastPlayedSong?.StopAsync();
            }

            if (cmd.LocalCommand == "Toggle")
            {
                await LastPlayedSong?.ToggleAsync();

                return;
            }

            if (cmd.LocalCommand == "Radio")
            {
                switch (RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["command"].Value.ToLower())
                {
                case "start":
                    await StartRadio();

                    cmd.Respond("Started radio");
                    break;

                case "stop":
                    StopRadio();
                    cmd.Respond("Stopped radio");
                    break;
                }
                return;
            }

            if (cmd.LocalCommand == "Skip Radio Song")
            {
                if (!_runRadio)
                {
                    return;
                }

                await MusicFile.ResetAnd(async() => {
                    var file = RandomSong;
                    await file?.PlayAsync();
                });

                cmd.Respond("Skipping currently playing song");
                return;
            }

            #region Sleep

            if (cmd.LocalCommand == "Sleep")
            {
                var seconds = RegisteredCommands[cmd.LocalCommand].Match(cmd.UserInput).Groups["seconds"].Value;
                int secs;
                if (string.IsNullOrWhiteSpace(seconds) || !int.TryParse(seconds, out secs))
                {
                    return;
                }

                _sleepTimer.Interval = secs * 1000;
                _sleepTimer.Elapsed += SleepTimerOnElapsedAsync;
                _sleepTimer.Start();
                cmd.Respond($"Sleeping after {secs} seconds");

                return;
            }

            if (cmd.LocalCommand == "Stop Sleep")
            {
                _sleepTimer?.Stop();
                cmd.Respond($"Stopped scheduled sleep");
                return;
            }

            if (cmd.LocalCommand == "Get Sleep")
            {
                var response = (_sleepTimer.Enabled == false)
                    ? @"Sleep timer is inactive."
                    : $"Remaining time: {_sleepTimer.RemainingTime}";
                cmd.Respond(response);
                return;
            }

            #endregion
        }
示例#3
0
 private async void SleepTimerOnElapsedAsync(object sender, ElapsedEventArgs elapsedEventArgs)
 {
     await MusicFile.ResetAnd();
 }