Moved a part of music logic to a music service

This commit is contained in:
Master Kwoth 2017-05-14 21:00:35 +02:00
parent 558796809c
commit a6d3b0bd07
7 changed files with 304 additions and 212 deletions

View File

@ -76,10 +76,10 @@ namespace NadekoBot.Modules.Administration
{ "%servers%", () => NadekoBot.Client.Guilds.Count.ToString()}, { "%servers%", () => NadekoBot.Client.Guilds.Count.ToString()},
{ "%users%", () => NadekoBot.Client.Guilds.Sum(s => s.Users.Count).ToString()}, { "%users%", () => NadekoBot.Client.Guilds.Sum(s => s.Users.Count).ToString()},
{ "%playing%", () => { { "%playing%", () => {
var cnt = Music.Music.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null); var cnt = NadekoBot.MusicService.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null);
if (cnt != 1) return cnt.ToString(); if (cnt != 1) return cnt.ToString();
try { try {
var mp = Music.Music.MusicPlayers.FirstOrDefault(); var mp = NadekoBot.MusicService.MusicPlayers.FirstOrDefault();
return mp.Value.CurrentSong.SongInfo.Title; return mp.Value.CurrentSong.SongInfo.Title;
} }
catch { catch {
@ -87,7 +87,7 @@ namespace NadekoBot.Modules.Administration
} }
} }
}, },
{ "%queued%", () => Music.Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()}, { "%queued%", () => NadekoBot.MusicService.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()},
{ "%time%", () => DateTime.Now.ToString("HH:mm " + TimeZoneInfo.Local.StandardName.GetInitials()) }, { "%time%", () => DateTime.Now.ToString("HH:mm " + TimeZoneInfo.Local.StandardName.GetInitials()) },
{ "%shardcount%", () => NadekoBot.Client.Shards.Count.ToString() }, { "%shardcount%", () => NadekoBot.Client.Shards.Count.ToString() },
}; };

View File

