From e83c35027d40acccc03bcd754035ebbb1b8ac28d Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 25 Feb 2016 10:57:11 +0100 Subject: [PATCH] added `.. [keyword] [text]` and `... [keyword]` quote system. closes #60 --- NadekoBot/Classes/DBHandler.cs | 10 +++++ .../Classes/_DataModels/UserQuoteModel.cs | 13 +++++++ NadekoBot/Modules/Conversations.cs | 37 +++++++++++++++++++ NadekoBot/NadekoBot.csproj | 1 + 4 files changed, 61 insertions(+) create mode 100644 NadekoBot/Classes/_DataModels/UserQuoteModel.cs diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index e20c1fe0..bab5ad24 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -2,6 +2,8 @@ using System.Linq; using SQLite; using NadekoBot.Classes._DataModels; +using System; +using System.Linq.Expressions; namespace NadekoBot.Classes { class DBHandler { @@ -21,6 +23,7 @@ namespace NadekoBot.Classes { _conn.CreateTable(); _conn.CreateTable(); _conn.CreateTable(); + _conn.CreateTable(); _conn.Execute(Queries.TransactionTriggerQuery); } } @@ -76,6 +79,13 @@ namespace NadekoBot.Classes { _conn.Update(o, typeof(T)); } } + + internal T GetRandom(Expression> p) where T : IDataModel, new() { + using (var _conn = new SQLiteConnection(_filePath)) { + var r = new Random(); + return _conn.Table().Where(p).ToList().OrderBy(x => r.Next()).FirstOrDefault(); + } + } } } diff --git a/NadekoBot/Classes/_DataModels/UserQuoteModel.cs b/NadekoBot/Classes/_DataModels/UserQuoteModel.cs new file mode 100644 index 00000000..caa463f9 --- /dev/null +++ b/NadekoBot/Classes/_DataModels/UserQuoteModel.cs @@ -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; } + } +} diff --git a/NadekoBot/Modules/Conversations.cs b/NadekoBot/Modules/Conversations.cs index 902c63db..db55fd2a 100644 --- a/NadekoBot/Modules/Conversations.cs +++ b/NadekoBot/Modules/Conversations.cs @@ -42,6 +42,43 @@ namespace NadekoBot.Modules { .Do(async e => { 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(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 => { diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 91bd6d86..97f7abc2 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -134,6 +134,7 @@ +