Quote system improved. Quotes now show ids, and .delq is taking an ID

This commit is contained in:
Kwoth 2017-03-09 16:30:34 +01:00
parent b86589c15a
commit 5f98c273d7
8 changed files with 39 additions and 35 deletions

View File

@ -219,7 +219,7 @@ namespace NadekoBot.Modules.Permissions
var startPos = 20 * (page - 1); var startPos = 20 * (page - 1);
var toSend = Format.Bold(GetText("page", page)) + "\n\n" + string.Join("\n", var toSend = Format.Bold(GetText("page", page)) + "\n\n" + string.Join("\n",
perms.Reverse() perms.Reverse()
.Skip((page - 1) * 20) .Skip(startPos)
.Take(20) .Take(20)
.Select(p => .Select(p =>
{ {

View File

@ -33,9 +33,9 @@ namespace NadekoBot.Modules.Utility
} }
if (quotes.Any()) if (quotes.Any())
await Context.Channel.SendConfirmAsync(GetText("quotes_page", page + 1), await Context.Channel.SendConfirmAsync(GetText("quotes_page", page + 1),
string.Join("\n", quotes.Select(q => $"{q.Keyword,-20} by {q.AuthorName}"))) string.Join("\n", quotes.Select(q => $"`#{q.Id}` {Format.Bold(q.Keyword),-20} by {q.AuthorName}")))
.ConfigureAwait(false); .ConfigureAwait(false);
else else
await ReplyErrorLocalized("quotes_page_none").ConfigureAwait(false); await ReplyErrorLocalized("quotes_page_none").ConfigureAwait(false);
} }
@ -52,7 +52,8 @@ namespace NadekoBot.Modules.Utility
Quote quote; Quote quote;
using (var uow = DbHandler.UnitOfWork()) 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) if (quote == null)
@ -61,7 +62,11 @@ namespace NadekoBot.Modules.Utility
CREmbed crembed; CREmbed crembed;
if (CREmbed.TryParse(quote.Text, out 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) catch (Exception ex)
{ {
_log.Warn("Sending CREmbed failed"); _log.Warn("Sending CREmbed failed");
@ -69,28 +74,31 @@ namespace NadekoBot.Modules.Utility
} }
return; return;
} }
await Context.Channel.SendMessageAsync("📣 " + quote.Text.SanitizeMentions()); await Context.Channel.SendMessageAsync($"`#{quote.Id}` 📣 " + quote.Text.SanitizeMentions());
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task SearchQuote(string keyword, [Remainder] string text) public async Task SearchQuote(string keyword, [Remainder] string text)
{ {
if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text)) if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text))
return; return;
keyword = keyword.ToUpperInvariant(); keyword = keyword.ToUpperInvariant();
Quote keywordquote; Quote keywordquote;
using (var uow = DbHandler.UnitOfWork()) 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) if (keywordquote == null)
return; return;
await Context.Channel.SendMessageAsync("💬 " + keyword.ToLowerInvariant() + ": " + keywordquote.Text.SanitizeMentions()); await Context.Channel.SendMessageAsync("💬 " + keyword.ToLowerInvariant() + ": " +
keywordquote.Text.SanitizeMentions());
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -119,36 +127,29 @@ namespace NadekoBot.Modules.Utility
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task DeleteQuote([Remainder] string keyword) public async Task DeleteQuote(int id)
{ {
if (string.IsNullOrWhiteSpace(keyword)) var isAdmin = ((IGuildUser) Context.Message.Author).GuildPermissions.Administrator;
return;
var isAdmin = ((IGuildUser)Context.Message.Author).GuildPermissions.Administrator;
keyword = keyword.ToUpperInvariant();
var sucess = false; var sucess = false;
string response; string response;
using (var uow = DbHandler.UnitOfWork()) 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"); response = GetText("quotes_remove_none");
} }
else else
{ {
var q = qs[new NadekoRandom().Next(0, qs.Length)];
uow.Quotes.Remove(q); uow.Quotes.Remove(q);
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
sucess = true; sucess = true;
response = GetText("quote_deleted"); response = GetText("quote_deleted", id);
} }
} }
if(sucess) if (sucess)
await Context.Channel.SendConfirmAsync(response); await Context.Channel.SendConfirmAsync(response);
else else
await Context.Channel.SendErrorAsync(response); await Context.Channel.SendErrorAsync(response);
@ -166,9 +167,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var quotes = uow.Quotes.GetAllQuotesByKeyword(Context.Guild.Id, keyword); uow.Quotes.RemoveAllByKeyword(Context.Guild.Id, keyword.ToUpperInvariant());
//todo kwoth please don't be complete retard
uow.Quotes.RemoveRange(quotes.ToArray());//wtf?!
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
@ -177,4 +176,4 @@ namespace NadekoBot.Modules.Utility
} }
} }
} }
} }

View File

@ -2364,7 +2364,7 @@ namespace NadekoBot.Resources {
} }
/// <summary> /// <summary>
/// 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..
/// </summary> /// </summary>
public static string deletequote_desc { public static string deletequote_desc {
get { get {

View File

@ -1156,7 +1156,7 @@
<value>deletequote delq</value> <value>deletequote delq</value>
</data> </data>
<data name="deletequote_desc" xml:space="preserve"> <data name="deletequote_desc" xml:space="preserve">
<value>Deletes a random quote with the specified keyword. You have to either be server Administrator or the creator of the quote to delete it.</value> <value>Deletes a quote with the specified ID. You have to be either server Administrator or the creator of the quote to delete it.</value>
</data> </data>
<data name="deletequote_usage" xml:space="preserve"> <data name="deletequote_usage" xml:space="preserve">
<value>`{0}delq abc`</value> <value>`{0}delq abc`</value>

View File

@ -2091,7 +2091,7 @@ Owner ID: {2}</value>
<value>Quote Added</value> <value>Quote Added</value>
</data> </data>
<data name="utility_quote_deleted" xml:space="preserve"> <data name="utility_quote_deleted" xml:space="preserve">
<value>Deleted a random quote.</value> <value>Quote #{0} deleted.</value>
</data> </data>
<data name="utility_region" xml:space="preserve"> <data name="utility_region" xml:space="preserve">
<value>Region</value> <value>Region</value>

View File

@ -10,5 +10,6 @@ namespace NadekoBot.Services.Database.Repositories
Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword); Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword);
Task<Quote> SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text); Task<Quote> SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text);
IEnumerable<Quote> GetGroup(ulong guildId, int skip, int take); IEnumerable<Quote> GetGroup(ulong guildId, int skip, int take);
void RemoveAllByKeyword(ulong guildId, string keyword);
} }
} }

View File

@ -29,6 +29,10 @@ namespace NadekoBot.Services.Database.Repositories.Impl
{ {
var rngk = new NadekoRandom(); 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(); 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));
} }
} }

View File

@ -13,7 +13,7 @@ namespace NadekoBot.Services.Database.Repositories.Impl
public bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId) 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) if (role == null)
return false; return false;