2017-07-20 03:10:39 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2017-05-14 19:00:35 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using NLog;
|
|
|
|
|
using System.IO;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-07-01 06:15:58 +00:00
|
|
|
|
using Discord.Commands;
|
2017-07-01 19:22:11 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common;
|
2017-07-06 17:20:00 +00:00
|
|
|
|
using NadekoBot.Services.Impl;
|
2017-07-17 02:37:51 +00:00
|
|
|
|
using NadekoBot.Services;
|
|
|
|
|
using NadekoBot.Modules.Music.Common;
|
|
|
|
|
using NadekoBot.Modules.Music.Common.Exceptions;
|
|
|
|
|
using NadekoBot.Modules.Music.Common.SongResolver;
|
2017-05-14 19:00:35 +00:00
|
|
|
|
|
2017-07-17 02:37:51 +00:00
|
|
|
|
namespace NadekoBot.Modules.Music.Services
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
public class MusicService : INService, IUnloadableService
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
2017-05-24 04:43:00 +00:00
|
|
|
|
public const string MusicDataPath = "data/musicdata";
|
|
|
|
|
|
2017-05-14 19:00:35 +00:00
|
|
|
|
private readonly IGoogleApiService _google;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
private readonly NadekoStrings _strings;
|
|
|
|
|
private readonly ILocalization _localization;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
private readonly Logger _log;
|
|
|
|
|
private readonly SoundCloudApiService _sc;
|
|
|
|
|
private readonly IBotCredentials _creds;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly ConcurrentDictionary<ulong, float> _defaultVolumes;
|
2017-07-01 19:22:11 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
2017-05-14 19:00:35 +00:00
|
|
|
|
public ConcurrentDictionary<ulong, MusicPlayer> MusicPlayers { get; } = new ConcurrentDictionary<ulong, MusicPlayer>();
|
|
|
|
|
|
2017-07-01 19:22:11 +00:00
|
|
|
|
public MusicService(DiscordSocketClient client, IGoogleApiService google,
|
2017-07-01 06:15:58 +00:00
|
|
|
|
NadekoStrings strings, ILocalization localization, DbService db,
|
2017-05-29 04:13:22 +00:00
|
|
|
|
SoundCloudApiService sc, IBotCredentials creds, IEnumerable<GuildConfig> gcs)
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
2017-07-01 19:22:11 +00:00
|
|
|
|
_client = client;
|
2017-05-14 19:00:35 +00:00
|
|
|
|
_google = google;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_strings = strings;
|
|
|
|
|
_localization = localization;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
_db = db;
|
|
|
|
|
_sc = sc;
|
|
|
|
|
_creds = creds;
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
_client.LeftGuild += _client_LeftGuild;
|
|
|
|
|
|
2017-05-24 04:43:00 +00:00
|
|
|
|
try { Directory.Delete(MusicDataPath, true); } catch { }
|
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
_defaultVolumes = new ConcurrentDictionary<ulong, float>(gcs.ToDictionary(x => x.GuildId, x => x.DefaultMusicVolume));
|
|
|
|
|
|
2017-05-24 04:43:00 +00:00
|
|
|
|
Directory.CreateDirectory(MusicDataPath);
|
2017-10-04 22:51:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task Unload()
|
|
|
|
|
{
|
|
|
|
|
_client.LeftGuild -= _client_LeftGuild;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2017-07-07 08:30:19 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
private Task _client_LeftGuild(SocketGuild arg)
|
|
|
|
|
{
|
|
|
|
|
var t = DestroyPlayer(arg.Id);
|
|
|
|
|
return Task.CompletedTask;
|
2017-05-14 19:00:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-01 06:15:58 +00:00
|
|
|
|
public float GetDefaultVolume(ulong guildId)
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
2017-07-01 06:15:58 +00:00
|
|
|
|
return _defaultVolumes.GetOrAdd(guildId, (id) =>
|
|
|
|
|
{
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
return uow.GuildConfigs.For(guildId, set => set).DefaultMusicVolume;
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-05-14 19:00:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-01 06:15:58 +00:00
|
|
|
|
public Task<MusicPlayer> GetOrCreatePlayer(ICommandContext context)
|
|
|
|
|
{
|
|
|
|
|
var gUsr = (IGuildUser)context.User;
|
|
|
|
|
var txtCh = (ITextChannel)context.Channel;
|
|
|
|
|
var vCh = gUsr.VoiceChannel;
|
|
|
|
|
return GetOrCreatePlayer(context.Guild.Id, vCh, txtCh);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<MusicPlayer> GetOrCreatePlayer(ulong guildId, IVoiceChannel voiceCh, ITextChannel textCh)
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
|
|
|
|
string GetText(string text, params object[] replacements) =>
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_strings.GetText(text, _localization.GetCultureInfo(textCh.Guild), "Music".ToLowerInvariant(), replacements);
|
2017-05-14 19:00:35 +00:00
|
|
|
|
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Checks");
|
2017-07-01 06:15:58 +00:00
|
|
|
|
if (voiceCh == null || voiceCh.Guild != textCh.Guild)
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
2017-07-01 06:15:58 +00:00
|
|
|
|
if (textCh != null)
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
2017-07-01 06:15:58 +00:00
|
|
|
|
await textCh.SendErrorAsync(GetText("must_be_in_voice")).ConfigureAwait(false);
|
|
|
|
|
}
|
2017-07-20 03:10:39 +00:00
|
|
|
|
throw new NotInVoiceChannelException();
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Get or add");
|
2017-07-01 06:15:58 +00:00
|
|
|
|
return MusicPlayers.GetOrAdd(guildId, _ =>
|
|
|
|
|
{
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Getting default volume");
|
2017-07-01 06:15:58 +00:00
|
|
|
|
var vol = GetDefaultVolume(guildId);
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Creating musicplayer instance");
|
2017-07-04 13:38:19 +00:00
|
|
|
|
var mp = new MusicPlayer(this, _google, voiceCh, textCh, vol);
|
2017-07-01 06:15:58 +00:00
|
|
|
|
|
2017-05-14 19:00:35 +00:00
|
|
|
|
IUserMessage playingMessage = null;
|
|
|
|
|
IUserMessage lastFinishedMessage = null;
|
2017-07-05 16:39:54 +00:00
|
|
|
|
|
|
|
|
|
_log.Info("Subscribing");
|
2017-05-14 19:00:35 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
mp.OnStarted += async (player, song) =>
|
|
|
|
|
{
|
2017-07-01 06:15:58 +00:00
|
|
|
|
//try { await mp.UpdateSongDurationsAsync().ConfigureAwait(false); }
|
|
|
|
|
//catch
|
|
|
|
|
//{
|
|
|
|
|
// // ignored
|
|
|
|
|
//}
|
2017-05-14 19:00:35 +00:00
|
|
|
|
var sender = player;
|
|
|
|
|
if (sender == null)
|
|
|
|
|
return;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
playingMessage?.DeleteAfter(0);
|
|
|
|
|
|
|
|
|
|
playingMessage = await mp.OutputTextChannel.EmbedAsync(new EmbedBuilder().WithOkColor()
|
2017-07-03 10:40:12 +00:00
|
|
|
|
.WithAuthor(eab => eab.WithName(GetText("playing_song", song.Index + 1)).WithMusicIcon())
|
|
|
|
|
.WithDescription(song.Song.PrettyName)
|
|
|
|
|
.WithFooter(ef => ef.WithText(mp.PrettyVolume + " | " + song.Song.PrettyInfo)))
|
2017-05-14 19:00:35 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-07-01 06:15:58 +00:00
|
|
|
|
mp.OnPauseChanged += async (player, paused) =>
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-07-05 16:39:54 +00:00
|
|
|
|
_log.Info("Done creating");
|
2017-05-14 19:00:35 +00:00
|
|
|
|
return mp;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-01 19:22:11 +00:00
|
|
|
|
public MusicPlayer GetPlayerOrDefault(ulong guildId)
|
|
|
|
|
{
|
|
|
|
|
if (MusicPlayers.TryGetValue(guildId, out var mp))
|
|
|
|
|
return mp;
|
|
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-08 23:37:17 +00:00
|
|
|
|
public async Task TryQueueRelatedSongAsync(SongInfo song, ITextChannel txtCh, IVoiceChannel vch)
|
2017-07-01 19:22:11 +00:00
|
|
|
|
{
|
2017-07-08 23:37:17 +00:00
|
|
|
|
var related = (await _google.GetRelatedVideosAsync(song.VideoId, 4)).ToArray();
|
2017-07-01 19:22:11 +00:00
|
|
|
|
if (!related.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-07-02 17:00:25 +00:00
|
|
|
|
var si = await ResolveSong(related[new NadekoRandom().Next(related.Length)], _client.CurrentUser.ToString(), MusicType.YouTube);
|
|
|
|
|
if (si == null)
|
|
|
|
|
throw new SongNotFoundException();
|
2017-07-01 19:22:11 +00:00
|
|
|
|
var mp = await GetOrCreatePlayer(txtCh.GuildId, vch, txtCh);
|
|
|
|
|
mp.Enqueue(si);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-02 17:00:25 +00:00
|
|
|
|
public async Task<SongInfo> ResolveSong(string query, string queuerName, MusicType? musicType = null)
|
2017-05-14 19:00:35 +00:00
|
|
|
|
{
|
2017-07-01 06:15:58 +00:00
|
|
|
|
query.ThrowIfNull(nameof(query));
|
2017-05-14 19:00:35 +00:00
|
|
|
|
|
2017-07-11 01:16:56 +00:00
|
|
|
|
ISongResolverFactory resolverFactory = new SongResolverFactory(_sc);
|
|
|
|
|
var strategy = await resolverFactory.GetResolveStrategy(query, musicType);
|
|
|
|
|
var sinfo = await strategy.ResolveSong(query);
|
2017-07-02 17:00:25 +00:00
|
|
|
|
|
2017-07-11 01:16:56 +00:00
|
|
|
|
if (sinfo == null)
|
2017-07-02 17:00:25 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
2017-07-11 01:16:56 +00:00
|
|
|
|
sinfo.QueuerName = queuerName;
|
2017-07-02 17:00:25 +00:00
|
|
|
|
|
2017-07-11 01:16:56 +00:00
|
|
|
|
return sinfo;
|
2017-07-02 17:00:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 10:23:37 +00:00
|
|
|
|
public async Task DestroyAllPlayers()
|
|
|
|
|
{
|
|
|
|
|
foreach (var key in MusicPlayers.Keys)
|
|
|
|
|
{
|
|
|
|
|
await DestroyPlayer(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-01 06:16:06 +00:00
|
|
|
|
public async Task DestroyPlayer(ulong id)
|
2017-05-24 04:43:00 +00:00
|
|
|
|
{
|
2017-07-01 06:15:58 +00:00
|
|
|
|
if (MusicPlayers.TryRemove(id, out var mp))
|
2017-07-01 06:16:06 +00:00
|
|
|
|
await mp.Destroy();
|
2017-05-24 04:43:00 +00:00
|
|
|
|
}
|
2017-05-14 19:00:35 +00:00
|
|
|
|
}
|
2017-07-01 06:15:58 +00:00
|
|
|
|
}
|