@ -15,6 +15,7 @@ using Newtonsoft.Json.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using System.Threading; using System.Threading;
using NadekoBot.Services.Music;
namespace NadekoBot.Modules.Music namespace NadekoBot.Modules.Music
{ {
@ -22,10 +23,10 @@ namespace NadekoBot.Modules.Music
[DontAutoLoad] [DontAutoLoad]
public class Music : NadekoTopLevelModule public class Music : NadekoTopLevelModule
{ {
public static ConcurrentDictionary<ulong, MusicPlayer> MusicPlayers { get; } = new ConcurrentDictionary<ulong, MusicPlayer>();
public const string MusicDataPath = "data/musicdata"; public const string MusicDataPath = "data/musicdata";
private static MusicService music;
static Music() static Music()
{ {
//it can fail if its currenctly opened or doesn't exist. Either way i don't care //it can fail if its currenctly opened or doesn't exist. Either way i don't care
@ -34,6 +35,9 @@ namespace NadekoBot.Modules.Music
NadekoBot.Client.UserVoiceStateUpdated += Client_UserVoiceStateUpdated; NadekoBot.Client.UserVoiceStateUpdated += Client_UserVoiceStateUpdated;
Directory.CreateDirectory(MusicDataPath); Directory.CreateDirectory(MusicDataPath);
//todo move to service
music = NadekoBot.MusicService;
} }
private static Task Client_UserVoiceStateUpdated(SocketUser iusr, SocketVoiceState oldState, SocketVoiceState newState) private static Task Client_UserVoiceStateUpdated(SocketUser iusr, SocketVoiceState oldState, SocketVoiceState newState)
@ -44,13 +48,11 @@ namespace NadekoBot.Modules.Music
return Task.CompletedTask; return Task.CompletedTask;
MusicPlayer player; MusicPlayer player;
if (!MusicPlayers.TryGetValue(usr.Guild.Id, out player)) if ((player = music.GetPlayer(usr.Guild.Id)) == null)
return Task.CompletedTask; return Task.CompletedTask;
try try
{ {
//if bot moved //if bot moved
if ((player.PlaybackVoiceChannel == oldState.VoiceChannel) && if ((player.PlaybackVoiceChannel == oldState.VoiceChannel) &&
usr.Id == NadekoBot.Client.CurrentUser.Id) usr.Id == NadekoBot.Client.CurrentUser.Id)
@ -92,7 +94,8 @@ namespace NadekoBot.Modules.Music
return Task.CompletedTask; return Task.CompletedTask;
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) return Task.CompletedTask; if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return Task.CompletedTask;
if (musicPlayer.PlaybackVoiceChannel == ((IGuildUser)Context.User).VoiceChannel) if (musicPlayer.PlaybackVoiceChannel == ((IGuildUser)Context.User).VoiceChannel)
{ {
while (--skipCount > 0) while (--skipCount > 0)
@ -109,7 +112,8 @@ namespace NadekoBot.Modules.Music
public Task Stop() public Task Stop()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) return Task.CompletedTask; if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return Task.CompletedTask;
if (((IGuildUser)Context.User).VoiceChannel == musicPlayer.PlaybackVoiceChannel) if (((IGuildUser)Context.User).VoiceChannel == musicPlayer.PlaybackVoiceChannel)
{ {
musicPlayer.Autoplay = false; musicPlayer.Autoplay = false;
@ -123,10 +127,10 @@ namespace NadekoBot.Modules.Music
public Task Destroy() public Task Destroy()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) return Task.CompletedTask; if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return Task.CompletedTask;
if (((IGuildUser)Context.User).VoiceChannel == musicPlayer.PlaybackVoiceChannel) if (((IGuildUser)Context.User).VoiceChannel == musicPlayer.PlaybackVoiceChannel)
if (MusicPlayers.TryRemove(Context.Guild.Id, out musicPlayer)) music.DestroyPlayer(Context.Guild.Id);
musicPlayer.Destroy();
return Task.CompletedTask; return Task.CompletedTask;
@ -137,7 +141,8 @@ namespace NadekoBot.Modules.Music
public Task Pause() public Task Pause()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) return Task.CompletedTask; if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return Task.CompletedTask;
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel) if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return Task.CompletedTask; return Task.CompletedTask;
musicPlayer.TogglePause(); musicPlayer.TogglePause();
@ -150,7 +155,8 @@ namespace NadekoBot.Modules.Music
{ {
var channel = (ITextChannel)Context.Channel; var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) return; if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return;
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel) if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return; return;
var val = musicPlayer.FairPlay = !musicPlayer.FairPlay; var val = musicPlayer.FairPlay = !musicPlayer.FairPlay;
@ -169,7 +175,7 @@ namespace NadekoBot.Modules.Music
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task Queue([Remainder] string query) public async Task Queue([Remainder] string query)
{ {
await QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, query).ConfigureAwait(false); await music.QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, query).ConfigureAwait(false);
if ((await Context.Guild.GetCurrentUserAsync()).GetPermissions((IGuildChannel)Context.Channel).ManageMessages) if ((await Context.Guild.GetCurrentUserAsync()).GetPermissions((IGuildChannel)Context.Channel).ManageMessages)
{ {
Context.Message.DeleteAfter(10); Context.Message.DeleteAfter(10);
@ -180,7 +186,7 @@ namespace NadekoBot.Modules.Music
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task SoundCloudQueue([Remainder] string query) public async Task SoundCloudQueue([Remainder] string query)
{ {
await QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, query, musicType: MusicType.Soundcloud).ConfigureAwait(false); await music.QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, query, musicType: MusicType.Soundcloud).ConfigureAwait(false);
if ((await Context.Guild.GetCurrentUserAsync()).GetPermissions((IGuildChannel)Context.Channel).ManageMessages) if ((await Context.Guild.GetCurrentUserAsync()).GetPermissions((IGuildChannel)Context.Channel).ManageMessages)
{ {
Context.Message.DeleteAfter(10); Context.Message.DeleteAfter(10);
@ -193,8 +199,9 @@ namespace NadekoBot.Modules.Music
{ {
Song currentSong; Song currentSong;
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer) || if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
(currentSong = musicPlayer?.CurrentSong) == null) return;
if ((currentSong = musicPlayer?.CurrentSong) == null)
{ {
await ReplyErrorLocalized("no_player").ConfigureAwait(false); await ReplyErrorLocalized("no_player").ConfigureAwait(false);
return; return;
@ -253,7 +260,7 @@ namespace NadekoBot.Modules.Music
public async Task NowPlaying() public async Task NowPlaying()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
var currentSong = musicPlayer.CurrentSong; var currentSong = musicPlayer.CurrentSong;
if (currentSong == null) if (currentSong == null)
@ -274,7 +281,7 @@ namespace NadekoBot.Modules.Music
public async Task Volume(int val) public async Task Volume(int val)
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel) if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return; return;
@ -308,9 +315,8 @@ namespace NadekoBot.Modules.Music
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task ShufflePlaylist() public async Task ShufflePlaylist()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel) if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return; return;
@ -362,7 +368,7 @@ namespace NadekoBot.Modules.Music
return; return;
try try
{ {
await QueueSong(gusr, (ITextChannel)Context.Channel, gusr.VoiceChannel, id, true).ConfigureAwait(false); await music.QueueSong(gusr, (ITextChannel)Context.Channel, gusr.VoiceChannel, id, true).ConfigureAwait(false);
} }
catch (SongNotFoundException) { } catch (SongNotFoundException) { }
catch { try { cancelSource.Cancel(); } catch { } } catch { try { cancelSource.Cancel(); } catch { } }
@ -388,17 +394,17 @@ namespace NadekoBot.Modules.Music
using (var http = new HttpClient()) using (var http = new HttpClient())
{ {
var scvids = JObject.Parse(await http.GetStringAsync($"http://api.soundcloud.com/resolve?url={pl}&client_id={NadekoBot.Credentials.SoundCloudClientId}").ConfigureAwait(false))["tracks"].ToObject<SoundCloudVideo[]>(); var scvids = JObject.Parse(await http.GetStringAsync($"http://api.soundcloud.com/resolve?url={pl}&client_id={NadekoBot.Credentials.SoundCloudClientId}").ConfigureAwait(false))["tracks"].ToObject<SoundCloudVideo[]>();
await QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, scvids[0].TrackLink).ConfigureAwait(false); await music.QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, scvids[0].TrackLink).ConfigureAwait(false);
MusicPlayer mp; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out mp)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
foreach (var svideo in scvids.Skip(1)) foreach (var svideo in scvids.Skip(1))
{ {
try try
{ {
mp.AddSong(new Song(new SongInfo musicPlayer.AddSong(new Song(new SongInfo
{ {
Title = svideo.FullName, Title = svideo.FullName,
Provider = "SoundCloud", Provider = "SoundCloud",
@ -429,7 +435,7 @@ namespace NadekoBot.Modules.Music
{ {
try try
{ {
await QueueSong(gusr, (ITextChannel)Context.Channel, gusr.VoiceChannel, file.FullName, true, MusicType.Local).ConfigureAwait(false); await music.QueueSong(gusr, (ITextChannel)Context.Channel, gusr.VoiceChannel, file.FullName, true, MusicType.Local).ConfigureAwait(false);
} }
catch (PlaylistFullException) catch (PlaylistFullException)
{ {
@ -453,7 +459,7 @@ namespace NadekoBot.Modules.Music
await ReplyErrorLocalized("must_be_in_voice").ConfigureAwait(false); await ReplyErrorLocalized("must_be_in_voice").ConfigureAwait(false);
return; return;
} }
await QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, radioLink, musicType: MusicType.Radio).ConfigureAwait(false); await music.QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, radioLink, musicType: MusicType.Radio).ConfigureAwait(false);
if ((await Context.Guild.GetCurrentUserAsync()).GetPermissions((IGuildChannel)Context.Channel).ManageMessages) if ((await Context.Guild.GetCurrentUserAsync()).GetPermissions((IGuildChannel)Context.Channel).ManageMessages)
{ {
Context.Message.DeleteAfter(10); Context.Message.DeleteAfter(10);
@ -469,7 +475,7 @@ namespace NadekoBot.Modules.Music
var arg = path; var arg = path;
if (string.IsNullOrWhiteSpace(arg)) if (string.IsNullOrWhiteSpace(arg))
return; return;
await QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, path, musicType: MusicType.Local).ConfigureAwait(false); await music.QueueSong(((IGuildUser)Context.User), (ITextChannel)Context.Channel, ((IGuildUser)Context.User).VoiceChannel, path, musicType: MusicType.Local).ConfigureAwait(false);
} }
@ -491,7 +497,7 @@ namespace NadekoBot.Modules.Music
public Task Remove(int num) public Task Remove(int num)
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return Task.CompletedTask; return Task.CompletedTask;
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel) if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return Task.CompletedTask; return Task.CompletedTask;
@ -508,7 +514,8 @@ namespace NadekoBot.Modules.Music
if (all.Trim().ToUpperInvariant() != "ALL") if (all.Trim().ToUpperInvariant() != "ALL")
return; return;
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) return; if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return;
musicPlayer.ClearQueue(); musicPlayer.ClearQueue();
await ReplyConfirmLocalized("queue_cleared").ConfigureAwait(false); await ReplyConfirmLocalized("queue_cleared").ConfigureAwait(false);
} }
@ -521,7 +528,7 @@ namespace NadekoBot.Modules.Music
return; return;
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
fromto = fromto?.Trim(); fromto = fromto?.Trim();
@ -564,7 +571,7 @@ namespace NadekoBot.Modules.Music
public async Task SetMaxQueue(uint size = 0) public async Task SetMaxQueue(uint size = 0)
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
musicPlayer.MaxQueueSize = size; musicPlayer.MaxQueueSize = size;
@ -584,7 +591,7 @@ namespace NadekoBot.Modules.Music
var channel = (ITextChannel)Context.Channel; var channel = (ITextChannel)Context.Channel;
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(channel.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
musicPlayer.MaxPlaytimeSeconds = seconds; musicPlayer.MaxPlaytimeSeconds = seconds;
if (seconds == 0) if (seconds == 0)
@ -597,9 +604,8 @@ namespace NadekoBot.Modules.Music
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task ReptCurSong() public async Task ReptCurSong()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
var currentSong = musicPlayer.CurrentSong; var currentSong = musicPlayer.CurrentSong;
if (currentSong == null) if (currentSong == null)
@ -621,9 +627,8 @@ namespace NadekoBot.Modules.Music
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task RepeatPl() public async Task RepeatPl()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
var currentValue = musicPlayer.ToggleRepeatPlaylist(); var currentValue = musicPlayer.ToggleRepeatPlaylist();
if(currentValue) if(currentValue)
@ -636,9 +641,8 @@ namespace NadekoBot.Modules.Music
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task Save([Remainder] string name) public async Task Save([Remainder] string name)
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
var curSong = musicPlayer.CurrentSong; var curSong = musicPlayer.CurrentSong;
@ -694,7 +698,7 @@ namespace NadekoBot.Modules.Music
var usr = (IGuildUser)Context.User; var usr = (IGuildUser)Context.User;
try try
{ {
await QueueSong(usr, (ITextChannel)Context.Channel, usr.VoiceChannel, item.Query, true, item.ProviderType).ConfigureAwait(false); await music.QueueSong(usr, (ITextChannel)Context.Channel, usr.VoiceChannel, item.Query, true, item.ProviderType).ConfigureAwait(false);
} }
catch (SongNotFoundException) { } catch (SongNotFoundException) { }
catch { break; } catch { break; }
@ -764,7 +768,7 @@ namespace NadekoBot.Modules.Music
public async Task Goto(int time) public async Task Goto(int time)
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel) if (((IGuildUser)Context.User).VoiceChannel != musicPlayer.PlaybackVoiceChannel)
return; return;
@ -799,7 +803,7 @@ namespace NadekoBot.Modules.Music
public async Task Autoplay() public async Task Autoplay()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
return; return;
if (!musicPlayer.ToggleAutoplay()) if (!musicPlayer.ToggleAutoplay())
@ -814,7 +818,7 @@ namespace NadekoBot.Modules.Music
public async Task SetMusicChannel() public async Task SetMusicChannel()
{ {
MusicPlayer musicPlayer; MusicPlayer musicPlayer;
if (!MusicPlayers.TryGetValue(Context.Guild.Id, out musicPlayer)) if ((musicPlayer = music.GetPlayer(Context.Guild.Id)) == null)
{ {
await ReplyErrorLocalized("no_player").ConfigureAwait(false); await ReplyErrorLocalized("no_player").ConfigureAwait(false);
return; return;
@ -825,167 +829,5 @@ namespace NadekoBot.Modules.Music
await ReplyConfirmLocalized("set_music_channel").ConfigureAwait(false); await ReplyConfirmLocalized("set_music_channel").ConfigureAwait(false);
} }
public async Task QueueSong(IGuildUser queuer, ITextChannel textCh, IVoiceChannel voiceCh, string query, bool silent = false, MusicType musicType = MusicType.Normal)
{
if (voiceCh == null || voiceCh.Guild != textCh.Guild)
{
if (!silent)
await textCh.SendErrorAsync(GetText("must_be_in_voice")).ConfigureAwait(false);
throw new ArgumentNullException(nameof(voiceCh));
}
if (string.IsNullOrWhiteSpace(query) || query.Length < 3)
throw new ArgumentException("Invalid song query.", nameof(query));
var musicPlayer = MusicPlayers.GetOrAdd(textCh.Guild.Id, server =>
{
float vol;// SpecificConfigurations.Default.Of(server.Id).DefaultMusicVolume;
using (var uow = DbHandler.UnitOfWork())
{
vol = uow.GuildConfigs.For(textCh.Guild.Id, set => set).DefaultMusicVolume;
}
var mp = new MusicPlayer(voiceCh, textCh, vol);
IUserMessage playingMessage = null;
IUserMessage lastFinishedMessage = null;
mp.OnCompleted += async (s, song) =>
{
try
{
lastFinishedMessage?.DeleteAfter(0);
try
{
lastFinishedMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithAuthor(eab => eab.WithName(GetText("finished_song")).WithMusicIcon())
.WithDescription(song.PrettyName)
.WithFooter(ef => ef.WithText(song.PrettyInfo)))
.ConfigureAwait(false);
}
catch
{
// ignored
}
if (mp.Autoplay && mp.Playlist.Count == 0 && song.SongInfo.ProviderType == MusicType.Normal)
{
var relatedVideos = (await NadekoBot.Google.GetRelatedVideosAsync(song.SongInfo.Query, 4)).ToList();
if(relatedVideos.Count > 0)
await QueueSong(await queuer.Guild.GetCurrentUserAsync(),
textCh,
voiceCh,
relatedVideos[new NadekoRandom().Next(0, relatedVideos.Count)],
true).ConfigureAwait(false);
}
}
catch
{
// ignored
}
};
mp.OnStarted += async (player, song) =>
{
try { await mp.UpdateSongDurationsAsync().ConfigureAwait(false); }
catch
{
// ignored
}
var sender = player;
if (sender == null)
return;
try
{
playingMessage?.DeleteAfter(0);
playingMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithAuthor(eab => eab.WithName(GetText("playing_song")).WithMusicIcon())
.WithDescription(song.PrettyName)
.WithFooter(ef => ef.WithText(song.PrettyInfo)))
.ConfigureAwait(false);
}
catch
{
// ignored
}
};
mp.OnPauseChanged += async (paused) =>
{
try
{
IUserMessage msg;
if (paused)
msg = await mp.OutputTextChannel.SendConfirmAsync(GetText("paused")).ConfigureAwait(false);
else
msg = await mp.OutputTextChannel.SendConfirmAsync(GetText("resumed")).ConfigureAwait(false);
msg?.DeleteAfter(10);
}
catch
{
// ignored
}
};
mp.SongRemoved += async (song, index) =>
{
try
{
var embed = new EmbedBuilder()
.WithAuthor(eab => eab.WithName(GetText("removed_song") + " #" + (index + 1)).WithMusicIcon())
.WithDescription(song.PrettyName)
.WithFooter(ef => ef.WithText(song.PrettyInfo))
.WithErrorColor();
await mp.OutputTextChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch
{
// ignored
}
};
return mp;
});
Song resolvedSong;
try
{
musicPlayer.ThrowIfQueueFull();
resolvedSong = await SongHandler.ResolveSong(query, musicType).ConfigureAwait(false);
if (resolvedSong == null)
throw new SongNotFoundException();
musicPlayer.AddSong(resolvedSong, queuer.Username);
}
catch (PlaylistFullException)
{
try
{
await textCh.SendConfirmAsync(GetText("queue_full", musicPlayer.MaxQueueSize));
}
catch
{
// ignored
}
throw;
}
if (!silent)
{
try
{
//var queuedMessage = await textCh.SendConfirmAsync($"🎵 Queued **{resolvedSong.SongInfo.Title}** at `#{musicPlayer.Playlist.Count + 1}`").ConfigureAwait(false);
var queuedMessage = await textCh.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithAuthor(eab => eab.WithName(GetText("queued_song") + " #" + (musicPlayer.Playlist.Count + 1)).WithMusicIcon())
.WithDescription($"{resolvedSong.PrettyName}\n{GetText("queue")} ")
.WithThumbnailUrl(resolvedSong.Thumbnail)
.WithFooter(ef => ef.WithText(resolvedSong.PrettyProvider)))
.ConfigureAwait(false);
queuedMessage?.DeleteAfter(10);
}
catch
{
// ignored
} // if queued message sending fails, don't attempt to delete it
}
}
} }
} }

View File

@ -418,8 +418,8 @@ namespace NadekoBot.Modules.Utility
NadekoBot.Client.Guilds.Count, stats.TextChannels, stats.VoiceChannels)).WithIsInline(true)) NadekoBot.Client.Guilds.Count, stats.TextChannels, stats.VoiceChannels)).WithIsInline(true))
#if !GLOBAL_NADEKO #if !GLOBAL_NADEKO
.WithFooter(efb => efb.WithText(GetText("stats_songs", .WithFooter(efb => efb.WithText(GetText("stats_songs",
Music.Music.MusicPlayers.Count(mp => mp.Value.CurrentSong != null), NadekoBot.MusicService.MusicPlayers.Count(mp => mp.Value.CurrentSong != null),
Music.Music.MusicPlayers.Sum(mp => mp.Value.Playlist.Count)))) NadekoBot.MusicService.MusicPlayers.Sum(mp => mp.Value.Playlist.Count))))
#endif #endif
); );
} }

