improved song queue logic, added cleanup function
This commit is contained in:
parent
f6bb62cb32
commit
91affc7cef
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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) => {
|
|
||||||
try {
|
|
||||||
var msgTxt = $"🎵`Playing`{song.PrettyName} `Vol: {(int)(musicPlayer.Volume * 100)}%`";
|
|
||||||
await textCh.SendMessage(msgTxt);
|
|
||||||
} catch { }
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
MusicPlayers.TryAdd(textCh.Server, musicPlayer);
|
mp.OnStarted += async (s, song) => {
|
||||||
}
|
var sender = s as MusicPlayer;
|
||||||
|
if (sender == null)
|
||||||
|
return;
|
||||||
|
try {
|
||||||
|
var msgTxt = $"🎵`Playing`{song.PrettyName} `Vol: {(int)(sender.Volume * 100)}%`";
|
||||||
|
await textCh.SendMessage(msgTxt);
|
||||||
|
} catch { }
|
||||||
|
};
|
||||||
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user