From 5f98c273d7a9e27389969cd3ed4c18f726df4fa1 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 9 Mar 2017 16:30:34 +0100 Subject: [PATCH] Quote system improved. Quotes now show ids, and .delq is taking an ID --- .../Modules/Permissions/Permissions.cs | 2 +- .../Modules/Utility/Commands/QuoteCommands.cs | 57 +++++++++---------- .../Resources/CommandStrings.Designer.cs | 2 +- src/NadekoBot/Resources/CommandStrings.resx | 2 +- src/NadekoBot/Resources/ResponseStrings.resx | 2 +- .../Database/Repositories/IQuoteRepository.cs | 1 + .../Repositories/Impl/QuoteRepository.cs | 6 +- .../Impl/SelfAssignedRolesRepository.cs | 2 +- 8 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/NadekoBot/Modules/Permissions/Permissions.cs b/src/NadekoBot/Modules/Permissions/Permissions.cs index 93186fb9..f29369f8 100644 --- a/src/NadekoBot/Modules/Permissions/Permissions.cs +++ b/src/NadekoBot/Modules/Permissions/Permissions.cs @@ -219,7 +219,7 @@ namespace NadekoBot.Modules.Permissions var startPos = 20 * (page - 1); var toSend = Format.Bold(GetText("page", page)) + "\n\n" + string.Join("\n", perms.Reverse() - .Skip((page - 1) * 20) + .Skip(startPos) .Take(20) .Select(p => { diff --git a/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs b/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs index d811f565..41c29a44 100644 --- a/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs +++ b/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs @@ -33,9 +33,9 @@ namespace NadekoBot.Modules.Utility } if (quotes.Any()) - await Context.Channel.SendConfirmAsync(GetText("quotes_page", page + 1), - string.Join("\n", quotes.Select(q => $"{q.Keyword,-20} by {q.AuthorName}"))) - .ConfigureAwait(false); + await Context.Channel.SendConfirmAsync(GetText("quotes_page", page + 1), + string.Join("\n", quotes.Select(q => $"`#{q.Id}` {Format.Bold(q.Keyword),-20} by {q.AuthorName}"))) + .ConfigureAwait(false); else await ReplyErrorLocalized("quotes_page_none").ConfigureAwait(false); } @@ -52,7 +52,8 @@ namespace NadekoBot.Modules.Utility Quote quote; using (var uow = DbHandler.UnitOfWork()) { - quote = await uow.Quotes.GetRandomQuoteByKeywordAsync(Context.Guild.Id, keyword).ConfigureAwait(false); + quote = + await uow.Quotes.GetRandomQuoteByKeywordAsync(Context.Guild.Id, keyword).ConfigureAwait(false); } if (quote == null) @@ -61,7 +62,11 @@ namespace NadekoBot.Modules.Utility CREmbed crembed; if (CREmbed.TryParse(quote.Text, out crembed)) { - try { await Context.Channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? "").ConfigureAwait(false); } + try + { + await Context.Channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? "") + .ConfigureAwait(false); + } catch (Exception ex) { _log.Warn("Sending CREmbed failed"); @@ -69,28 +74,31 @@ namespace NadekoBot.Modules.Utility } return; } - await Context.Channel.SendMessageAsync("📣 " + quote.Text.SanitizeMentions()); + await Context.Channel.SendMessageAsync($"`#{quote.Id}` 📣 " + quote.Text.SanitizeMentions()); } - + [NadekoCommand, Usage, Description, Aliases] - [RequireContext(ContextType.Guild)] + [RequireContext(ContextType.Guild)] public async Task SearchQuote(string keyword, [Remainder] string text) { - if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text)) - return; + if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text)) + return; keyword = keyword.ToUpperInvariant(); Quote keywordquote; using (var uow = DbHandler.UnitOfWork()) { - keywordquote = await uow.Quotes.SearchQuoteKeywordTextAsync(Context.Guild.Id, keyword, text).ConfigureAwait(false); + keywordquote = + await uow.Quotes.SearchQuoteKeywordTextAsync(Context.Guild.Id, keyword, text) + .ConfigureAwait(false); } if (keywordquote == null) return; - await Context.Channel.SendMessageAsync("💬 " + keyword.ToLowerInvariant() + ": " + keywordquote.Text.SanitizeMentions()); + await Context.Channel.SendMessageAsync("💬 " + keyword.ToLowerInvariant() + ": " + + keywordquote.Text.SanitizeMentions()); } [NadekoCommand, Usage, Description, Aliases] @@ -119,36 +127,29 @@ namespace NadekoBot.Modules.Utility [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] - public async Task DeleteQuote([Remainder] string keyword) + public async Task DeleteQuote(int id) { - if (string.IsNullOrWhiteSpace(keyword)) - return; + var isAdmin = ((IGuildUser) Context.Message.Author).GuildPermissions.Administrator; - 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)?.Where(elem => isAdmin || elem.AuthorId == Context.Message.Author.Id).ToArray(); + var q = uow.Quotes.Get(id); - if (qs == null || !qs.Any()) + if (q == null || (!isAdmin && q.AuthorId != Context.Message.Author.Id)) { - sucess = false; response = GetText("quotes_remove_none"); } else { - var q = qs[new NadekoRandom().Next(0, qs.Length)]; - uow.Quotes.Remove(q); await uow.CompleteAsync().ConfigureAwait(false); sucess = true; - response = GetText("quote_deleted"); + response = GetText("quote_deleted", id); } } - if(sucess) + if (sucess) await Context.Channel.SendConfirmAsync(response); else await Context.Channel.SendErrorAsync(response); @@ -166,9 +167,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?! + uow.Quotes.RemoveAllByKeyword(Context.Guild.Id, keyword.ToUpperInvariant()); await uow.CompleteAsync(); } @@ -177,4 +176,4 @@ namespace NadekoBot.Modules.Utility } } } -} +} \ No newline at end of file diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index e049a7f9..04f58fb5 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -2364,7 +2364,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to Deletes a random quote with the specified keyword. You have to either be server Administrator or the creator of the quote to delete it.. + /// Looks up a localized string similar to Deletes a quote with the specified ID. You have to be either server Administrator or the creator of the quote to delete it.. /// public static string deletequote_desc { get { diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index f1b4bc6a..d81c15d7 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -1156,7 +1156,7 @@ deletequote delq - Deletes a random quote with the specified keyword. You have to either be server Administrator or the creator of the quote to delete it. + Deletes a quote with the specified ID. You have to be either server Administrator or the creator of the quote to delete it. `{0}delq abc` diff --git a/src/NadekoBot/Resources/ResponseStrings.resx b/src/NadekoBot/Resources/ResponseStrings.resx index 139b54a7..22279c8b 100644 --- a/src/NadekoBot/Resources/ResponseStrings.resx +++ b/src/NadekoBot/Resources/ResponseStrings.resx @@ -2091,7 +2091,7 @@ Owner ID: {2} Quote Added - Deleted a random quote. + Quote #{0} deleted. Region diff --git a/src/NadekoBot/Services/Database/Repositories/IQuoteRepository.cs b/src/NadekoBot/Services/Database/Repositories/IQuoteRepository.cs index 080783ab..2cd1cdbe 100644 --- a/src/NadekoBot/Services/Database/Repositories/IQuoteRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/IQuoteRepository.cs @@ -10,5 +10,6 @@ namespace NadekoBot.Services.Database.Repositories Task GetRandomQuoteByKeywordAsync(ulong guildId, string keyword); Task SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text); IEnumerable GetGroup(ulong guildId, int skip, int take); + void RemoveAllByKeyword(ulong guildId, string keyword); } } diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs index c2963c2b..76002d1c 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs @@ -29,6 +29,10 @@ namespace NadekoBot.Services.Database.Repositories.Impl { var rngk = new NadekoRandom(); return _set.Where(q => q.Text.ContainsNoCase(text, StringComparison.OrdinalIgnoreCase) && q.GuildId == guildId && q.Keyword == keyword).OrderBy(q => rngk.Next()).FirstOrDefaultAsync(); - } + } + + public void RemoveAllByKeyword(ulong guildId, string keyword) => + _set.RemoveRange(_set.Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword)); + } } diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignedRolesRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignedRolesRepository.cs index 77926648..d17dac7d 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignedRolesRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignedRolesRepository.cs @@ -13,7 +13,7 @@ namespace NadekoBot.Services.Database.Repositories.Impl public bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId) { - var role = _set.Where(s => s.GuildId == guildId && s.RoleId == roleId).FirstOrDefault(); + var role = _set.FirstOrDefault(s => s.GuildId == guildId && s.RoleId == roleId); if (role == null) return false;