public static async Task Calc(SocketMessage arg) { try { RestUserMessage msg = await arg.Channel.SendMessageAsync($"Syncing.."); double allWeight = 0; bool canRecurse = !(arg.Content.Contains(' ') && int.TryParse(arg.Content.Split(' ')[1], out int _)); int thisReq = arg.Content.Contains(' ') && int.TryParse(arg.Content.Split(' ')[1], out int _) ? int.Parse(arg.Content.Split(' ')[1]) : 100; pie_result foodA = await GrabFood(arg.Author.Id, thisReq, canRecurse); Dictionary <string, double> allNames = new Dictionary <string, double>(); foreach (var foodItem in foodA.value) { allWeight += foodItem.Weight; if (foodItem != null) { if (allNames.ContainsKey(foodItem.Name.ToUpper())) { allNames[foodItem.Name.ToUpper()] += foodItem.Weight; } else { allNames.Add(foodItem.Name.ToUpper(), foodItem.Weight); } } else { foodA.value.Remove(foodItem); } } var highest5 = allNames.OrderByDescending(pair => pair.Value).Take(5).ToDictionary(pair => pair.Key, pair => pair.Value); bool isSuc = await save.SaveOne(Program.idIndexUser[arg.Author.Id]); await msg.ModifyAsync(z => { string a = ""; foreach (var key in highest5.Keys) { a += $"\n{key.ToLower()} [{(float)(highest5[key] / allWeight) * 100}%] `x{highest5[key]}`"; } z.Embed = new EmbedBuilder() { ThumbnailUrl = arg.Author.GetAvatarUrl(ImageFormat.Auto), Color = new Color(0x7dffba), Footer = new EmbedFooterBuilder() { Text = $"since {foodA.value.Last().ConsumedAt.ToShortDateString()}", }, Description = a, }.Build(); //z.Content = a + $"\n`{foodA.numOfMessagesChecked}` messages analyzed\nsync service `{isSuc}`"; }); } catch (Exception e) { Console.WriteLine(e); } }
public static async Task <pie_result> GrabFood(ulong userId, int thisReq = 300, bool recurse = true, bool clean = false) { pie_result pie = new pie_result(); try { if (Program.idIndexUser[userId].channelId == 0) { return new pie_result() { numOfMessagesChecked = 0, numOfMessagesDeleted = 0, value = new List <Food>(), } } ; List <Food> foodA = new List <Food>(); int x = 0; var msgs = await Discord.AsyncEnumerableExtensions.FlattenAsync(((SocketTextChannel)Program.client.GetChannel(Program.idIndexUser[userId].channelId)).GetMessagesAsync(thisReq)); if (msgs.Count() >= thisReq && recurse && thisReq < 1500) { Thread.Sleep(2000); return(await GrabFood(userId, thisReq + 500)); } pie.value = foodA; pie.numOfMessagesChecked = msgs.Count(); foreach (var sentMsg in msgs) { try { if (sentMsg.Author.Id == userId) { int hr = sentMsg.Content.ToUpper().Contains("AM") || int.Parse(sentMsg.Content.Split(':')[0]) == 12 ? int.Parse(sentMsg.Content.Split(':')[0]) : int.Parse(sentMsg.Content.Split(':')[0]) + 12; int min = int.Parse(sentMsg.Content.Split(':')[1].Split(' ')[0]); ulong thisId = sentMsg.Id; List <string> a = sentMsg.Content.Split('\n').ToList(); a.RemoveAt(0); foreach (string befstr in a.ToList()) { Food f = new Food(); string str = ""; List <string> descs = new List <string>(); if (befstr.Contains('(')) { for (int i = 0; i < befstr.Count(); i++) { if (befstr[i] == '(') { for (int z = i; z < befstr.Count(); z++) { if (befstr[z] == ')') { string toAddH = befstr.Substring(i + 1, z - i - 1); descs.Add(toAddH); i = i + z; break; } } } else { str += befstr[i]; } } } else { str = befstr; } f.Weight = str.Contains('[') && str.Contains(']') ? double.Parse(str.ToLower().Split('[')[1].Split(']')[0].Split('x')[1]) : 1; f.FromMessage = thisId; f.Description = descs; f.Name = str.Contains('[') && str.Contains(']') ? str.Split('[')[0] : str; f.ConsumedAt = new DateTime(sentMsg.Timestamp.Year, sentMsg.Timestamp.Month, sentMsg.Timestamp.Day, hr, min, 0); foodA.Add(f); } x++; } } catch (Exception e) { if (clean) { await sentMsg.DeleteAsync(); pie.numOfMessagesDeleted++; } } } } catch (Exception e) { Console.WriteLine(e); } return(pie); }