NadekoBot/src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs

179 lines
6.5 KiB
C#
Raw Normal View History

2016-08-24 13:29:01 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
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())
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
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;
using (var uow = DbHandler.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
2017-02-17 14:06:44 +00:00
CREmbed crembed;
if (CREmbed.TryParse(quote.Text, out crembed))
{
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;
}
await Context.Channel.SendMessageAsync($"`#{quote.Id}` 📣 " + quote.Text.SanitizeMentions());
2016-12-17 00:21:05 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task SearchQuote(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;
using (var uow = DbHandler.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
await Context.Channel.SendMessageAsync("💬 " + keyword.ToLowerInvariant() + ": " +
keywordquote.Text.SanitizeMentions());
}
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);
}
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 DeleteQuote(int id)
2016-08-24 13:29:01 +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
{
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);
sucess = true;
response = GetText("quote_deleted", id);
2017-01-29 21:54:27 +00:00
}
2016-12-17 00:21:05 +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())
{
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
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
}
}
}