added .. [keyword] [text] and ... [keyword] quote system. closes #60

This commit is contained in:
Master Kwoth 2016-02-25 10:57:11 +01:00
parent 99d8e09ec9
commit e83c35027d
4 changed files with 61 additions and 0 deletions

View File

@ -2,6 +2,8 @@
using System.Linq; using System.Linq;
using SQLite; using SQLite;
using NadekoBot.Classes._DataModels; using NadekoBot.Classes._DataModels;
using System;
using System.Linq.Expressions;
namespace NadekoBot.Classes { namespace NadekoBot.Classes {
class DBHandler { class DBHandler {
@ -21,6 +23,7 @@ namespace NadekoBot.Classes {
_conn.CreateTable<CurrencyState>(); _conn.CreateTable<CurrencyState>();
_conn.CreateTable<CurrencyTransaction>(); _conn.CreateTable<CurrencyTransaction>();
_conn.CreateTable<Donator>(); _conn.CreateTable<Donator>();
_conn.CreateTable<UserQuote>();
_conn.Execute(Queries.TransactionTriggerQuery); _conn.Execute(Queries.TransactionTriggerQuery);
} }
} }
@ -76,6 +79,13 @@ namespace NadekoBot.Classes {
_conn.Update(o, typeof(T)); _conn.Update(o, typeof(T));
} }
} }
internal T GetRandom<T>(Expression<Func<T, bool>> p) where T : IDataModel, new() {
using (var _conn = new SQLiteConnection(_filePath)) {
var r = new Random();
return _conn.Table<T>().Where(p).ToList().OrderBy(x => r.Next()).FirstOrDefault();
}
}
} }
} }

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Classes._DataModels {
class UserQuote : IDataModel {
public string UserName { get; set; }
public string Keyword { get; set; }
public string Text { get; set; }
}
}

View File

@ -42,6 +42,43 @@ namespace NadekoBot.Modules {
.Do(async e => { .Do(async e => {
await e.Channel.SendMessage(e.User.Mention + "\\o\\"); await e.Channel.SendMessage(e.User.Mention + "\\o\\");
}); });
cgb.CreateCommand("..")
.Description("Adds a new quote with the specified name (single word) and message (no limit).\n**Usage**: .. abc My message")
.Parameter("keyword", ParameterType.Required)
.Parameter("text", ParameterType.Unparsed)
.Do(async e => {
var keyword = e.GetArg("keyword");
var text = e.GetArg("text");
if (string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(keyword))
return;
Classes.DBHandler.Instance.InsertData(new Classes._DataModels.UserQuote() {
DateAdded = DateTime.Now,
Keyword = keyword.ToLowerInvariant(),
Text = text,
UserName = e.User.Name,
});
await e.Channel.SendMessage("`New quote added.`");
});
cgb.CreateCommand("...")
.Description("Shows a random quote with a specified name.\n**Usage**: .. abc")
.Parameter("keyword", ParameterType.Required)
.Do(async e => {
var keyword = e.GetArg("keyword")?.ToLowerInvariant();
if (string.IsNullOrWhiteSpace(keyword))
return;
var quote = Classes.DBHandler.Instance.GetRandom<Classes._DataModels.UserQuote>(uqm => uqm.Keyword == keyword);
if (quote != null)
await e.Channel.SendMessage($"📣 {quote.Text}");
else
await e.Channel.SendMessage("💢`No quote found.`");
});
}); });
manager.CreateCommands(NadekoBot.botMention, cgb => { manager.CreateCommands(NadekoBot.botMention, cgb => {

View File

@ -134,6 +134,7 @@
<Compile Include="Classes\_DataModels\RequestModel.cs" /> <Compile Include="Classes\_DataModels\RequestModel.cs" />
<Compile Include="Classes\_DataModels\StatsModel.cs" /> <Compile Include="Classes\_DataModels\StatsModel.cs" />
<Compile Include="Classes\_DataModels\TypingArticleModel.cs" /> <Compile Include="Classes\_DataModels\TypingArticleModel.cs" />
<Compile Include="Classes\_DataModels\UserQuoteModel.cs" />
<Compile Include="Commands\ClashOfClans.cs" /> <Compile Include="Commands\ClashOfClans.cs" />
<Compile Include="Commands\LogCommand.cs" /> <Compile Include="Commands\LogCommand.cs" />
<Compile Include="Commands\LoLCommands.cs" /> <Compile Include="Commands\LoLCommands.cs" />