improved song queue logic, added cleanup function

This commit is contained in:
Master Kwoth 2016-03-05 12:18:22 +01:00
parent f6bb62cb32
commit 91affc7cef
2 changed files with 37 additions and 21 deletions

View File

@ -38,8 +38,8 @@ namespace NadekoBot.Classes.Music {
public float Volume { get; private set; } public float Volume { get; private set; }
public Action<Song> OnCompleted = delegate { }; public event EventHandler<Song> OnCompleted = delegate { };
public Action<Song> OnStarted = delegate { }; public event EventHandler<Song> OnStarted = delegate { };
public Channel PlaybackVoiceChannel { get; private set; } public Channel PlaybackVoiceChannel { get; private set; }
@ -68,14 +68,14 @@ namespace NadekoBot.Classes.Music {
var curSong = CurrentSong; var curSong = CurrentSong;
if (curSong != null) { if (curSong != null) {
try { try {
OnStarted(curSong); OnStarted(this, curSong);
await curSong.Play(audioClient, cancelToken); await curSong.Play(audioClient, cancelToken);
} catch (OperationCanceledException) { } catch (OperationCanceledException) {
Console.WriteLine("Song canceled"); Console.WriteLine("Song canceled");
} catch (Exception ex) { } catch (Exception ex) {
Console.WriteLine($"Exception in PlaySong: {ex}"); Console.WriteLine($"Exception in PlaySong: {ex}");
} }
OnCompleted(curSong); OnCompleted(this, curSong);
SongCancelSource = new CancellationTokenSource(); SongCancelSource = new CancellationTokenSource();
cancelToken = SongCancelSource.Token; cancelToken = SongCancelSource.Token;
} }

View File

@ -306,6 +306,20 @@ namespace NadekoBot.Modules {
await e.Channel.SendMessage($"🎵**Track at position `#{num}` has been removed.**"); await e.Channel.SendMessage($"🎵**Track at position `#{num}` has been removed.**");
}); });
cgb.CreateCommand("cleanup")
.Description("Cleans up hanging voice connections. BOT OWNER ONLY")
.Do(e => {
foreach (var kvp in MusicPlayers) {
var songs = kvp.Value.Playlist;
var currentSong = kvp.Value.CurrentSong;
if (songs.Count == 0 && currentSong == null) {
MusicPlayer throwaway;
MusicPlayers.TryRemove(kvp.Key, out throwaway);
throwaway.Destroy();
}
}
});
//cgb.CreateCommand("debug") //cgb.CreateCommand("debug")
// .Description("Does something magical. **BOT OWNER ONLY**") // .Description("Does something magical. **BOT OWNER ONLY**")
// .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) // .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly())
@ -324,27 +338,29 @@ namespace NadekoBot.Modules {
} }
if (string.IsNullOrWhiteSpace(query) || query.Length < 3) if (string.IsNullOrWhiteSpace(query) || query.Length < 3)
throw new ArgumentException("💢 Invalid query for queue song.", nameof(query)); throw new ArgumentException("💢 Invalid query for queue song.", nameof(query));
MusicPlayer musicPlayer = null;
if (!MusicPlayers.TryGetValue(textCh.Server, out musicPlayer)) { var musicPlayer = MusicPlayers.GetOrAdd(textCh.Server, server => {
float? vol = null; float? vol = null;
float throwAway; float throwAway;
if (DefaultMusicVolumes.TryGetValue(textCh.Server.Id, out throwAway)) if (DefaultMusicVolumes.TryGetValue(server.Id, out throwAway))
vol = throwAway; vol = throwAway;
musicPlayer = new MusicPlayer(voiceCh, vol) { var mp = new MusicPlayer(voiceCh, vol);
OnCompleted = async song => { mp.OnCompleted += async (s, song) => {
try { try {
await textCh.SendMessage($"🎵`Finished`{song.PrettyName}"); await textCh.SendMessage($"🎵`Finished`{song.PrettyName}");
} catch { } } catch { }
}, };
OnStarted = async (song) => { mp.OnStarted += async (s, song) => {
var sender = s as MusicPlayer;
if (sender == null)
return;
try { try {
var msgTxt = $"🎵`Playing`{song.PrettyName} `Vol: {(int)(musicPlayer.Volume * 100)}%`"; var msgTxt = $"🎵`Playing`{song.PrettyName} `Vol: {(int)(sender.Volume * 100)}%`";
await textCh.SendMessage(msgTxt); await textCh.SendMessage(msgTxt);
} catch { } } catch { }
},
}; };
MusicPlayers.TryAdd(textCh.Server, musicPlayer); return mp;
} });
var resolvedSong = await Song.ResolveSong(query, musicType); var resolvedSong = await Song.ResolveSong(query, musicType);
resolvedSong.MusicPlayer = musicPlayer; resolvedSong.MusicPlayer = musicPlayer;
if (!silent) if (!silent)