public async Task Logs(string text, [Remainder] string options = "") { string[] keywords = text.Split(","); StatsOptions modifiers = new StatsOptions(options); var rawLogs = GetLogs(keywords, modifiers); var logs = rawLogs.OrderBy(kvp => - kvp.Value.Count); using (StreamWriter file = new StreamWriter($@"./MessageData/Logs.txt")) { file.WriteLine("\n-------------------------------------------------"); foreach (var kvp in logs) { List <string> messages = kvp.Value; file.WriteLine($"\nUser: {kvp.Key}\nMessages: {messages.Count}\n\n*\n"); foreach (var msg in messages) { file.WriteLine(msg); file.WriteLine("\n*\n"); } file.WriteLine("-------------------------------------------------"); } } var embed = new EmbedBuilder(); await Context.Channel.SendFileAsync(@"./MessageData/Logs.txt"); }
public async Task Stats(string text, [Remainder] string options = "") { string[] keywords = text.Split(","); StatsOptions modifiers = new StatsOptions(options); var counts = GetCounts(keywords, modifiers); var orderedCounts = counts.OrderBy(kvp => - kvp.Value); // sort usages w/ highest first int total = 0; foreach (KeyValuePair <string, int> entry in counts) { total += entry.Value; } string leaderboard = ""; for (int i = 0; i < counts.Count; i++) { var(name, num) = orderedCounts.ElementAt(i); leaderboard += $" {i+1}. {name}: {num}\n"; } string response = $@"```c Top 10 statistics for ""{text}"": {leaderboard} Total Occurrences: {total}```"; await ReplyAsync(response); }
// returns an <string:string list> dictionary of users to messages containing the desired token public Dictionary <string, List <string> > GetLogs(string[] keywords, StatsOptions modifiers) { var counts = new Dictionary <string, List <string> >(); string[] allLogs = Directory.GetFiles("./MessageData/BotkicLogs/DiscordLogs", "*.json"); // iterate through all json logs foreach (string filePath in allLogs) { Quotes logs; using (StreamReader file = File.OpenText(filePath)) { JsonSerializer serializer = new JsonSerializer(); logs = (Quotes)serializer.Deserialize(file, typeof(Quotes)); } // iterate through all keywords and messages within a channel foreach (Message msg in logs.Messages) { string content = ParseMsg(msg.Content, modifiers.caseSensitive); foreach (string word in keywords) { string substr = ParseMsg(word, modifiers.caseSensitive); int matches = MatchWord(content, substr, modifiers.inclSubstrings, modifiers.ignoreRepeats); if (matches > 0 && (modifiers.inclBots || !msg.Author.IsBot)) { if (counts.ContainsKey(msg.Author.Username)) { counts[msg.Author.Username].Add(msg.Content); } else { var msgList = new List <string>(); msgList.Add(msg.Content); counts.Add(msg.Author.Username, msgList); } } } } } return(counts); }
public async Task Leaderboard(string text, [Remainder] string options = "") { string[] keywords = text.Split(","); StatsOptions modifiers = new StatsOptions(options); var counts = GetCounts(keywords, modifiers); var totals = GetCounts(new String[] { "" }, modifiers); // total message counts var proportions = new Dictionary <string, float>(); // get total usage and total messages sent var(totalCounts, totalMsgs) = (0, 0); foreach (var entry in counts) { totalCounts += entry.Value; } foreach (var entry in totals) { totalMsgs += entry.Value; } // put proportions into a new dictionary and sort foreach (KeyValuePair <string, int> entry in counts) { var(count, total) = (0, 0); string name = entry.Key; if (counts.ContainsKey(name)) { count = counts[name]; } if (totals.ContainsKey(name)) { total = totals[name]; } proportions.Add(name, (float)count / total); } var orderedProportions = proportions.OrderBy(kvp => - kvp.Value); // find the length of the longest username int maxNameLength = 0; foreach (var name in counts.Keys) { int len = name.Length; if (maxNameLength < len) { maxNameLength = len; } } // make and outboard the leaderboard string leaderboard = $" Rank | {PadStr("Username", maxNameLength)} | Total Uses | Total Msgs | Uses per 1000 Msgs \n"; leaderboard += $"------+{new string ('-', maxNameLength + 2)}+------------+------------+--------------------\n"; for (int i = 0; i < counts.Count; i++) { var(name, num) = orderedProportions.ElementAt(i); float ratio = 1000 * (float)counts[name] / (float)totals[name]; string rankStr = PadStr((i + 1).ToString(), 4); string nameStr = PadStr(name, maxNameLength); string countStr = PadStr(counts[name].ToString(), 10); string totalStr = PadStr(totals[name].ToString(), 10); string ratioStr = PadStr(ratio.ToString("0.00"), 18); leaderboard += $" {rankStr} | {nameStr} | {countStr} | {totalStr} | {ratioStr} \n"; } string response = $@"```c Usage leaderboard for ""{text}"": {leaderboard} Total Occurrences: {totalCounts} out of {totalMsgs} messages```"; await ReplyAsync(response); }