public async Task StartMon(string startfile) { Console.WriteLine("start mon"); while (true) { if (File.Exists(startfile)) { await CheckLive(Context.Message, Context.Guild.Id.ToString()); System.Threading.Thread.Sleep(60000); } else { break; } } //once the monitor stops, remove the stop file string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); try { File.Delete(Path.Combine(guildpath, FileDirUtil.JSONSTOP)); Console.WriteLine("removed stop file"); } catch (Exception e) { Console.WriteLine("could not remove stop file: " + e); } await ReplyAsync("**Monitor stopped**"); }
public async Task AddStream(string streamer) { Console.WriteLine("Executing add command for " + streamer); string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); string namepath = Path.Combine(guildpath, FileDirUtil.JSONNAMES); List <string> names = JSONUtil.GetJsonToList <string>(namepath); //"Link" can be twitch link or just the channel name string name = MessageUtil.GetChannelNameFromMessage(streamer); if (name == null) { await ReplyAsync("Invalid link, please check again"); return; } if (!names.Contains(name)) { names.Add(name); JSONUtil.WriteJsonToFile(names, namepath); await ReplyAsync("Successfully added streamer: " + name); //refresh IDS after adding await MessageUtil.GetChannelIDs(guildid, true); } else { await ReplyAsync("I already have that streamer stored, try !!list to check the current list"); } await Context.Message.DeleteAsync(); Console.WriteLine("leaving addstreamer"); }
public async Task PriceCheck(string coin, string cur = "USD", string expanded = null) { Console.WriteLine("Command: crypto pricecheck"); //get the api-correct name for the coin string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); Dictionary <string, string> coindict = JSONUtil.GetJsonToDic <string, string>(Path.Combine(guildpath, FileDirUtil.COINS)); string coinname = GetFullCoinName(coindict, coin); string message = "Could not find the coin mentioned, try updating my record!"; if (coinname != null) { if (cur == "full") { cur = "usd"; expanded = "full"; } string response = Request(coinname, cur); message = FormatCoinPriceResponse(response, cur.ToLower(), expanded); } await ReplyAsync(message); }
public static Dictionary <string, Dictionary <string, int> > AggregateRecords(SocketCommandContext context) { Console.WriteLine("aggregating records.."); string filepath = Path.Combine(FileDirUtil.GetGuildDir(context.Guild.Id.ToString()), FileDirUtil.PROCESSLOG); Console.WriteLine("reading from the csv file.."); List <ChatRecord> records = File.ReadAllLines(filepath) .Select(v => FromCSV(v)) .ToList(); Dictionary <string, Dictionary <string, int> > aggregate = new Dictionary <string, Dictionary <string, int> >(); // {user: {word1:0..}} Console.WriteLine("looking through records"); foreach (ChatRecord record in records) { if (aggregate.ContainsKey(record.User)) { //Console.WriteLine("Contained key " + record.User); foreach (string word in record.Words.Keys) { //Console.WriteLine("looking at word " + word); if (aggregate[record.User].ContainsKey(word)) { aggregate[record.User][word] += record.Words[word]; } else { aggregate[record.User].Add(word, record.Words[word]); } } } else { aggregate.Add(record.User, record.Words); } } Console.WriteLine("sorting word counts"); //sort the word collections for each user based on the number of instances of each word foreach (KeyValuePair <string, Dictionary <string, int> > pair in aggregate.ToList()) { //var mylist = aggregate[pair.Key].ToList(); //mylist.Sort((x, y) => x.Value.CompareTo(y.Value)); var sortedDict = from entry in aggregate[pair.Key] orderby entry.Value descending select entry; aggregate[pair.Key] = sortedDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); } Console.WriteLine("Aggregated orederd recrods"); return(aggregate); }
private async Task JoinedGuild(SocketGuild guild) { Console.WriteLine("Entered guild "); var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; Guilds guilds = JsonConvert.DeserializeObject <Guilds>(File.ReadAllText(FileDirUtil.JSONGUILDS), settings);//checks json containing all guilds + their settings Console.WriteLine("got guilds json"); string id = guild.Id.ToString(); string name = guild.Name; List <string> ids = guilds.GetIds(); Console.WriteLine("Entered guild " + name + " " + id); //if this is a brand new guild if ((guilds.guilds == null) || !ids.Contains(id)) { string guildpath = FileDirUtil.GetGuildDir(id); string settingfile = Path.Combine(guildpath, FileDirUtil.JSONSETTINGS); Console.WriteLine("new guild!"); Settings gdst = new Settings("!!", id, 0); Console.WriteLine("settings!"); Guild newguilld = new Guild(id, name, DateTime.Now, DateTime.Now); //create the new guild with info Console.WriteLine("newguild!"); guilds.AddGuild(newguilld); Console.WriteLine("addedguild!"); await FileDirUtil.EstablishGuildFiles(newguilld); Console.WriteLine("writing guilds back to file"); JSONUtil.WriteJsonToFile(guilds, FileDirUtil.JSONGUILDS); JSONUtil.WriteJsonToFile(gdst, settingfile); Console.WriteLine("completed setting up for new guild!"); } else { Console.WriteLine("existing guild"); await FileDirUtil.VerifyGuildFiles(id); } //write back the json }
public async Task List() { Console.WriteLine("executing list command"); string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); List <string> names = JSONUtil.GetJsonToList <string>(Path.Combine(guildpath, FileDirUtil.JSONNAMES)); Console.WriteLine("2"); string messagestring = "Here's the current list of streamers i'm looking out for!"; Console.WriteLine("3"); foreach (string name in names) { messagestring += ("\n<https://www.twitch.tv/" + name + ">"); } await ReplyAsync(messagestring); }
public async Task Start() { Console.WriteLine("Executing start command"); string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); string startfile = Path.Combine(guildpath, FileDirUtil.JSONSTART); string stopfile = Path.Combine(guildpath, FileDirUtil.JSONSTOP); //if already running if (File.Exists(startfile)) { await ReplyAsync("Bot already running, if it appears to be broken try to restart"); return; } //if a stop command already issued but not completed, don't try to re-start if (File.Exists(stopfile)) { await ReplyAsync("Awaiting termination of the existing session before restarting, please try again shortly!"); return; } //if not already running + no stop command in progress, start the bot FileStream fs = File.Create(startfile); fs.Flush(); fs.Close(); await MessageUtil.GetChannelIDs(guildid); await Context.Client.SetGameAsync("with yer nan"); await ReplyAsync("Starting up the bot!"); Task monitask = Task.Run(async() => { await StartMon(startfile); }); await Context.Message.DeleteAsync(); }
public async Task Stop() { Console.WriteLine("Executing stop command"); string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); string startfile = Path.Combine(guildpath, FileDirUtil.JSONSTART); string stopfile = Path.Combine(guildpath, FileDirUtil.JSONSTOP); FileStream fs = File.Create(stopfile); fs.Flush(); fs.Close(); if (File.Exists(stopfile)) { await ReplyAsync("Waiting for previous session to end, please try again shortly"); } if (File.Exists(startfile)) { try { File.Delete(startfile); await ReplyAsync("Monitor has been stopped! Please wait up to 1 minute before attempting a restart.."); Console.WriteLine("stopped"); } catch (Exception e) { Console.WriteLine("error trying to delete startfile " + e); await ReplyAsync("Could not stop the monitoring service right now, please retry"); return; } } else { await ReplyAsync("Currently not monitoring the streams, use **start** to begin monitoring!"); } await Context.Client.SetGameAsync("AFK"); await Context.Message.DeleteAsync(); }
public async Task RemoveStream(string streamer) { string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); List <string> names = JSONUtil.GetJsonToList <string>(Path.Combine(guildpath, FileDirUtil.JSONNAMES)); string name = MessageUtil.GetChannelNameFromMessage(streamer); if (names.Contains(name)) { await MessageUtil.UpdateList(name, guildid, true); await ReplyAsync("Successfully deleted streamer: " + name); } else { await ReplyAsync("Please try again, streamer could not be found!"); } await Context.Message.DeleteAsync(); Console.WriteLine("leaving deletestreamer"); }