2017-04-02 19:43:02 +00:00
|
|
|
using Discord;
|
2016-08-24 13:29:01 +00:00
|
|
|
using Discord.Commands;
|
2016-10-09 20:40:14 +00:00
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2016-08-24 13:29:01 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
using NadekoBot.Common;
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
using NadekoBot.Common.Replacements;
|
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
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
private readonly DbService _db;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
public QuoteCommands(DbService db)
|
2017-05-22 23:59:31 +00:00
|
|
|
{
|
|
|
|
_db = db;
|
|
|
|
}
|
|
|
|
|
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;
|
2017-05-22 23:59:31 +00:00
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-12-17 00:21:05 +00:00
|
|
|
{
|
|
|
|
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),
|
2017-04-10 01:02:46 +00:00
|
|
|
string.Join("\n", quotes.Select(q => $"`#{q.Id}` {Format.Bold(q.Keyword.SanitizeMentions()),-20} by {q.AuthorName.SanitizeMentions()}")))
|
2017-03-09 15:30:34 +00:00
|
|
|
.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-05-22 23:59:31 +00:00
|
|
|
using (var uow = _db.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-06-28 01:19:16 +00:00
|
|
|
var rep = new ReplacementBuilder()
|
|
|
|
.WithDefault(Context)
|
|
|
|
.Build();
|
|
|
|
|
2017-06-28 00:44:30 +00:00
|
|
|
if (CREmbed.TryParse(quote.Text, out var crembed))
|
2017-02-17 14:06:44 +00:00
|
|
|
{
|
2017-06-28 01:19:16 +00:00
|
|
|
rep.Replace(crembed);
|
2017-06-28 00:44:30 +00:00
|
|
|
await Context.Channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText?.SanitizeMentions() ?? "")
|
|
|
|
.ConfigureAwait(false);
|
2017-02-17 14:06:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-28 01:19:16 +00:00
|
|
|
await Context.Channel.SendMessageAsync($"`#{quote.Id}` 📣 " + rep.Replace(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-06-02 01:44:59 +00:00
|
|
|
public async Task QuoteSearch(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;
|
2017-05-22 23:59:31 +00:00
|
|
|
using (var uow = _db.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-22 06:08:08 +00:00
|
|
|
await Context.Channel.SendMessageAsync($"`#{keywordquote.Id}` 💬 " + keyword.ToLowerInvariant() + ": " +
|
2017-03-09 15:30:34 +00:00
|
|
|
keywordquote.Text.SanitizeMentions());
|
2017-02-14 03:30:16 +00:00
|
|
|
}
|
2017-04-02 19:43:02 +00:00
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
public async Task QuoteId(int id)
|
|
|
|
{
|
2017-04-02 19:57:56 +00:00
|
|
|
if (id < 0)
|
2017-04-02 19:43:02 +00:00
|
|
|
return;
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-04-02 19:43:02 +00:00
|
|
|
{
|
|
|
|
var qfromid = uow.Quotes.Get(id);
|
2017-06-28 00:44:30 +00:00
|
|
|
|
2017-06-28 01:19:16 +00:00
|
|
|
var rep = new ReplacementBuilder()
|
|
|
|
.WithDefault(Context)
|
|
|
|
.Build();
|
|
|
|
|
2017-04-02 19:43:02 +00:00
|
|
|
if (qfromid == null)
|
|
|
|
{
|
|
|
|
await Context.Channel.SendErrorAsync(GetText("quotes_notfound"));
|
|
|
|
}
|
2017-06-28 00:44:30 +00:00
|
|
|
else if (CREmbed.TryParse(qfromid.Text, out var crembed))
|
|
|
|
{
|
2017-06-28 01:19:16 +00:00
|
|
|
rep.Replace(crembed);
|
2017-06-28 00:44:30 +00:00
|
|
|
|
|
|
|
await Context.Channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText?.SanitizeMentions() ?? "")
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
2017-04-02 19:43:02 +00:00
|
|
|
{
|
2017-06-28 00:44:30 +00:00
|
|
|
await Context.Channel.SendMessageAsync($"`#{qfromid.Id}` 🗯️ " + qfromid.Keyword.ToLowerInvariant().SanitizeMentions() + ": " +
|
2017-06-28 01:19:16 +00:00
|
|
|
rep.Replace(qfromid.Text)?.SanitizeMentions());
|
2017-04-02 19:43:02 +00:00
|
|
|
}
|
2017-06-28 00:44:30 +00:00
|
|
|
|
2017-04-02 19:43:02 +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
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-12-17 00:21:05 +00:00
|
|
|
{
|
|
|
|
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-06-15 20:35:12 +00:00
|
|
|
public async Task QuoteDelete(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;
|
2017-04-02 19:43:02 +00:00
|
|
|
|
|
|
|
var success = false;
|
2016-12-17 00:21:05 +00:00
|
|
|
string response;
|
2017-05-22 23:59:31 +00:00
|
|
|
using (var uow = _db.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);
|
2017-04-02 19:43:02 +00:00
|
|
|
success = 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-04-02 19:43:02 +00:00
|
|
|
if (success)
|
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
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-12-17 00:21:05 +00:00
|
|
|
{
|
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-04-10 01:02:46 +00:00
|
|
|
await ReplyConfirmLocalized("quotes_deleted", Format.Bold(keyword.SanitizeMentions())).ConfigureAwait(false);
|
2016-12-17 00:21:05 +00:00
|
|
|
}
|
2016-08-24 13:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 06:08:08 +00:00
|
|
|
}
|