public GuildMessageStats(ulong guildId) { this.ID = guildId; try { if (GenericBot.LoadedGuildMessageStats.ContainsKey(this.ID)) { this.Users = GenericBot.LoadedGuildMessageStats[this.ID].Users; } else { var col = GenericBot.GlobalDatabase.GetCollection <GuildMessageStats>("messageStats"); GuildMessageStats tempdb; col.EnsureIndex(c => c.ID, true); if (col.Exists(c => c.ID.Equals(guildId))) { tempdb = col.FindOne(c => c.ID.Equals(guildId)); } else { tempdb = new GuildMessageStats() { ID = guildId, Users = new List <StatsUser>() }; } this.Users = tempdb.Users; } } catch (Exception ex) { GenericBot.Logger.LogErrorMessage($"Load Stats for {guildId} Failed: {ex.Message}\n{ex.StackTrace}"); } }
public List <Command> GetAnalyticsCommand() { List <Command> cmds = new List <Command>(); Command analytics = new Command("analytics"); analytics.RequiredPermission = Command.PermissionLevels.GlobalAdmin; analytics.Description = "Get a ton of analytics information from the server"; analytics.ToExecute += async(client, msg, parameters) => { var stats = new GuildMessageStats(msg.GetGuild().Id).DisposeLoader(); var years = stats.Years; var months = years.SelectMany(y => y.Months); var days = months.SelectMany(m => m.Days); var users = days.SelectMany(d => d.Users); var commands = users.SelectMany(u => u.Commands); var mostActiveIdOverall = users.OrderByDescending(u => u.MessageCount).Take(3); string mostActiveUsersOverall = ""; foreach (var id in mostActiveIdOverall) { if (msg.GetGuild().Users.HasElement(u => u.Id == id.UserId, out var us)) { mostActiveUsersOverall += $" {us.GetDisplayName()} (`{us}`) " + $"(`{id.MessageCount}` messages, `{id.Commands.Sum(c => c.Value)}` commands)\n"; } else { mostActiveUsersOverall += $" Unknown User (`{mostActiveIdOverall}`) " + $"(`{id.MessageCount}` messages, `{id.Commands.Sum(c => c.Value)}` commands)\n"; } } var MostUsedCommandInfoOverall = commands.OrderByDescending(c => c.Value).Take(3); string MostUsedCommandsOverall = ""; foreach (var cmd in MostUsedCommandInfoOverall) { MostUsedCommandsOverall += $" {cmd.Key} (`{cmd.Value}` uses)\n"; } string info = $"Analytics for **{msg.GetGuild().Name}**\n\n" + $"All Messages Logged: `{users.Sum(u => u.MessageCount)}`\n" + $"All Commands Logged: `{commands.Sum(c => c.Value)}`\n" + $"Most Active Users Overall: \n{mostActiveUsersOverall}" + $"Most Used Commands Overall: \n{MostUsedCommandsOverall}"; await msg.ReplyAsync(info); }; //cmds.Add(analytics); return(cmds); }
public List <Command> GetAnalyticsCommand() { List <Command> cmds = new List <Command>(); Command analytics = new Command("analytics"); analytics.RequiredPermission = Command.PermissionLevels.GlobalAdmin; analytics.Description = "Get a ton of analytics information from the server"; analytics.ToExecute += async(client, msg, parameters) => { var stats = new GuildMessageStats(msg.GetGuild().Id); var users = stats.Users.Where(u => u.Id != 0); var years = users.SelectMany(u => u.Years); var months = years.SelectMany(y => y.Months); var days = months.SelectMany(m => m.Days); var commands = days.Where(d => d.Commands != null).SelectMany(d => d.Commands) .GroupBy(kvp => kvp.Key, kvp => kvp.Value) .Select(g => new KeyValuePair <string, int>(g.Key, g.Sum())) .ToList(); var mostActiveIdOverall = users.OrderByDescending(u => u.Years.Sum(y => y.Months.Sum(m => m.Days.Sum(d => d.MessageCount)))).Take(3); string mostActiveUsersOverall = ""; foreach (var user in mostActiveIdOverall) { var commandCount = user.Years.Any(y => y.Months.Any(m => m.Days.Any(d => d.Commands != null))) ? user.Years.Sum(y => y.Months.Sum(m => m.Days.Where(d => d.Commands != null).Sum(d => d.Commands.Sum(c => c.Value)))) : 0; if (msg.GetGuild().Users.HasElement(u => u.Id == user.Id, out var us)) { mostActiveUsersOverall += $" {us.GetDisplayName()} (`{us}`) " + $"(`{user.Years.Sum(y => y.Months.Sum(m => m.Days.Sum(d => d.MessageCount)))}` messages, " + $"`{commandCount}` commands)\n"; } else { mostActiveUsersOverall += $" Unknown User (`{user.Id}`) " + $"(`{user.Years.Sum(y => y.Months.Sum(m => m.Days.Sum(d => d.MessageCount)))}` messages, " + $"`{commandCount}` commands)\n"; } } var MostUsedCommandInfoOverall = commands.OrderByDescending(c => c.Value).Take(3); string MostUsedCommandsOverall = ""; foreach (var cmd in MostUsedCommandInfoOverall) { MostUsedCommandsOverall += $" {cmd.Key} (`{cmd.Value}` uses)\n"; } string mostActiveUsersToday = ""; var mostActiveIdToday = GetTopToday(users); foreach (var user in mostActiveIdToday) { if (msg.GetGuild().Users.HasElement(u => u.Id == user.Id, out var us)) { mostActiveUsersToday += $" {us.GetDisplayName()} (`{us}`) " + $"(`{MessageCountTodayByUser(user)}` messages)\n"; } else { mostActiveUsersToday += $" Unknown User (`{user.Id}`) " + $"(`{MessageCountTodayByUser(user)}` messages)\n"; } } string info = $"Analytics for **{msg.GetGuild().Name}**\n\n"; info += $"All Messages Logged: `{days.Sum(d => d.MessageCount)}`\n"; info += $"All Commands Logged: `{commands.Sum(c => c.Value)}`\n"; info += $"Most Active Users Overall: \n{mostActiveUsersOverall}"; info += $"Most Used Commands Overall: \n{MostUsedCommandsOverall}"; info += $"Most Active Users Today: \n{mostActiveUsersToday}"; info += $"Users active overall: `{users.Count()}`\n"; info += $"Users active this month: `{GetTotalThisMonth(users).Count()}`\n"; info += $"Users active today: `{GetTotalToday(users).Count()}`\n"; await msg.ReplyAsync(info); }; cmds.Add(analytics); return(cmds); }