/* * jsonobj - data object to read the data from (pass as null if jsonstring = true) * filename - json file to write object to * jsonstring - if already serialised as a string, pass as true with a null jsonobj * */ public static void WriteJsonToFile(object jsonobj, string filename, string jsonstring = null) { string truefilename = Path.Combine(FileDirUtil.GetCurDir(), filename); string json = jsonstring; if (jsonobj != null) { try { json = JsonConvert.SerializeObject(jsonobj); } catch (Exception e) { Console.WriteLine("exception in serialize " + e); } } using (StreamWriter file = File.CreateText(truefilename)) { try { file.Write(json); file.Close(); Console.WriteLine("written " + truefilename); } catch (Exception e) { Console.WriteLine("failed to write to file.. retrying"); System.Threading.Thread.Sleep(1000); file.Write(json); file.Close(); Console.WriteLine("success"); } } }
public static async Task UpdateList(string name, string id, bool remove = true) { string guildpath = FileDirUtil.GetGuildDir(id); string namepath = Path.Combine(guildpath, FileDirUtil.JSONNAMES); string livepath = Path.Combine(guildpath, FileDirUtil.JSONLIVE); string idpath = Path.Combine(guildpath, FileDirUtil.JSONIDS); List <string> names = JSONUtil.GetJsonToList <string>(namepath); Dictionary <string, string> live = JSONUtil.GetJsonToDic <string, string>(livepath); Dictionary <string, string> ids = JSONUtil.GetJsonToDic <string, string>(idpath); if (remove) { names.Remove(name); live.Remove(name); ids.Remove(name); JSONUtil.WriteJsonToFile(names, namepath); JSONUtil.WriteJsonToFile(live, livepath); JSONUtil.WriteJsonToFile(ids, idpath); Console.WriteLine("updated name removal for " + name); } }
public static async Task GetChannelIDs(string id, bool refresh = false) { string guildpath = FileDirUtil.GetGuildDir(id); string namepath = Path.Combine(guildpath, FileDirUtil.JSONNAMES); string livepath = Path.Combine(guildpath, FileDirUtil.JSONLIVE); string idpath = Path.Combine(guildpath, FileDirUtil.JSONIDS); Console.WriteLine("getting channel ids"); Dictionary <string, string> nameid = new Dictionary <string, string>(); Dictionary <string, string> live = JSONUtil.GetJsonToDic <string, string>(livepath); List <string> names = JSONUtil.GetJsonToList <string>(namepath); string searchstring = "tinietinie"; if (names.Count > 1) { searchstring = string.Join("&login="******"users?login="******"Channels"); Console.WriteLine("loginresponse" + response); JSONUtil.WriteJsonToFile(null, Path.Combine(guildpath, FileDirUtil.JSONUSERS), response); Console.WriteLine("written users to file"); Users users = JsonConvert.DeserializeObject <Users>(response); Console.WriteLine("made user obj"); foreach (User u in users.users) { Console.WriteLine("user: "******"is refresh"); if (!live.ContainsKey(u.name)) { Console.WriteLine("new key"); live[u.name] = "offline"; } } else { Console.WriteLine("not refresh, set to offline"); live[u.name] = "offline"; } } JSONUtil.WriteJsonToFile(nameid, idpath); JSONUtil.WriteJsonToFile(live, livepath); }
public static Settings GetSettingsObj(string id) { string settingsfile = FileDirUtil.GetGuildFile(id, FileDirUtil.JSONSETTINGS); return(JsonConvert.DeserializeObject <Settings>(File.ReadAllText(settingsfile))); }
protected void AsyncLogEvent(string row) { File.AppendAllText(FileDirUtil.GetGuildDir(FileDirUtil.EVENTLOG), row); }
protected void AsyncLogProcess(string row, string id) { //Console.WriteLine("writing to file with row: "+row); File.AppendAllText(Path.Combine(FileDirUtil.GetGuildDir(id), FileDirUtil.PROCESSLOG), DateTime.Now + ", " + row + Environment.NewLine); //Console.WriteLine("added to queue"); }