From 4f898dc653636553d419b422a54bc5b214d0e408 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 7 Apr 2016 12:30:58 +0200 Subject: [PATCH] Loading almost done --- NadekoBot/Classes/DBHandler.cs | 8 +++ NadekoBot/Classes/Music/Song.cs | 6 ++ NadekoBot/Classes/_DataModels/SongInfo.cs | 6 +- NadekoBot/Modules/Music.cs | 81 ++++++++++++----------- 4 files changed, 59 insertions(+), 42 deletions(-) diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index 11d33e18..832d933d 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -44,6 +44,14 @@ namespace NadekoBot.Classes } } + internal IList FindAll(Expression> p) where T : IDataModel, new() + { + using (var conn = new SQLiteConnection(FilePath)) + { + return conn.Table().Where(p).ToList(); + } + } + internal void DeleteAll() where T : IDataModel { using (var conn = new SQLiteConnection(FilePath)) diff --git a/NadekoBot/Classes/Music/Song.cs b/NadekoBot/Classes/Music/Song.cs index dab40651..7aaa8601 100644 --- a/NadekoBot/Classes/Music/Song.cs +++ b/NadekoBot/Classes/Music/Song.cs @@ -16,6 +16,10 @@ namespace NadekoBot.Classes.Music { public string Provider { get; internal set; } public MusicType ProviderType { get; internal set; } + /// + /// Will be set only if the providertype is normal + /// + public string Query { get; internal set; } public string Title { get; internal set; } public string Uri { get; internal set; } } @@ -233,6 +237,7 @@ namespace NadekoBot.Classes.Music Provider = "SoundCloud", Uri = svideo.StreamLink, ProviderType = musicType, + Query = query, }); } var links = await SearchHelper.FindYoutubeUrlByKeywords(query); @@ -252,6 +257,7 @@ namespace NadekoBot.Classes.Music Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube" Provider = "YouTube", Uri = video.Uri, + Query = query, ProviderType = musicType, }); } diff --git a/NadekoBot/Classes/_DataModels/SongInfo.cs b/NadekoBot/Classes/_DataModels/SongInfo.cs index 324885b5..8570ea53 100644 --- a/NadekoBot/Classes/_DataModels/SongInfo.cs +++ b/NadekoBot/Classes/_DataModels/SongInfo.cs @@ -1,13 +1,11 @@ -using SQLite; - -namespace NadekoBot.Classes._DataModels +namespace NadekoBot.Classes._DataModels { internal class SongInfo : IDataModel { public string Provider { get; internal set; } public int ProviderType { get; internal set; } public string Title { get; internal set; } - [Unique] public string Uri { get; internal set; } + public string Query { get; internal set; } } } diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index 1e39a14a..26e38470 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -2,6 +2,7 @@ using Discord.Commands; using Discord.Modules; using NadekoBot.Classes; +using NadekoBot.Classes._DataModels; using NadekoBot.Classes.Music; using NadekoBot.Classes.Permissions; using NadekoBot.Extensions; @@ -10,7 +11,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Timer = System.Timers.Timer; namespace NadekoBot.Modules { @@ -20,24 +20,8 @@ namespace NadekoBot.Modules public static ConcurrentDictionary MusicPlayers = new ConcurrentDictionary(); public static ConcurrentDictionary DefaultMusicVolumes = new ConcurrentDictionary(); - private readonly Timer setgameTimer = new Timer(); - - private bool setgameEnabled = false; - public Music() { - - setgameTimer.Interval = 20000; - setgameTimer.Elapsed += (s, e) => - { - try - { - var num = MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null); - NadekoBot.Client.SetGame($"{num} songs".SnPl(num) + $", {MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count())} queued"); - } - catch { } - }; - // ready for 1.0 //NadekoBot.Client.UserUpdated += (s, e) => //{ @@ -449,10 +433,11 @@ namespace NadekoBot.Modules Provider = s.SongInfo.Provider, ProviderType = (int)s.SongInfo.ProviderType, Title = s.SongInfo.Title, - Uri = s.SongInfo.Uri + Uri = s.SongInfo.Uri, + Query = s.SongInfo.Query }); - var playlist = new Classes._DataModels.MusicPlaylist + var playlist = new MusicPlaylist { CreatorId = (long)e.User.Id, CreatorName = e.User.Name, @@ -461,7 +446,7 @@ namespace NadekoBot.Modules DbHandler.Instance.SaveAll(songInfos); DbHandler.Instance.Save(playlist); - DbHandler.Instance.InsertMany(songInfos.Select(s => new Classes._DataModels.PlaylistSongInfo + DbHandler.Instance.InsertMany(songInfos.Select(s => new PlaylistSongInfo { PlaylistId = playlist.Id, SongInfoId = s.Id @@ -472,39 +457,59 @@ namespace NadekoBot.Modules }); cgb.CreateCommand("load") - .Description("Loads a playlist under a certain name. Name must be no longer than 20 characters and mustn't contain dashes.\n**Usage**: `!m load classical1`") + .Description("Loads a playlist under a certain name. \n**Usage**: `!m load classical-1`") .Parameter("name", ParameterType.Unparsed) .Do(async e => { + var voiceCh = e.User.VoiceChannel; + var textCh = e.Channel; + if (voiceCh == null || voiceCh.Server != textCh.Server) + { + await textCh.SendMessage("💢 You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining."); + return; + } var name = e.GetArg("name")?.Trim(); - if (string.IsNullOrWhiteSpace(name) || - name.Length > 20 || - name.Contains("-")) + if (string.IsNullOrWhiteSpace(name)) return; - MusicPlayer musicPlayer; - if (!MusicPlayers.TryGetValue(e.Server, out musicPlayer)) + var parts = name.Split('-'); + if (parts.Length != 2) + return; + var playlistName = parts[0]; + + int playlistNumber; + if (!int.TryParse(parts[1], out playlistNumber)) return; - //to avoid concurrency issues - var currentPlaylist = new List(musicPlayer.Playlist); + var playlist = DbHandler.Instance.FindOne( + p => p.Name.ToLower() == name); - if (!currentPlaylist.Any()) + if (playlist == null) + { + await e.Channel.SendMessage("Can't find playlist under that name."); return; + } + var psis = DbHandler.Instance.FindAll(psi => + psi.PlaylistId == playlist.Id); + var songInfos = psis.Select(psi => DbHandler.Instance + .FindOne(si => si.Id == psi.SongInfoId)); - + await e.Channel.SendMessage($"`Attempting to load {songInfos.Count()} songs`"); + foreach (var si in songInfos) + { + try + { + await QueueSong(textCh, voiceCh, si.Query, true, (MusicType)si.ProviderType); + } + catch (Exception ex) + { + Console.WriteLine($"Failed QueueSong in load playlist. {ex}"); + } + } }); - - //cgb.CreateCommand("debug") - // .Description("Does something magical. **BOT OWNER ONLY**") - // .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) - // .Do(e => { - // var inactivePlayers = - // Console.WriteLine(""); - // }); }); }