diff --git a/src/NadekoBot/Modules/Administration/Commands/PlayingRotateCommands.cs b/src/NadekoBot/Modules/Administration/Commands/PlayingRotateCommands.cs index c9c49f60..b2e8115f 100644 --- a/src/NadekoBot/Modules/Administration/Commands/PlayingRotateCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/PlayingRotateCommands.cs @@ -71,8 +71,8 @@ namespace NadekoBot.Modules.Administration public static Dictionary> PlayingPlaceholders { get; } = new Dictionary> { - {"%servers%", () => NadekoBot.Client.GetGuilds().Count().ToString()}, - {"%users%", () => NadekoBot.Client.GetGuilds().Select(s => s.Users.Count).Sum().ToString()}, + {"%servers%", () => NadekoBot.Client.GetGuildsCount().ToString()}, + {"%users%", () => NadekoBot.Client.GetGuilds().Sum(s => s.Users.Count).ToString()}, {"%playing%", () => { var cnt = Music.Music.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null); if (cnt != 1) return cnt.ToString(); diff --git a/src/NadekoBot/Services/Impl/StatsService.cs b/src/NadekoBot/Services/Impl/StatsService.cs index 3331edeb..35bd77e8 100644 --- a/src/NadekoBot/Services/Impl/StatsService.cs +++ b/src/NadekoBot/Services/Impl/StatsService.cs @@ -21,16 +21,14 @@ namespace NadekoBot.Services.Impl public int MessageCounter { get; private set; } = 0; public int CommandsRan { get; private set; } = 0; public string Heap => -#if !GLOBAL_NADEKO Math.Round((double)GC.GetTotalMemory(false) / 1.MiB(), 2).ToString(); -#else - "a lot :)"; -#endif public double MessagesPerSecond => MessageCounter / (double)GetUptime().TotalSeconds; - public int TextChannels => client.GetGuilds().SelectMany(g => g.Channels.Where(c => c is ITextChannel)).Count(); - public int VoiceChannels => client.GetGuilds().SelectMany(g => g.Channels.Where(c => c is IVoiceChannel)).Count(); + private uint _textChannels = 0; + public uint TextChannels => _textChannels; + private uint _voiceChannels = 0; + public uint VoiceChannels => _voiceChannels; public string OwnerIds => string.Join(", ", NadekoBot.Credentials.OwnerIds); - + Timer carbonitexTimer { get; } public StatsService(ShardedDiscordClient client, CommandHandler cmdHandler) @@ -44,17 +42,45 @@ namespace NadekoBot.Services.Impl this.client.Disconnected += _ => Reset(); + var guilds = this.client.GetGuilds(); + var _textChannels = guilds.Sum(g => g.Channels.Where(cx => cx is ITextChannel).Count()); + var _voiceChannels = guilds.Sum(g => g.Channels.Count) - _textChannels; + + this.client.ChannelCreated += (c) => + { + if (c is ITextChannel) + ++_textChannels; + else if (c is IVoiceChannel) + ++_voiceChannels; + }; + + this.client.ChannelDestroyed += (c) => + { + if (c is ITextChannel) + --_textChannels; + else if (c is IVoiceChannel) + --_voiceChannels; + }; + + this.client.JoinedGuild += (g) => + { + var tc = g.Channels.Where(cx => cx is ITextChannel).Count(); + var vc = g.Channels.Count - tc; + _textChannels += tc; + _voiceChannels += vc; + }; + this.carbonitexTimer = new Timer(async (state) => { - if (string.IsNullOrWhiteSpace(NadekoBot.Credentials.CarbonKey)) - return; + if (string.IsNullOrWhiteSpace(NadekoBot.Credentials.CarbonKey)) + return; try { using (var http = new HttpClient()) { using (var content = new FormUrlEncodedContent( new Dictionary { - { "servercount", this.client.GetGuilds().Count.ToString() }, + { "servercount", this.client.GetGuildsCount().ToString() }, { "key", NadekoBot.Credentials.CarbonKey }})) { content.Headers.Clear(); @@ -76,7 +102,7 @@ Bot Version: [{BotVersion}] Bot ID: {curUser.Id} Owner ID(s): {OwnerIds} Uptime: {GetUptimeString()} -Servers: {client.GetGuilds().Count} | TextChannels: {TextChannels} | VoiceChannels: {VoiceChannels} +Servers: {client.GetGuildsCount()} | TextChannels: {TextChannels} | VoiceChannels: {VoiceChannels} Commands Ran this session: {CommandsRan} Messages: {MessageCounter} [{MessagesPerSecond:F2}/sec] Heap: [{Heap} MB]"); } diff --git a/src/NadekoBot/ShardedDiscordClient.cs b/src/NadekoBot/ShardedDiscordClient.cs index 29630eaf..e7f2604c 100644 --- a/src/NadekoBot/ShardedDiscordClient.cs +++ b/src/NadekoBot/ShardedDiscordClient.cs @@ -18,6 +18,7 @@ namespace NadekoBot public event Action MessageReceived = delegate { }; public event Action UserLeft = delegate { }; public event Action UserUpdated = delegate { }; + public event Action GuildUserUpdated = delegate { }; public event Action, SocketMessage> MessageUpdated = delegate { }; public event Action> MessageDeleted = delegate { }; public event Action UserBanned = delegate { }; @@ -27,6 +28,10 @@ namespace NadekoBot public event Action ChannelCreated = delegate { }; public event Action ChannelDestroyed = delegate { }; public event Action ChannelUpdated = delegate { }; + + public event Action JoinedGuild = delegate { }; + public event Action LeftGuild = delegate { }; + public event Action Disconnected = delegate { }; private uint _connectedCount = 0; @@ -54,6 +59,7 @@ namespace NadekoBot }; client.UserLeft += arg1 => { UserLeft(arg1); return Task.CompletedTask; }; client.UserUpdated += (arg1, gu2) => { UserUpdated(arg1, gu2); return Task.CompletedTask; }; + client.GuildMemberUpdated += (arg1, arg2) => { GuildUserUpdated(arg1, arg2); return Task.CompletedTask; }; client.MessageUpdated += (arg1, m2) => { MessageUpdated(arg1, m2); return Task.CompletedTask; }; client.MessageDeleted += (arg1, arg2) => { MessageDeleted(arg1, arg2); return Task.CompletedTask; }; client.UserBanned += (arg1, arg2) => { UserBanned(arg1, arg2); return Task.CompletedTask; }; @@ -63,6 +69,8 @@ namespace NadekoBot client.ChannelCreated += arg => { ChannelCreated(arg); return Task.CompletedTask; }; client.ChannelDestroyed += arg => { ChannelDestroyed(arg); return Task.CompletedTask; }; client.ChannelUpdated += (arg1, arg2) => { ChannelUpdated(arg1, arg2); return Task.CompletedTask; }; + client.JoinedGuild += (arg1) => { JoinedGuild(arg1); return Task.CompletedTask; }; + client.LeftGuild += (arg1) => { LeftGuild(arg1); return Task.CompletedTask; }; _log.Info($"Shard #{i} initialized."); #if GLOBAL_NADEKO @@ -93,8 +101,8 @@ namespace NadekoBot public SocketSelfUser CurrentUser() => Clients[0].CurrentUser; - public IReadOnlyCollection GetGuilds() => - Clients.SelectMany(c => c.Guilds).ToList(); + public IEnumerable GetGuilds() => + Clients.SelectMany(c => c.Guilds); public int GetGuildsCount() => Clients.Sum(c => c.Guilds.Count);