From 2d52d188478203a0aa6a935b1fd7ae163c1951dc Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 8 Feb 2016 00:41:42 +0100 Subject: [PATCH] Cleanup, .parsetosql will merge parse data from data/parsedata to sql --- NadekoBot/Classes/DBHandler.cs | 10 ++++++ .../Classes/_DataModels/AnnouncementModel.cs | 6 +++- NadekoBot/Classes/_DataModels/CommandModel.cs | 1 + NadekoBot/Classes/_DataModels/RequestModel.cs | 2 ++ NadekoBot/Classes/_DataModels/StatsModel.cs | 1 + .../Classes/_DataModels/TypingArticleModel.cs | 4 ++- NadekoBot/Commands/SpeedTyping.cs | 2 +- NadekoBot/Modules/Administration.cs | 34 ++++++++++++++++--- NadekoBot/Modules/Conversations.cs | 5 +-- 9 files changed, 54 insertions(+), 11 deletions(-) diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index 91812e8e..e3e52861 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -31,6 +31,16 @@ namespace NadekoBot.Classes { } } + internal void InsertMany(T objects) where T : IEnumerable { + try { + using (var _conn = new SQLiteConnection(_filePath)) { + _conn.InsertAll(objects); + } + } catch (Exception ex) { + Console.WriteLine(ex); + } + } + internal void UpdateData(T o) where T : IDataModel { using (var _conn = new SQLiteConnection(_filePath)) { _conn.Update(o, typeof(T)); diff --git a/NadekoBot/Classes/_DataModels/AnnouncementModel.cs b/NadekoBot/Classes/_DataModels/AnnouncementModel.cs index 25db6842..39d9bfd8 100644 --- a/NadekoBot/Classes/_DataModels/AnnouncementModel.cs +++ b/NadekoBot/Classes/_DataModels/AnnouncementModel.cs @@ -1,4 +1,5 @@ -using SQLite; +using Newtonsoft.Json; +using SQLite; using System; using System.Collections.Generic; using System.Linq; @@ -10,12 +11,15 @@ namespace NadekoBot.Classes._DataModels { public long ServerId { get; set; } = 0; public bool Greet { get; set; } = false; public bool GreetPM { get; set; } = false; + [JsonProperty("greetChannel")] public long GreetChannelId { get; set; } = 0; public string GreetText { get; set; } = "Welcome %user%!"; public bool Bye { get; set; } = false; public bool ByePM { get; set; } = false; + [JsonProperty("byeChannel")] public long ByeChannelId { get; set; } = 0; public string ByeText { get; set; } = "%user% has left the server."; + [JsonProperty("createdAt")] public DateTime DateAdded { get; set; } = DateTime.Now; } } diff --git a/NadekoBot/Classes/_DataModels/CommandModel.cs b/NadekoBot/Classes/_DataModels/CommandModel.cs index d035a7e9..7c40adc3 100644 --- a/NadekoBot/Classes/_DataModels/CommandModel.cs +++ b/NadekoBot/Classes/_DataModels/CommandModel.cs @@ -14,6 +14,7 @@ namespace NadekoBot.Classes._DataModels { public long ChannelId { get; set; } public string ChannelName { get; set; } public string CommandName { get; set; } + [Newtonsoft.Json.JsonProperty("createdAt")] public DateTime DateAdded { get; set; } } } diff --git a/NadekoBot/Classes/_DataModels/RequestModel.cs b/NadekoBot/Classes/_DataModels/RequestModel.cs index 3f70b868..87efd72a 100644 --- a/NadekoBot/Classes/_DataModels/RequestModel.cs +++ b/NadekoBot/Classes/_DataModels/RequestModel.cs @@ -11,7 +11,9 @@ namespace NadekoBot.Classes._DataModels { public long UserId { get; set; } public string ServerName { get; set; } public long ServerId { get; set; } + [Newtonsoft.Json.JsonProperty("Request")] public string RequestText { get; set; } + [Newtonsoft.Json.JsonProperty("createdAt")] public DateTime DateAdded { get; set; } } } diff --git a/NadekoBot/Classes/_DataModels/StatsModel.cs b/NadekoBot/Classes/_DataModels/StatsModel.cs index e7e3869e..d54bfab0 100644 --- a/NadekoBot/Classes/_DataModels/StatsModel.cs +++ b/NadekoBot/Classes/_DataModels/StatsModel.cs @@ -11,6 +11,7 @@ namespace NadekoBot.Classes._DataModels { public int OnlineUsers { get; set; } public TimeSpan Uptime { get; set; } public int RealOnlineUsers { get; set; } + [Newtonsoft.Json.JsonProperty("createdAt")] public DateTime DateAdded { get; set; } } } diff --git a/NadekoBot/Classes/_DataModels/TypingArticleModel.cs b/NadekoBot/Classes/_DataModels/TypingArticleModel.cs index 385d682f..1283255a 100644 --- a/NadekoBot/Classes/_DataModels/TypingArticleModel.cs +++ b/NadekoBot/Classes/_DataModels/TypingArticleModel.cs @@ -1,4 +1,5 @@ -using SQLite; +using Newtonsoft.Json; +using SQLite; using System; using System.Collections.Generic; using System.Linq; @@ -8,6 +9,7 @@ using System.Threading.Tasks; namespace NadekoBot.Classes._DataModels { class TypingArticle : IDataModel { public string Text { get; set; } + [JsonProperty("createdAt")] public DateTime DateAdded { get; set; } } } diff --git a/NadekoBot/Commands/SpeedTyping.cs b/NadekoBot/Commands/SpeedTyping.cs index 88986f40..a7474415 100644 --- a/NadekoBot/Commands/SpeedTyping.cs +++ b/NadekoBot/Commands/SpeedTyping.cs @@ -155,7 +155,7 @@ namespace NadekoBot.Commands { DateAdded = DateTime.Now }); - await e.Send("Added new article for typing."); + await e.Send("Added new article for typing game."); }); //todo add user submissions diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index 8310ec41..83cbde15 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -10,15 +10,15 @@ using System.Threading.Tasks; using NadekoBot.Commands; using System.IO; using System.Collections.Concurrent; +using Newtonsoft.Json.Linq; +using System.Collections.Generic; +using NadekoBot.Classes._DataModels; namespace NadekoBot.Modules { class Administration : DiscordModule { public Administration() : base() { commands.Add(new HelpCommand()); - if (NadekoBot.ParseActive) - commands.Add(new ServerGreetCommand()); - else - Console.WriteLine("Parse not active. Server greet disabled."); + commands.Add(new ServerGreetCommand()); } public override void Install(ModuleManager manager) { @@ -443,6 +443,7 @@ namespace NadekoBot.Modules { clearDictionary.TryRemove(e.Server, out throwaway); }); cgb.CreateCommand(".newname") + .Alias(".setname") .Description("Give the bot a new name.") .Parameter("new_name", ParameterType.Unparsed) .Do(async e => { @@ -549,6 +550,21 @@ namespace NadekoBot.Modules { } await e.Channel.Send(send); }); + + cgb.CreateCommand(".parsetosql") + .Description("Loads exported parsedata from /data/parsedata/ into sqlite database.") + .Do(async e => { + if (e.User.Id != NadekoBot.OwnerID) + return; + await Task.Run(() => { + SaveParseToDb("data/parsedata/Announcements.json"); + SaveParseToDb("data/parsedata/CommandsRan.json"); + SaveParseToDb("data/parsedata/Requests.json"); + SaveParseToDb("data/parsedata/Stats.json"); + SaveParseToDb("data/parsedata/TypingArticles.json"); + }); + }); + /*cgb.CreateCommand(".voicetext") .Description("Enabled or disabled voice to text channel connection. Only people in a certain voice channel will see ") @@ -583,5 +599,15 @@ namespace NadekoBot.Modules { */ }); } + + public void SaveParseToDb(string where) where T : IDataModel { + var data = File.ReadAllText(where); + var arr = JObject.Parse(data)["results"] as JArray; + var objects = new List(); + foreach (JObject obj in arr) { + objects.Add(obj.ToObject()); + } + Classes.DBHandler.Instance.InsertMany(objects); + } } } diff --git a/NadekoBot/Modules/Conversations.cs b/NadekoBot/Modules/Conversations.cs index 442b2aac..6552dc66 100644 --- a/NadekoBot/Modules/Conversations.cs +++ b/NadekoBot/Modules/Conversations.cs @@ -20,10 +20,7 @@ namespace NadekoBot.Modules { private string firestr = "🔥 ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็็็็้้้้ 🔥"; public Conversations() : base() { commands.Add(new CopyCommand()); - if (NadekoBot.ParseActive) - commands.Add(new RequestsCommand()); - else - Console.WriteLine("Requests don't work, parse not valid."); + commands.Add(new RequestsCommand()); } public override void Install(ModuleManager manager) {