From 0b1d47fc4ed2e5462b6a2075e7e3ecf1bb86418f Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 22 Dec 2016 05:32:34 +0100 Subject: [PATCH] More music stuff, more fixes --- .../Commands/SelfAssignedRolesCommand.cs | 16 +--- .../Commands/ServerGreetCommands.cs | 12 +-- .../Games/Commands/Hangman/HangmanGame.cs | 2 +- .../Games/Commands/PlantAndPickCommands.cs | 6 +- .../Modules/Games/Commands/PollCommands.cs | 3 +- .../Modules/Music/Classes/MusicControls.cs | 6 +- src/NadekoBot/Modules/Music/Classes/Song.cs | 4 +- src/NadekoBot/Modules/Music/Music.cs | 86 +++++++++---------- src/NadekoBot/Modules/NSFW/NSFW.cs | 2 +- src/NadekoBot/_Extensions/Extensions.cs | 3 + 10 files changed, 60 insertions(+), 80 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Commands/SelfAssignedRolesCommand.cs b/src/NadekoBot/Modules/Administration/Commands/SelfAssignedRolesCommand.cs index dfd8b0a7..e4e66155 100644 --- a/src/NadekoBot/Modules/Administration/Commands/SelfAssignedRolesCommand.cs +++ b/src/NadekoBot/Modules/Administration/Commands/SelfAssignedRolesCommand.cs @@ -195,12 +195,8 @@ namespace NadekoBot.Modules.Administration if (conf.AutoDeleteSelfAssignedRoleMessages) { - var t = Task.Run(async () => - { - await Task.Delay(3000).ConfigureAwait(false); - try { await msg.DeleteAsync().ConfigureAwait(false); } catch { } // if 502 or something, i don't want bot crashing - try { await usrMsg.DeleteAsync().ConfigureAwait(false); } catch { } - }); + msg.DeleteAfter(3); + umsg.DeleteAfter(3); } } @@ -242,12 +238,8 @@ namespace NadekoBot.Modules.Administration if (autoDeleteSelfAssignedRoleMessages) { - var t = Task.Run(async () => - { - await Task.Delay(3000).ConfigureAwait(false); - try { await msg.DeleteAsync().ConfigureAwait(false); } catch { } // if 502 or something, i don't want bot crashing - try { await umsg.DeleteAsync().ConfigureAwait(false); } catch { } - }); + msg.DeleteAfter(3); + umsg.DeleteAfter(3); } } } diff --git a/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs b/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs index 1a42e613..beffdd57 100644 --- a/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs @@ -52,11 +52,7 @@ namespace NadekoBot.Modules.Administration var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false); if (conf.AutoDeleteByeMessagesTimer > 0) { - var t = Task.Run(async () => - { - await Task.Delay(conf.AutoDeleteByeMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes - try { await toDelete.DeleteAsync().ConfigureAwait(false); } catch { } - }); + toDelete.DeleteAfter(conf.AutoDeleteByeMessagesTimer); } } catch (Exception ex) { _log.Warn(ex); } @@ -91,11 +87,7 @@ namespace NadekoBot.Modules.Administration var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false); if (conf.AutoDeleteGreetMessagesTimer > 0) { - var t = Task.Run(async () => - { - await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes - try { await toDelete.DeleteAsync().ConfigureAwait(false); } catch { } - }); + toDelete.DeleteAfter(conf.AutoDeleteGreetMessagesTimer); } } catch (Exception ex) { _log.Warn(ex); } diff --git a/src/NadekoBot/Modules/Games/Commands/Hangman/HangmanGame.cs b/src/NadekoBot/Modules/Games/Commands/Hangman/HangmanGame.cs index 6baa9404..be814333 100644 --- a/src/NadekoBot/Modules/Games/Commands/Hangman/HangmanGame.cs +++ b/src/NadekoBot/Modules/Games/Commands/Hangman/HangmanGame.cs @@ -118,7 +118,7 @@ namespace NadekoBot.Modules.Games.Commands.Hangman .AddField(efb => efb.WithName("It was").WithValue(Term.Word)) .WithImage(eib => eib.WithUrl(Term.ImageUrl)); if (Errors >= MaxErrors) - await GameChannel.EmbedAsync(embed.WithColor(NadekoBot.ErrorColor).Build()).ConfigureAwait(false); + await GameChannel.EmbedAsync(embed.WithErrorColor().Build()).ConfigureAwait(false); else await GameChannel.EmbedAsync(embed.WithOkColor().Build()).ConfigureAwait(false); } diff --git a/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs b/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs index 61a9a10d..b85825e3 100644 --- a/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs @@ -119,11 +119,7 @@ namespace NadekoBot.Modules.Games await CurrencyHandler.AddCurrencyAsync((IGuildUser)imsg.Author, "Picked flower(s).", msgs.Count, false).ConfigureAwait(false); var msg = await channel.SendConfirmAsync($"**{imsg.Author}** picked {msgs.Count}{Gambling.Gambling.CurrencySign}!").ConfigureAwait(false); - var t = Task.Run(async () => - { - await Task.Delay(10000).ConfigureAwait(false); - try { await msg.DeleteAsync().ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); } - }); + msg.DeleteAfter(10); } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Modules/Games/Commands/PollCommands.cs b/src/NadekoBot/Modules/Games/Commands/PollCommands.cs index 840abdf1..23441b6e 100644 --- a/src/NadekoBot/Modules/Games/Commands/PollCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/PollCommands.cs @@ -172,8 +172,7 @@ namespace NadekoBot.Modules.Games else { var toDelete = await ch.SendConfirmAsync($"{msg.Author.Mention} cast their vote.").ConfigureAwait(false); - await Task.Delay(5000); - await toDelete.DeleteAsync().ConfigureAwait(false); + toDelete.DeleteAfter(5); } } } diff --git a/src/NadekoBot/Modules/Music/Classes/MusicControls.cs b/src/NadekoBot/Modules/Music/Classes/MusicControls.cs index 5d10a22a..ae31b20e 100644 --- a/src/NadekoBot/Modules/Music/Classes/MusicControls.cs +++ b/src/NadekoBot/Modules/Music/Classes/MusicControls.cs @@ -41,9 +41,9 @@ namespace NadekoBot.Modules.Music.Classes public float Volume { get; private set; } - public event Func OnCompleted = delegate { return Task.CompletedTask; }; - public event Func OnStarted = delegate { return Task.CompletedTask; }; - public event Func OnPauseChanged = delegate { return Task.CompletedTask; }; + public event Action OnCompleted = delegate { }; + public event Action OnStarted = delegate { }; + public event Action OnPauseChanged = delegate { }; public IVoiceChannel PlaybackVoiceChannel { get; private set; } diff --git a/src/NadekoBot/Modules/Music/Classes/Song.cs b/src/NadekoBot/Modules/Music/Classes/Song.cs index 71daa7ec..407f0dc8 100644 --- a/src/NadekoBot/Modules/Music/Classes/Song.cs +++ b/src/NadekoBot/Modules/Music/Classes/Song.cs @@ -31,7 +31,7 @@ namespace NadekoBot.Modules.Music.Classes public string QueuerName { get; set; } public TimeSpan TotalTime { get; set; } = TimeSpan.Zero; - public TimeSpan CurrentTime => TimeSpan.FromSeconds(bytesSent / frameBytes / 1000 / milliseconds); + public TimeSpan CurrentTime => TimeSpan.FromSeconds(bytesSent / frameBytes / (1000 / milliseconds)); const int milliseconds = 20; const int samplesPerFrame = (48000 / 1000) * milliseconds; @@ -52,7 +52,7 @@ namespace NadekoBot.Modules.Music.Classes public string PrettyFullName => $"{PrettyName}\n\t\t*{PrettyInfo}*"; - public string PrettyCurrentTime => TotalTime.ToString(@"mm\:ss"); + public string PrettyCurrentTime => CurrentTime.ToString(@"mm\:ss"); private string PrettyTotalTime { get { diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index 64eb905c..afc42496 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -15,6 +15,7 @@ using Newtonsoft.Json.Linq; using System.Collections.Generic; using NadekoBot.Services.Database.Models; using System.Text.RegularExpressions; +using System.Threading; namespace NadekoBot.Modules.Music { @@ -99,7 +100,6 @@ namespace NadekoBot.Modules.Music [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task Destroy(IUserMessage umsg) - //public Task Destroy(IUserMessage umsg) { var channel = (ITextChannel)umsg.Channel; await channel.SendErrorAsync("This command is temporarily disabled.").ConfigureAwait(false); @@ -135,8 +135,7 @@ namespace NadekoBot.Modules.Music await QueueSong(((IGuildUser)umsg.Author), channel, ((IGuildUser)umsg.Author).VoiceChannel, query).ConfigureAwait(false); if (channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages) { - await Task.Delay(10000).ConfigureAwait(false); - await ((IUserMessage)umsg).DeleteAsync().ConfigureAwait(false); + umsg.DeleteAfter(10); } } @@ -149,8 +148,7 @@ namespace NadekoBot.Modules.Music await QueueSong(((IGuildUser)umsg.Author), channel, ((IGuildUser)umsg.Author).VoiceChannel, query, musicType: MusicType.Soundcloud).ConfigureAwait(false); if (channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages) { - await Task.Delay(10000).ConfigureAwait(false); - await ((IUserMessage)umsg).DeleteAsync().ConfigureAwait(false); + umsg.DeleteAfter(10); } } @@ -182,7 +180,7 @@ namespace NadekoBot.Modules.Music var number = 0 + startAt; var embed = new EmbedBuilder() - .WithAuthor(eab => eab.WithName($"Player Queue: Page {page}") + .WithAuthor(eab => eab.WithName($"Player Queue - Page {page}") .WithMusicIcon()) .WithDescription(string.Join("\n", musicPlayer.Playlist .Skip(startAt) @@ -315,17 +313,21 @@ namespace NadekoBot.Modules.Music var msg = await channel.SendMessageAsync($"🎵 Attempting to queue **{count}** songs".SnPl(count) + "...").ConfigureAwait(false); + var cancelSource = new CancellationTokenSource(); - foreach (var id in idArray) + var tasks = Task.WhenAll(idArray.Select(async id => { + if (cancelSource.Token.IsCancellationRequested) + return; try { await QueueSong(((IGuildUser)umsg.Author), channel, ((IGuildUser)umsg.Author).VoiceChannel, id, true).ConfigureAwait(false); } catch (SongNotFoundException) { } - catch { break; } + catch { try { cancelSource.Cancel(); } catch { } } + })); - } + await Task.WhenAny(tasks, Task.Delay(Timeout.Infinite, cancelSource.Token)); await msg.ModifyAsync(m => m.Content = "✅ Playlist queue complete.").ConfigureAwait(false); } @@ -411,8 +413,7 @@ namespace NadekoBot.Modules.Music await QueueSong(((IGuildUser)umsg.Author), channel, ((IGuildUser)umsg.Author).VoiceChannel, radio_link, musicType: MusicType.Radio).ConfigureAwait(false); if (channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages) { - await Task.Delay(10000).ConfigureAwait(false); - await ((IUserMessage)umsg).DeleteAsync().ConfigureAwait(false); + umsg.DeleteAfter(10); } } @@ -459,12 +460,13 @@ namespace NadekoBot.Modules.Music return; var song = (musicPlayer.Playlist as List)?[num - 1]; musicPlayer.RemoveSongAt(num - 1); - //await channel.SendConfirmAsync($"🎵 Track {song.PrettyName} at position `#{num}` has been **removed**.").ConfigureAwait(false); + var embed = new EmbedBuilder() .WithAuthor(eab => eab.WithName("Song Removed!").WithMusicIcon()) .AddField(fb => fb.WithName("**Song Position**").WithValue($"#{num}").WithIsInline(true)) .AddField(fb => fb.WithName("**Song Name**").WithValue($"**[{song.SongInfo.Title.TrimTo(70)}]({song.SongInfo.Query})** `{song.PrettyProvider} | {song.QueuerName.TrimTo(15)}`").WithIsInline(true)) - .WithColor(NadekoBot.ErrorColor); + .WithErrorColor(); + await channel.EmbedAsync(embed.Build()).ConfigureAwait(false); } @@ -661,10 +663,6 @@ namespace NadekoBot.Modules.Music playlists = uow.MusicPlaylists.GetPlaylistsOnPage(num); } - //await channel.SendConfirmAsync($@"🎶 **Page {num} of saved playlists:** - - //" + string.Join("\n", playlists.Select(r => $"`#{r.Id}` - **{r.Name}** by __{r.Author}__ ({r.Songs.Count} songs)"))).ConfigureAwait(false); - var embed = new EmbedBuilder() .WithAuthor(eab => eab.WithName($"Page {num} of Saved Playlists").WithMusicIcon()) .WithDescription(string.Join("\n", playlists.Select(r => $"`#{r.Id}` - **{r.Name}**\t by **`{r.Author}`**\t ({r.Songs.Count} songs)"))) @@ -843,30 +841,38 @@ namespace NadekoBot.Modules.Music IUserMessage playingMessage = null; mp.OnStarted += async (player, song) => { - if (playingMessage != null) - playingMessage.DeleteAfter(0); + try + { + if (playingMessage != null) + playingMessage.DeleteAfter(0); - playingMessage = await textCh.EmbedAsync(new EmbedBuilder().WithOkColor() - .WithAuthor(eab => eab.WithName("Playing Song").WithMusicIcon()) - .WithDescription(song.PrettyName) - .WithFooter(ef => ef.WithText(song.PrettyInfo)) - .Build()) - .ConfigureAwait(false); + playingMessage = await textCh.EmbedAsync(new EmbedBuilder().WithOkColor() + .WithAuthor(eab => eab.WithName("Playing Song").WithMusicIcon()) + .WithDescription(song.PrettyName) + .WithFooter(ef => ef.WithText(song.PrettyInfo)) + .Build()) + .ConfigureAwait(false); + } + catch { } }; mp.OnPauseChanged += async (paused) => { - IUserMessage pauseMessage = null; - if (paused) + try { - pauseMessage = await textCh.SendConfirmAsync("🎵 Music playback **paused**.").ConfigureAwait(false); + IUserMessage pauseMessage = null; + if (paused) + { + pauseMessage = await textCh.SendConfirmAsync("🎵 Music playback **paused**.").ConfigureAwait(false); + } + else + { + pauseMessage = await textCh.SendConfirmAsync("🎵 Music playback **resumed**.").ConfigureAwait(false); + } + if (pauseMessage != null) + pauseMessage.DeleteAfter(15); } - else - { - pauseMessage = await textCh.SendConfirmAsync("🎵 Music playback **resumed**.").ConfigureAwait(false); - } - if (pauseMessage != null) - pauseMessage.DeleteAfter(15); + catch { } }; return mp; }); @@ -898,16 +904,8 @@ namespace NadekoBot.Modules.Music .WithFooter(ef => ef.WithText($"{resolvedSong.PrettyProvider}")) .Build()) .ConfigureAwait(false); - var t = Task.Run(async () => - { - try - { - await Task.Delay(10000).ConfigureAwait(false); - - await queuedMessage.DeleteAsync().ConfigureAwait(false); - } - catch { } - }).ConfigureAwait(false); + if (queuedMessage != null) + queuedMessage.DeleteAfter(10); } catch { } // if queued message sending fails, don't attempt to delete it } diff --git a/src/NadekoBot/Modules/NSFW/NSFW.cs b/src/NadekoBot/Modules/NSFW/NSFW.cs index 0fba9e0e..62603807 100644 --- a/src/NadekoBot/Modules/NSFW/NSFW.cs +++ b/src/NadekoBot/Modules/NSFW/NSFW.cs @@ -247,7 +247,7 @@ namespace NadekoBot.Modules.NSFW if (matches.Count == 0) return null; - return matches[rng.Next(0, matches.Count)].Groups["ll"].Value; + return "http:" + matches[rng.Next(0, matches.Count)].Groups["ll"].Value; } } diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index 5cbe62ab..2e9c4b68 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -28,6 +28,9 @@ namespace NadekoBot.Extensions public static EmbedBuilder WithOkColor(this EmbedBuilder eb) => eb.WithColor(NadekoBot.OkColor); + public static EmbedBuilder WithErrorColor(this EmbedBuilder eb) => + eb.WithColor(NadekoBot.ErrorColor); + public static IMessage DeleteAfter(this IUserMessage msg, int seconds) { Task.Run(async () =>