2016-08-24 13:29:01 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using NadekoBot.Attributes;
|
2016-10-09 20:40:14 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2016-08-24 13:29:01 +00:00
|
|
|
|
using NadekoBot.Services;
|
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-02-17 14:06:44 +00:00
|
|
|
|
using NadekoBot.DataStructures;
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-08-24 17:04:24 +00:00
|
|
|
|
namespace NadekoBot.Modules.Utility
|
2016-08-24 13:29:01 +00:00
|
|
|
|
{
|
|
|
|
|
public partial class Utility
|
|
|
|
|
{
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[Group]
|
2017-02-17 14:06:44 +00:00
|
|
|
|
public class QuoteCommands : NadekoSubmodule
|
2016-10-27 10:36:42 +00:00
|
|
|
|
{
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
public async Task ListQuotes(int page = 1)
|
2016-10-27 10:36:42 +00:00
|
|
|
|
{
|
2016-12-17 00:21:05 +00:00
|
|
|
|
page -= 1;
|
2016-10-27 10:36:42 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
if (page < 0)
|
|
|
|
|
return;
|
2016-10-27 10:36:42 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
IEnumerable<Quote> quotes;
|
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
|
|
|
|
{
|
|
|
|
|
quotes = uow.Quotes.GetGroup(Context.Guild.Id, page * 16, 16);
|
|
|
|
|
}
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
if (quotes.Any())
|
2017-03-09 15:30:34 +00:00
|
|
|
|
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);
|
2016-12-17 00:21:05 +00:00
|
|
|
|
else
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await ReplyErrorLocalized("quotes_page_none").ConfigureAwait(false);
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2016-08-24 18:56:41 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
public async Task ShowQuote([Remainder] string keyword)
|
2016-08-24 13:29:01 +00:00
|
|
|
|
{
|
2016-12-17 00:21:05 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(keyword))
|
|
|
|
|
return;
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
keyword = keyword.ToUpperInvariant();
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
Quote quote;
|
2017-01-22 20:06:10 +00:00
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
2016-12-17 00:21:05 +00:00
|
|
|
|
{
|
2017-03-09 15:30:34 +00:00
|
|
|
|
quote =
|
|
|
|
|
await uow.Quotes.GetRandomQuoteByKeywordAsync(Context.Guild.Id, keyword).ConfigureAwait(false);
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
if (quote == null)
|
|
|
|
|
return;
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2017-02-17 14:06:44 +00:00
|
|
|
|
CREmbed crembed;
|
|
|
|
|
if (CREmbed.TryParse(quote.Text, out crembed))
|
|
|
|
|
{
|
2017-03-09 15:30:34 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Context.Channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? "")
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2017-02-17 14:06:44 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn("Sending CREmbed failed");
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-09 15:30:34 +00:00
|
|
|
|
await Context.Channel.SendMessageAsync($"`#{quote.Id}` 📣 " + quote.Text.SanitizeMentions());
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2017-03-09 15:30:34 +00:00
|
|
|
|
|
2017-03-02 08:41:33 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-03-09 15:30:34 +00:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2017-02-14 03:30:16 +00:00
|
|
|
|
public async Task SearchQuote(string keyword, [Remainder] string text)
|
2017-02-14 04:43:35 +00:00
|
|
|
|
{
|
2017-03-09 15:30:34 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text))
|
|
|
|
|
return;
|
2017-02-14 03:30:16 +00:00
|
|
|
|
|
|
|
|
|
keyword = keyword.ToUpperInvariant();
|
|
|
|
|
|
|
|
|
|
Quote keywordquote;
|
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
2017-03-02 08:41:33 +00:00
|
|
|
|
{
|
2017-03-09 15:30:34 +00:00
|
|
|
|
keywordquote =
|
|
|
|
|
await uow.Quotes.SearchQuoteKeywordTextAsync(Context.Guild.Id, keyword, text)
|
|
|
|
|
.ConfigureAwait(false);
|
2017-03-02 08:41:33 +00:00
|
|
|
|
}
|
2017-02-14 03:30:16 +00:00
|
|
|
|
|
|
|
|
|
if (keywordquote == null)
|
|
|
|
|
return;
|
2017-03-02 16:24:37 +00:00
|
|
|
|
|
2017-03-09 15:30:34 +00:00
|
|
|
|
await Context.Channel.SendMessageAsync("💬 " + keyword.ToLowerInvariant() + ": " +
|
|
|
|
|
keywordquote.Text.SanitizeMentions());
|
2017-02-14 03:30:16 +00:00
|
|
|
|
}
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
public async Task AddQuote(string keyword, [Remainder] string text)
|
2016-08-24 13:29:01 +00:00
|
|
|
|
{
|
2016-12-17 00:21:05 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text))
|
|
|
|
|
return;
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
keyword = keyword.ToUpperInvariant();
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
|
|
|
|
{
|
|
|
|
|
uow.Quotes.Add(new Quote
|
|
|
|
|
{
|
|
|
|
|
AuthorId = Context.Message.Author.Id,
|
|
|
|
|
AuthorName = Context.Message.Author.Username,
|
|
|
|
|
GuildId = Context.Guild.Id,
|
|
|
|
|
Keyword = keyword,
|
|
|
|
|
Text = text,
|
|
|
|
|
});
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await ReplyConfirmLocalized("quote_added").ConfigureAwait(false);
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2016-10-10 00:27:33 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2017-03-09 15:30:34 +00:00
|
|
|
|
public async Task DeleteQuote(int id)
|
2016-08-24 13:29:01 +00:00
|
|
|
|
{
|
2017-03-09 15:30:34 +00:00
|
|
|
|
var isAdmin = ((IGuildUser) Context.Message.Author).GuildPermissions.Administrator;
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2017-01-29 21:54:27 +00:00
|
|
|
|
var sucess = false;
|
2016-12-17 00:21:05 +00:00
|
|
|
|
string response;
|
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
2016-08-24 13:29:01 +00:00
|
|
|
|
{
|
2017-03-09 15:30:34 +00:00
|
|
|
|
var q = uow.Quotes.Get(id);
|
2016-12-17 00:21:05 +00:00
|
|
|
|
|
2017-03-09 15:30:34 +00:00
|
|
|
|
if (q == null || (!isAdmin && q.AuthorId != Context.Message.Author.Id))
|
2016-12-17 00:21:05 +00:00
|
|
|
|
{
|
2017-02-23 02:06:37 +00:00
|
|
|
|
response = GetText("quotes_remove_none");
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2017-01-29 21:54:27 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
uow.Quotes.Remove(q);
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
sucess = true;
|
2017-03-09 15:30:34 +00:00
|
|
|
|
response = GetText("quote_deleted", id);
|
2017-01-29 21:54:27 +00:00
|
|
|
|
}
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2017-03-09 15:30:34 +00:00
|
|
|
|
if (sucess)
|
2017-01-29 21:54:27 +00:00
|
|
|
|
await Context.Channel.SendConfirmAsync(response);
|
|
|
|
|
else
|
|
|
|
|
await Context.Channel.SendErrorAsync(response);
|
2016-08-24 13:29:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
|
|
|
public async Task DelAllQuotes([Remainder] string keyword)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(keyword))
|
|
|
|
|
return;
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
keyword = keyword.ToUpperInvariant();
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
|
|
|
|
{
|
2017-03-09 15:30:34 +00:00
|
|
|
|
uow.Quotes.RemoveAllByKeyword(Context.Guild.Id, keyword.ToUpperInvariant());
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2016-12-17 00:21:05 +00:00
|
|
|
|
await uow.CompleteAsync();
|
|
|
|
|
}
|
2016-08-24 13:29:01 +00:00
|
|
|
|
|
2017-02-23 02:06:37 +00:00
|
|
|
|
await ReplyConfirmLocalized("quotes_deleted", Format.Bold(keyword)).ConfigureAwait(false);
|
2016-12-17 00:21:05 +00:00
|
|
|
|
}
|
2016-08-24 13:29:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-09 15:30:34 +00:00
|
|
|
|
}
|