Db stuff, quotes implemented?

This commit is contained in:
Kwoth
2016-08-24 15:29:01 +02:00
parent 03ac084437
commit 2a2d26513a
14 changed files with 199 additions and 31 deletions

View File

@@ -313,16 +313,6 @@ $@"🌍 **Weather for** 【{obj["target"]}】
await channel.SendMessageAsync("💢 Failed finding a definition for that tag.").ConfigureAwait(false);
}
}
//todo DB
//[LocalizedCommand, LocalizedDescription, LocalizedSummary]
//[RequireContext(ContextType.Guild)]
//public async Task Quote(IMessage imsg)
//{
// var channel = (ITextChannel)imsg.Channel;
// var quote = NadekoBot.Config.Quotes[rng.Next(0, NadekoBot.Config.Quotes.Count)].ToString();
// await channel.SendMessageAsync(quote).ConfigureAwait(false);
//}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]

View File

@@ -0,0 +1,114 @@
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Utility.Commands
{
public partial class Utility
{
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task Quote(IMessage imsg, string keyword)
{
var channel = imsg.Channel as ITextChannel;
if (string.IsNullOrWhiteSpace(keyword))
return;
Quote quote;
using (var uow = DbHandler.Instance.GetUnitOfWork())
{
quote = await uow.Quotes.GetRandomQuoteByKeywordAsync(channel.Guild.Id, keyword);
}
if (quote == null)
return;
await channel.SendMessageAsync(":megaphone: " + quote.Text);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task AddQuote(IMessage imsg, string keyword, [Remainder] string text)
{
var channel = imsg.Channel as ITextChannel;
if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text))
return;
keyword = keyword.ToUpperInvariant();
using (var uow = DbHandler.UnitOfWork())
{
uow.Quotes.Add(new Quote
{
AuthorId = imsg.Author.Id,
AuthorName = imsg.Author.Username,
GuildId = channel.Guild.Id,
Keyword = keyword,
Text = text,
});
await uow.CompleteAsync();
await channel.SendMessageAsync("`Quote added.`");
}
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task DelQuote(IMessage imsg, string keyword)
{
var channel = imsg.Channel as ITextChannel;
if (string.IsNullOrWhiteSpace(keyword))
return;
keyword = keyword.ToUpperInvariant();
using (var uow = DbHandler.UnitOfWork())
{
var q = await uow.Quotes.GetRandomQuoteByKeywordAsync(channel.Guild.Id, keyword);
if (q == null)
{
await channel.SendMessageAsync("`No quotes found.`");
return;
}
uow.Quotes.Remove(q);
await uow.CompleteAsync();
}
await channel.SendMessageAsync("`Deleted a random quote.`");
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task DelAllQuotes(IMessage imsg, string keyword)
{
var channel = imsg.Channel as ITextChannel;
if (string.IsNullOrWhiteSpace(keyword))
return;
keyword = keyword.ToUpperInvariant();
using (var uow = DbHandler.UnitOfWork())
{
var quotes = uow.Quotes.GetAllQuotesByKeyword(keyword);
uow.Quotes.RemoveRange(quotes.Select(q => q.Id).ToArray());//wtf?!
await uow.CompleteAsync();
}
await channel.SendMessageAsync($"`Deleted all quotes with '{keyword}' keyword`");
}
}
}