Cleanup, .parsetosql will merge parse data from data/parsedata to sql

This commit is contained in:
Master Kwoth 2016-02-08 00:41:42 +01:00
parent f61606f8bc
commit 2d52d18847
9 changed files with 54 additions and 11 deletions

View File

@ -31,6 +31,16 @@ namespace NadekoBot.Classes {
}
}
internal void InsertMany<T>(T objects) where T : IEnumerable<IDataModel> {
try {
using (var _conn = new SQLiteConnection(_filePath)) {
_conn.InsertAll(objects);
}
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
internal void UpdateData<T>(T o) where T : IDataModel {
using (var _conn = new SQLiteConnection(_filePath)) {
_conn.Update(o, typeof(T));

View File

@ -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;
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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

View File

@ -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.");
}
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<Announcement>("data/parsedata/Announcements.json");
SaveParseToDb<Classes._DataModels.Command>("data/parsedata/CommandsRan.json");
SaveParseToDb<Request>("data/parsedata/Requests.json");
SaveParseToDb<Stats>("data/parsedata/Stats.json");
SaveParseToDb<TypingArticle>("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<T>(string where) where T : IDataModel {
var data = File.ReadAllText(where);
var arr = JObject.Parse(data)["results"] as JArray;
var objects = new List<T>();
foreach (JObject obj in arr) {
objects.Add(obj.ToObject<T>());
}
Classes.DBHandler.Instance.InsertMany(objects);
}
}
}

View File

@ -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.");
}
public override void Install(ModuleManager manager) {