From 873037a238b921324118835c3c217ae55ad6ec30 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 9 Apr 2016 15:21:19 +0200 Subject: [PATCH] !m save and !m load work properly now, Music is much less spammy --- NadekoBot/Classes/DBHandler.cs | 9 +-- NadekoBot/Classes/Music/Song.cs | 13 ++-- NadekoBot/Classes/_DataModels/IDataModel.cs | 8 ++- NadekoBot/Classes/_DataModels/SongInfo.cs | 5 +- .../Modules/Administration/Commands/Remind.cs | 2 +- NadekoBot/Modules/Music.cs | 69 +++++++++++++++---- NadekoBot/Modules/Pokemon/PokemonModule.cs | 10 +-- NadekoBot/NadekoBot.cs | 2 +- NadekoBot/bin/Debug/credentials_example.json | 1 + 9 files changed, 81 insertions(+), 38 deletions(-) diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index 832d933d..eb67c6a6 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -143,13 +143,10 @@ namespace NadekoBot.Classes { using (var conn = new SQLiteConnection(FilePath)) { - conn.RunInTransaction(() => + foreach (var o in ocol) { - foreach (var o in ocol) - { - conn.InsertOrReplace(o, typeof(T)); - } - }); + conn.InsertOrReplace(o, typeof(T)); + } } } diff --git a/NadekoBot/Classes/Music/Song.cs b/NadekoBot/Classes/Music/Song.cs index 7aaa8601..e4cac75c 100644 --- a/NadekoBot/Classes/Music/Song.cs +++ b/NadekoBot/Classes/Music/Song.cs @@ -83,10 +83,7 @@ namespace NadekoBot.Classes.Music else await Task.Delay(100, cancelToken); else - { attempt = 0; - await Task.Delay(5, cancelToken); - } await songBuffer.WriteAsync(buffer, read, cancelToken); if (songBuffer.ContentLength > 2.MB()) prebufferingComplete = true; @@ -218,6 +215,7 @@ namespace NadekoBot.Classes.Music Title = Path.GetFileNameWithoutExtension(query), Provider = "Local File", ProviderType = musicType, + Query = query, }); case MusicType.Radio: return new Song(new SongInfo @@ -226,6 +224,7 @@ namespace NadekoBot.Classes.Music Title = $"{query}", Provider = "Radio Stream", ProviderType = musicType, + Query = query }); } if (SoundCloud.Default.IsSoundCloudLink(query)) @@ -240,10 +239,10 @@ namespace NadekoBot.Classes.Music Query = query, }); } - var links = await SearchHelper.FindYoutubeUrlByKeywords(query); - if (links == String.Empty) + var link = await SearchHelper.FindYoutubeUrlByKeywords(query); + if (link == String.Empty) throw new OperationCanceledException("Not a valid youtube query."); - var allVideos = await Task.Factory.StartNew(async () => await YouTube.Default.GetAllVideosAsync(links)).Unwrap(); + var allVideos = await Task.Factory.StartNew(async () => await YouTube.Default.GetAllVideosAsync(link)).Unwrap(); var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio); var video = videos .Where(v => v.AudioBitrate < 192) @@ -257,7 +256,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, + Query = link, ProviderType = musicType, }); } diff --git a/NadekoBot/Classes/_DataModels/IDataModel.cs b/NadekoBot/Classes/_DataModels/IDataModel.cs index bb5a5070..12684f52 100644 --- a/NadekoBot/Classes/_DataModels/IDataModel.cs +++ b/NadekoBot/Classes/_DataModels/IDataModel.cs @@ -1,10 +1,12 @@ using SQLite; using System; -namespace NadekoBot.Classes._DataModels { - internal abstract class IDataModel { +namespace NadekoBot.Classes._DataModels +{ + internal abstract class IDataModel + { [PrimaryKey, AutoIncrement] - public int Id { get; set; } + public int? Id { get; set; } [Newtonsoft.Json.JsonProperty("createdAt")] public DateTime DateAdded { get; set; } = DateTime.Now; public IDataModel() { } diff --git a/NadekoBot/Classes/_DataModels/SongInfo.cs b/NadekoBot/Classes/_DataModels/SongInfo.cs index 8570ea53..cf4239fa 100644 --- a/NadekoBot/Classes/_DataModels/SongInfo.cs +++ b/NadekoBot/Classes/_DataModels/SongInfo.cs @@ -1,4 +1,6 @@ -namespace NadekoBot.Classes._DataModels +using SQLite; + +namespace NadekoBot.Classes._DataModels { internal class SongInfo : IDataModel { @@ -6,6 +8,7 @@ public int ProviderType { get; internal set; } public string Title { get; internal set; } public string Uri { get; internal set; } + [Unique] public string Query { get; internal set; } } } diff --git a/NadekoBot/Modules/Administration/Commands/Remind.cs b/NadekoBot/Modules/Administration/Commands/Remind.cs index cbbe126d..3dfa0afd 100644 --- a/NadekoBot/Modules/Administration/Commands/Remind.cs +++ b/NadekoBot/Modules/Administration/Commands/Remind.cs @@ -63,7 +63,7 @@ namespace NadekoBot.Modules.Administration.Commands } finally { - DbHandler.Instance.Delete(r.Id); + DbHandler.Instance.Delete(r.Id.Value); t.Stop(); t.Dispose(); } diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index 2f677b12..7404ddd8 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -49,7 +49,7 @@ namespace NadekoBot.Modules manager.CreateCommands(Prefix, cgb => { - cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance); + cgb.AddCheck(PermissionChecker.Instance); commands.ForEach(cmd => cmd.Init(cgb)); @@ -113,6 +113,11 @@ namespace NadekoBot.Modules .Do(async e => { await QueueSong(e.Channel, e.User.VoiceChannel, e.GetArg("query")); + if (e.Server.CurrentUser.GetPermissions(e.Channel).ManageMessages) + { + await Task.Delay(10000); + await e.Message.Delete(); + } }); cgb.CreateCommand("lq") @@ -272,7 +277,7 @@ namespace NadekoBot.Modules cgb.CreateCommand("lopl") .Description("Queues up to 50 songs from a directory. **Owner Only!**\n**Usage**: `!m lopl C:/music/classical`") .Parameter("directory", ParameterType.Unparsed) - .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) + .AddCheck(SimpleCheckers.OwnerOnly()) .Do(async e => { var arg = e.GetArg("directory"); @@ -301,12 +306,17 @@ namespace NadekoBot.Modules return; } await QueueSong(e.Channel, e.User.VoiceChannel, e.GetArg("radio_link"), musicType: MusicType.Radio); + if (e.Server.CurrentUser.GetPermissions(e.Channel).ManageMessages) + { + await Task.Delay(10000); + await e.Message.Delete(); + } }); cgb.CreateCommand("lo") .Description("Queues a local file by specifying a full path. **Owner Only!**\n**Usage**: `!m ra C:/music/mysong.mp3`") .Parameter("path", ParameterType.Unparsed) - .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) + .AddCheck(SimpleCheckers.OwnerOnly()) .Do(async e => { var arg = e.GetArg("path"); @@ -350,9 +360,9 @@ namespace NadekoBot.Modules } if (num <= 0 || num > musicPlayer.Playlist.Count) return; - + var song = (musicPlayer.Playlist as List)?[num - 1]; musicPlayer.RemoveSongAt(num - 1); - await e.Channel.SendMessage($"🎵**Track at position `#{num}` has been removed.**"); + await e.Channel.SendMessage($"🎵**Track {song.PrettyName} at position `#{num}` has been removed.**"); }); cgb.CreateCommand("cleanup") @@ -434,8 +444,8 @@ namespace NadekoBot.Modules ProviderType = (int)s.SongInfo.ProviderType, Title = s.SongInfo.Title, Uri = s.SongInfo.Uri, - Query = s.SongInfo.Query - }); + Query = s.SongInfo.Query, + }).ToList(); var playlist = new MusicPlaylist { @@ -443,19 +453,28 @@ namespace NadekoBot.Modules CreatorName = e.User.Name, Name = name.ToLowerInvariant(), }; - DbHandler.Instance.SaveAll(songInfos); DbHandler.Instance.Save(playlist); DbHandler.Instance.InsertMany(songInfos.Select(s => new PlaylistSongInfo { - PlaylistId = playlist.Id, - SongInfoId = s.Id + PlaylistId = playlist.Id.Value, + SongInfoId = s.Id.Value })); await e.Channel.SendMessage($"🎵 `Saved playlist as {name}-{playlist.Id}`"); }); + //cgb.CreateCommand("info") + // .Description("Prints music info (queued/finished/playing) only to this channel") + // .Do(async e => + // { + // MusicPlayer musicPlayer; + // if (!MusicPlayers.TryGetValue(e.Server, out musicPlayer)) + // return; + // musicPlayer + // }); + cgb.CreateCommand("load") .Description("Loads a playlist under a certain name. \n**Usage**: `!m load classical-1`") .Parameter("name", ParameterType.Unparsed) @@ -531,11 +550,19 @@ namespace NadekoBot.Modules if (DefaultMusicVolumes.TryGetValue(server.Id, out throwAway)) vol = throwAway; var mp = new MusicPlayer(voiceCh, vol); + + + Message playingMessage = null; + Message lastFinishedMessage = null; mp.OnCompleted += async (s, song) => { try { - await textCh.SendMessage($"🎵`Finished`{song.PrettyName}"); + if (lastFinishedMessage != null) + await lastFinishedMessage.Delete(); + if (playingMessage != null) + await playingMessage.Delete(); + lastFinishedMessage = await textCh.SendMessage($"🎵`Finished`{song.PrettyName}"); } catch { } }; @@ -547,7 +574,7 @@ namespace NadekoBot.Modules try { var msgTxt = $"🎵`Playing`{song.PrettyName} `Vol: {(int)(sender.Volume * 100)}%`"; - await textCh.SendMessage(msgTxt); + playingMessage = await textCh.SendMessage(msgTxt); } catch { } }; @@ -555,9 +582,23 @@ namespace NadekoBot.Modules }); var resolvedSong = await Song.ResolveSong(query, musicType); resolvedSong.MusicPlayer = musicPlayer; - if (!silent) - await textCh.Send($"🎵`Queued`{resolvedSong.PrettyName}"); + musicPlayer.AddSong(resolvedSong); + if (!silent) + { + var queuedMessage = await textCh.SendMessage($"🎵`Queued`{resolvedSong.PrettyName} **at** `#{musicPlayer.Playlist.Count}`"); +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + Task.Run(async () => + { + await Task.Delay(10000); + try + { + await queuedMessage.Delete(); + } + catch { } + }); +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + } } } } diff --git a/NadekoBot/Modules/Pokemon/PokemonModule.cs b/NadekoBot/Modules/Pokemon/PokemonModule.cs index ffe60c5b..b754a236 100644 --- a/NadekoBot/Modules/Pokemon/PokemonModule.cs +++ b/NadekoBot/Modules/Pokemon/PokemonModule.cs @@ -20,7 +20,7 @@ namespace NadekoBot.Modules.Pokemon public PokemonModule() { - + } private int GetDamage(PokemonType usertype, PokemonType targetType) @@ -35,7 +35,7 @@ namespace NadekoBot.Modules.Pokemon damage = (int)(damage * multiplier); } } - + return damage; } @@ -50,12 +50,12 @@ namespace NadekoBot.Modules.Pokemon } int count = NadekoBot.Config.PokemonTypes.Count; - int remainder = Math.Abs((int)(id % (ulong) count)); + int remainder = Math.Abs((int)(id % (ulong)count)); return NadekoBot.Config.PokemonTypes[remainder]; } - + private PokemonType stringToPokemonType(string v) { @@ -311,7 +311,7 @@ namespace NadekoBot.Modules.Pokemon FlowersHandler.RemoveFlowers(e.User, $"set usertype to {targetTypeStr}", amount); //Actually changing the type here var preTypes = DbHandler.Instance.GetAllRows(); - Dictionary Dict = preTypes.ToDictionary(x => x.UserId, y => y.Id); + Dictionary Dict = preTypes.ToDictionary(x => x.UserId, y => y.Id.Value); if (Dict.ContainsKey((long)e.User.Id)) { //delete previous type diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index 25db9a10..1d6130d5 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -167,7 +167,7 @@ namespace NadekoBot modules.Add(new Conversations(), "Conversations", ModuleFilter.None); modules.Add(new GamblingModule(), "Gambling", ModuleFilter.None); modules.Add(new GamesModule(), "Games", ModuleFilter.None); - //modules.Add(new Music(), "Music", ModuleFilter.None); + modules.Add(new Music(), "Music", ModuleFilter.None); modules.Add(new Searches(), "Searches", ModuleFilter.None); modules.Add(new NSFW(), "NSFW", ModuleFilter.None); modules.Add(new ClashOfClans(), "ClashOfClans", ModuleFilter.None); diff --git a/NadekoBot/bin/Debug/credentials_example.json b/NadekoBot/bin/Debug/credentials_example.json index 2822695d..922d81f9 100644 --- a/NadekoBot/bin/Debug/credentials_example.json +++ b/NadekoBot/bin/Debug/credentials_example.json @@ -1,6 +1,7 @@ { "Username": "myemail@email.com", "Password": "xxxxxxx", + "Token": "", "BotId": 1231231231231, "GoogleAPIKey": "", "OwnerIds": [