View File

@ -18,6 +18,7 @@ using System.Diagnostics;
using NadekoBot.Modules.Music; using NadekoBot.Modules.Music;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using System.Threading; using System.Threading;
using NadekoBot.Services.Music;
namespace NadekoBot namespace NadekoBot
{ {
@ -49,6 +50,7 @@ namespace NadekoBot
//services //services
//todo DI in the future //todo DI in the future
public static GreetSettingsService GreetSettingsService { get; private set; } public static GreetSettingsService GreetSettingsService { get; private set; }
public static MusicService MusicService { get; private set; }
static NadekoBot() static NadekoBot()
{ {

View File

@ -1,4 +1,5 @@
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
@ -36,6 +37,41 @@ namespace NadekoBot.Services
return settings; return settings;
} }
public async Task<bool> SetSettings(ulong guildId, GreetSettings settings)
{
if (settings.AutoDeleteByeMessagesTimer > 600 ||
settings.AutoDeleteByeMessagesTimer < 0 ||
settings.AutoDeleteGreetMessagesTimer > 600 ||
settings.AutoDeleteGreetMessagesTimer < 0)
{
return false;
}
using (var uow = DbHandler.UnitOfWork())
{
var conf = uow.GuildConfigs.For(guildId, set => set);
conf.DmGreetMessageText = settings.DmGreetMessageText?.SanitizeMentions();
conf.ChannelGreetMessageText = settings.ChannelGreetMessageText?.SanitizeMentions();
conf.ChannelByeMessageText = settings.ChannelByeMessageText?.SanitizeMentions();
conf.AutoDeleteGreetMessagesTimer = settings.AutoDeleteGreetMessagesTimer;
conf.AutoDeleteGreetMessages = settings.AutoDeleteGreetMessagesTimer > 0;
conf.AutoDeleteByeMessagesTimer = settings.AutoDeleteByeMessagesTimer;
conf.AutoDeleteByeMessages = settings.AutoDeleteByeMessagesTimer > 0;
conf.GreetMessageChannelId = settings.GreetMessageChannelId;
conf.ByeMessageChannelId = settings.ByeMessageChannelId;
conf.SendChannelGreetMessage = settings.SendChannelGreetMessage;
conf.SendChannelByeMessage = settings.SendChannelByeMessage;
await uow.CompleteAsync().ConfigureAwait(false);
}
return true;
}
public async Task<bool> SetGreet(ulong guildId, ulong channelId, bool? value = null) public async Task<bool> SetGreet(ulong guildId, ulong channelId, bool? value = null)
{ {
bool enabled; bool enabled;

View File

@ -11,7 +11,6 @@ using NLog;
namespace NadekoBot.Modules.Music.Classes namespace NadekoBot.Modules.Music.Classes
{ {
public enum MusicType public enum MusicType
{ {
Radio, Radio,

View File

@ -0,0 +1,213 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NadekoBot.Modules.Music.Classes;
using Discord;
using NadekoBot.Extensions;
using NadekoBot.Modules;
namespace NadekoBot.Services.Music
{
public class MusicService
{
private readonly IGoogleApiService _google;
public ConcurrentDictionary<ulong, MusicPlayer> MusicPlayers { get; } = new ConcurrentDictionary<ulong, MusicPlayer>();
public MusicService(IGoogleApiService google)
{
_google = google;
}
public MusicPlayer GetPlayer(ulong guildId)
{
MusicPlayers.TryGetValue(guildId, out var player);
return player;
}
public MusicPlayer GetOrCreatePlayer(ulong guildId, IVoiceChannel voiceCh, ITextChannel textCh)
{
string GetText(string text, params object[] replacements) =>
NadekoTopLevelModule.GetTextStatic(text, NadekoBot.Localization.GetCultureInfo(textCh.Guild), nameof(Modules.Music.Music).ToLowerInvariant(), replacements);
return MusicPlayers.GetOrAdd(guildId, server =>
{
float vol;// SpecificConfigurations.Default.Of(server.Id).DefaultMusicVolume;
using (var uow = DbHandler.UnitOfWork())
{
//todo move to cached variable
vol = uow.GuildConfigs.For(guildId, set => set).DefaultMusicVolume;
}
var mp = new MusicPlayer(voiceCh, textCh, vol);
IUserMessage playingMessage = null;
IUserMessage lastFinishedMessage = null;
mp.OnCompleted += async (s, song) =>
{
try
{
lastFinishedMessage?.DeleteAfter(0);
try
{
lastFinishedMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithAuthor(eab => eab.WithName(GetText("finished_song")).WithMusicIcon())
.WithDescription(song.PrettyName)
.WithFooter(ef => ef.WithText(song.PrettyInfo)))
.ConfigureAwait(false);
}
catch
{
// ignored
}
if (mp.Autoplay && mp.Playlist.Count == 0 && song.SongInfo.ProviderType == MusicType.Normal)
{
var relatedVideos = (await NadekoBot.Google.GetRelatedVideosAsync(song.SongInfo.Query, 4)).ToList();
if (relatedVideos.Count > 0)
await QueueSong(await textCh.Guild.GetCurrentUserAsync(),
textCh,
voiceCh,
relatedVideos[new NadekoRandom().Next(0, relatedVideos.Count)],
true).ConfigureAwait(false);
}
}
catch
{
// ignored
}
};
mp.OnStarted += async (player, song) =>
{
try { await mp.UpdateSongDurationsAsync().ConfigureAwait(false); }
catch
{
// ignored
}
var sender = player;
if (sender == null)
return;
try
{
playingMessage?.DeleteAfter(0);
playingMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithAuthor(eab => eab.WithName(GetText("playing_song")).WithMusicIcon())
.WithDescription(song.PrettyName)
.WithFooter(ef => ef.WithText(song.PrettyInfo)))
.ConfigureAwait(false);
}
catch
{
// ignored
}
};
mp.OnPauseChanged += async (paused) =>
{
try
{
IUserMessage msg;
if (paused)
msg = await mp.OutputTextChannel.SendConfirmAsync(GetText("paused")).ConfigureAwait(false);
else
msg = await mp.OutputTextChannel.SendConfirmAsync(GetText("resumed")).ConfigureAwait(false);
msg?.DeleteAfter(10);
}
catch
{
// ignored
}
};
mp.SongRemoved += async (song, index) =>
{
try
{
var embed = new EmbedBuilder()
.WithAuthor(eab => eab.WithName(GetText("removed_song") + " #" + (index + 1)).WithMusicIcon())
.WithDescription(song.PrettyName)
.WithFooter(ef => ef.WithText(song.PrettyInfo))
.WithErrorColor();
await mp.OutputTextChannel.EmbedAsync(embed).ConfigureAwait(false);
}
catch
{
// ignored
}
};
return mp;
});
}
public async Task QueueSong(IGuildUser queuer, ITextChannel textCh, IVoiceChannel voiceCh, string query, bool silent = false, MusicType musicType = MusicType.Normal)
{
string GetText(string text, params object[] replacements) =>
NadekoTopLevelModule.GetTextStatic(text, NadekoBot.Localization.GetCultureInfo(textCh.Guild), nameof(Modules.Music.Music).ToLowerInvariant(), replacements);
if (voiceCh == null || voiceCh.Guild != textCh.Guild)
{
if (!silent)
await textCh.SendErrorAsync(GetText("must_be_in_voice")).ConfigureAwait(false);
throw new ArgumentNullException(nameof(voiceCh));
}
if (string.IsNullOrWhiteSpace(query) || query.Length < 3)
throw new ArgumentException("Invalid song query.", nameof(query));
var musicPlayer = GetOrCreatePlayer(textCh.Guild.Id, voiceCh, textCh);
Song resolvedSong;
try
{
musicPlayer.ThrowIfQueueFull();
resolvedSong = await SongHandler.ResolveSong(query, musicType).ConfigureAwait(false);
if (resolvedSong == null)
throw new SongNotFoundException();
musicPlayer.AddSong(resolvedSong, queuer.Username);
}
catch (PlaylistFullException)
{
try
{
await textCh.SendConfirmAsync(GetText("queue_full", musicPlayer.MaxQueueSize));
}
catch
{
// ignored
}
throw;
}
if (!silent)
{
try
{
//var queuedMessage = await textCh.SendConfirmAsync($"🎵 Queued **{resolvedSong.SongInfo.Title}** at `#{musicPlayer.Playlist.Count + 1}`").ConfigureAwait(false);
var queuedMessage = await textCh.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithAuthor(eab => eab.WithName(GetText("queued_song") + " #" + (musicPlayer.Playlist.Count + 1)).WithMusicIcon())
.WithDescription($"{resolvedSong.PrettyName}\n{GetText("queue")} ")
.WithThumbnailUrl(resolvedSong.Thumbnail)
.WithFooter(ef => ef.WithText(resolvedSong.PrettyProvider)))
.ConfigureAwait(false);
queuedMessage?.DeleteAfter(10);
}
catch
{
// ignored
} // if queued message sending fails, don't attempt to delete it
}
}
internal void DestroyPlayer(ulong id)
{
throw new NotImplementedException();
}
}
}