From cce8465ff3f8b1565f480ff40a821af0ab8b99c2 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 29 Jan 2017 22:54:27 +0100 Subject: [PATCH] a bit of cleanup --- src/NadekoBot/Modules/Games/Games.cs | 4 +-- src/NadekoBot/Modules/Music/Music.cs | 9 ++++++- src/NadekoBot/Modules/Searches/Searches.cs | 23 +++++++++------- .../Modules/Utility/Commands/QuoteCommands.cs | 27 ++++++++++++------- src/NadekoBot/Modules/Utility/Utility.cs | 22 ++++++++++----- .../Resources/CommandStrings.Designer.cs | 2 +- src/NadekoBot/Resources/CommandStrings.resx | 2 +- 7 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/NadekoBot/Modules/Games/Games.cs b/src/NadekoBot/Modules/Games/Games.cs index 8dfd7d5a..6faf288c 100644 --- a/src/NadekoBot/Modules/Games/Games.cs +++ b/src/NadekoBot/Modules/Games/Games.cs @@ -13,7 +13,7 @@ namespace NadekoBot.Modules.Games [NadekoModule("Games", ">")] public partial class Games : DiscordModule { - private static IEnumerable _8BallResponses { get; } = NadekoBot.BotConfig.EightBallResponses.Select(ebr => ebr.Text); + private static string[] _8BallResponses { get; } = NadekoBot.BotConfig.EightBallResponses.Select(ebr => ebr.Text).ToArray(); [NadekoCommand, Usage, Description, Aliases] @@ -37,7 +37,7 @@ namespace NadekoBot.Modules.Games await Context.Channel.EmbedAsync(new EmbedBuilder().WithColor(NadekoBot.OkColor) .AddField(efb => efb.WithName("❓ Question").WithValue(question).WithIsInline(false)) - .AddField(efb => efb.WithName("🎱 8Ball").WithValue(_8BallResponses.Shuffle().FirstOrDefault()).WithIsInline(false))); + .AddField(efb => efb.WithName("🎱 8Ball").WithValue(_8BallResponses[new NadekoRandom().Next(0, _8BallResponses.Length)]).WithIsInline(false))); } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Modules/Music/Music.cs b/src/NadekoBot/Modules/Music/Music.cs index b7c41b55..b229bf84 100644 --- a/src/NadekoBot/Modules/Music/Music.cs +++ b/src/NadekoBot/Modules/Music/Music.cs @@ -833,7 +833,14 @@ namespace NadekoBot.Modules.Music if (mp.Autoplay && mp.Playlist.Count == 0 && song.SongInfo.ProviderType == MusicType.Normal) { - await QueueSong(await queuer.Guild.GetCurrentUserAsync(), textCh, voiceCh, (await NadekoBot.Google.GetRelatedVideosAsync(song.SongInfo.Query, 4)).ToList().Shuffle().FirstOrDefault(), silent, musicType).ConfigureAwait(false); + 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)], + silent, + musicType).ConfigureAwait(false); } } catch { } diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index 27840678..8abcfee8 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -320,10 +320,10 @@ namespace NadekoBot.Modules.Searches .ConfigureAwait(false); try { - var items = JArray.Parse(response).Shuffle().ToList(); - if (items == null) + var items = JArray.Parse(response).ToArray(); + if (items == null || items.Length == 0) throw new KeyNotFoundException("Cannot find a card by that name"); - var item = items[0]; + var item = items[new NadekoRandom().Next(0, items.Length)]; var storeUrl = await NadekoBot.Google.ShortenUrl(item["store_url"].ToString()); var cost = item["cost"].ToString(); var desc = item["text"].ToString(); @@ -379,13 +379,16 @@ namespace NadekoBot.Modules.Searches throw new KeyNotFoundException("Cannot find a card by that name"); foreach (var item in items.Where(item => item.HasValues && item["img"] != null).Take(4)) { - using (var sr = await http.GetStreamAsync(item["img"].ToString())) + await Task.Run(async () => { - var imgStream = new MemoryStream(); - await sr.CopyToAsync(imgStream); - imgStream.Position = 0; - images.Add(new ImageSharp.Image(imgStream)); - } + using (var sr = await http.GetStreamAsync(item["img"].ToString())) + { + var imgStream = new MemoryStream(); + await sr.CopyToAsync(imgStream); + imgStream.Position = 0; + images.Add(new ImageSharp.Image(imgStream)); + } + }).ConfigureAwait(false); } string msg = null; if (items.Count > 4) @@ -393,7 +396,7 @@ namespace NadekoBot.Modules.Searches msg = "⚠ Found over 4 images. Showing random 4."; } var ms = new MemoryStream(); - images.AsEnumerable().Merge().SaveAsPng(ms); + await Task.Run(() => images.AsEnumerable().Merge().SaveAsPng(ms)); ms.Position = 0; await Context.Channel.SendFileAsync(ms, arg + ".png", msg).ConfigureAwait(false); } diff --git a/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs b/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs index fd60a591..8add0707 100644 --- a/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs +++ b/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs @@ -93,24 +93,31 @@ namespace NadekoBot.Modules.Utility var isAdmin = ((IGuildUser)Context.Message.Author).GuildPermissions.Administrator; keyword = keyword.ToUpperInvariant(); + var sucess = false; string response; using (var uow = DbHandler.UnitOfWork()) { - var qs = uow.Quotes.GetAllQuotesByKeyword(Context.Guild.Id, keyword); + var qs = uow.Quotes.GetAllQuotesByKeyword(Context.Guild.Id, keyword)?.Where(elem => isAdmin || elem.AuthorId == Context.Message.Author.Id).ToArray(); if (qs == null || !qs.Any()) { - await Context.Channel.SendErrorAsync("No quotes found.").ConfigureAwait(false); - return; + sucess = false; + response = "No quotes found which you can remove."; } + else + { + var q = qs[new NadekoRandom().Next(0, qs.Length)]; - var q = qs.Shuffle().FirstOrDefault(elem => isAdmin || elem.AuthorId == Context.Message.Author.Id); - - uow.Quotes.Remove(q); - await uow.CompleteAsync().ConfigureAwait(false); - response = "🗑 **Deleted a random quote.**"; + uow.Quotes.Remove(q); + await uow.CompleteAsync().ConfigureAwait(false); + sucess = true; + response = "🗑 **Deleted a random quote.**"; + } } - await Context.Channel.SendConfirmAsync(response); + if(sucess) + await Context.Channel.SendConfirmAsync(response); + else + await Context.Channel.SendErrorAsync(response); } [NadekoCommand, Usage, Description, Aliases] @@ -126,7 +133,7 @@ namespace NadekoBot.Modules.Utility using (var uow = DbHandler.UnitOfWork()) { var quotes = uow.Quotes.GetAllQuotesByKeyword(Context.Guild.Id, keyword); - + //todo kwoth please don't be complete retard uow.Quotes.RemoveRange(quotes.ToArray());//wtf?! await uow.CompleteAsync(); diff --git a/src/NadekoBot/Modules/Utility/Utility.cs b/src/NadekoBot/Modules/Utility/Utility.cs index 304b2782..1c1af1e0 100644 --- a/src/NadekoBot/Modules/Utility/Utility.cs +++ b/src/NadekoBot/Modules/Utility/Utility.cs @@ -15,6 +15,8 @@ using System.Threading; using ImageSharp; using System.Collections.Generic; using Newtonsoft.Json; +using Discord.WebSocket; +using NadekoBot.Services; namespace NadekoBot.Modules.Utility { @@ -113,21 +115,29 @@ namespace NadekoBot.Modules.Utility game = game.Trim().ToUpperInvariant(); if (string.IsNullOrWhiteSpace(game)) return; - var arr = (await (Context.Channel as IGuildChannel).Guild.GetUsersAsync()) + + var socketGuild = Context.Guild as SocketGuild; + if (socketGuild == null) { + _log.Warn("Can't cast guild to socket guild."); + return; + } + var rng = new NadekoRandom(); + var arr = await Task.Run(() => socketGuild.Users .Where(u => u.Game?.Name?.ToUpperInvariant() == game) .Select(u => u.Username) - .Shuffle() + .OrderBy(x => rng.Next()) .Take(60) - .ToList(); + .ToArray()).ConfigureAwait(false); int i = 0; - if (!arr.Any()) + if (arr.Length == 0) await Context.Channel.SendErrorAsync("Nobody is playing that game.").ConfigureAwait(false); - else { + else + { await Context.Channel.SendConfirmAsync("```css\n" + string.Join("\n", arr.GroupBy(item => (i++) / 2) .Select(ig => string.Concat(ig.Select(el => $"• {el,-27}")))) + "\n```") .ConfigureAwait(false); - } + } } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index ae2ff2c6..4388c9bd 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -8340,7 +8340,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to `{0}voice+text`. + /// Looks up a localized string similar to `{0}v+t`. /// public static string voiceplustext_usage { get { diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index a31288b1..2dc50365 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -340,7 +340,7 @@ Creates a text channel for each voice channel only users in that voice channel can see.If you are server owner, keep in mind you will see them all the time regardless. - `{0}voice+text` + `{0}v+t` scsc