NadekoBot/NadekoBot.Modules.Utility/QuoteCommands.cs

218 lines
8.0 KiB
C#
Raw Normal View History

2017-04-02 19:43:02 +00:00
using Discord;
2016-08-24 13:29:01 +00:00
using Discord.Commands;
using NadekoBot.Extensions;
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
{
private readonly DbService _db;
2017-05-22 23:59:31 +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())
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()}")))
.ConfigureAwait(false);
2016-12-17 00:21:05 +00:00
else
await ReplyErrorLocalized("quotes_page_none").ConfigureAwait(false);
2016-12-17 00:21:05 +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
{
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
var rep = new ReplacementBuilder()
.WithDefault(Context)
.Build();
if (CREmbed.TryParse(quote.Text, out var crembed))
2017-02-17 14:06:44 +00:00
{
rep.Replace(crembed);
await Context.Channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText?.SanitizeMentions() ?? "")
.ConfigureAwait(false);
2017-02-17 14:06:44 +00:00
return;
}
await Context.Channel.SendMessageAsync($"`#{quote.Id}` 📣 " + rep.Replace(quote.Text)?.SanitizeMentions());
2016-12-17 00:21:05 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task QuoteSearch(string keyword, [Remainder] string text)
2017-02-14 04:43:35 +00:00
{
if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text))
return;
keyword = keyword.ToUpperInvariant();
Quote keywordquote;
2017-05-22 23:59:31 +00:00
using (var uow = _db.UnitOfWork)
{
keywordquote =
await uow.Quotes.SearchQuoteKeywordTextAsync(Context.Guild.Id, keyword, text)
.ConfigureAwait(false);
}
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() + ": " +
keywordquote.Text.SanitizeMentions());
}
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);
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"));
}
else if (CREmbed.TryParse(qfromid.Text, out var crembed))
{
rep.Replace(crembed);
await Context.Channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText?.SanitizeMentions() ?? "")
.ConfigureAwait(false);
}
else
2017-04-02 19:43:02 +00:00
{
await Context.Channel.SendMessageAsync($"`#{qfromid.Id}` 🗯️ " + qfromid.Keyword.ToLowerInvariant().SanitizeMentions() + ": " +
rep.Replace(qfromid.Text)?.SanitizeMentions());
2017-04-02 19:43:02 +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);
}
await ReplyConfirmLocalized("quote_added").ConfigureAwait(false);
2016-12-17 00:21:05 +00:00
}
2016-12-17 00:21:05 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task QuoteDelete(int id)
2016-08-24 13:29:01 +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
{
var q = uow.Quotes.Get(id);
2016-12-17 00:21:05 +00:00
if (q == null || (!isAdmin && q.AuthorId != Context.Message.Author.Id))
2016-12-17 00:21:05 +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;
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
{
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
}