fix: Fixed voice and text channel counting

This commit is contained in:
Kwoth
2024-09-05 06:04:47 +00:00
parent e85e7c49cb
commit c36ab34c4f

View File

@@ -55,76 +55,60 @@ public sealed class StatsService : IStatsService, IReadyExecutor, INService
_client.ChannelCreated += c => _client.ChannelCreated += c =>
{ {
_ = Task.Run(() => if (c is IVoiceChannel)
{ Interlocked.Increment(ref voiceChannels);
if (c is ITextChannel) else if (c is ITextChannel)
Interlocked.Increment(ref textChannels); Interlocked.Increment(ref textChannels);
else if (c is IVoiceChannel)
Interlocked.Increment(ref voiceChannels);
});
return Task.CompletedTask; return Task.CompletedTask;
}; };
_client.ChannelDestroyed += c => _client.ChannelDestroyed += c =>
{ {
_ = Task.Run(() => if (c is IVoiceChannel)
{ Interlocked.Decrement(ref voiceChannels);
if (c is ITextChannel) else if (c is ITextChannel)
Interlocked.Decrement(ref textChannels); Interlocked.Decrement(ref textChannels);
else if (c is IVoiceChannel)
Interlocked.Decrement(ref voiceChannels);
});
return Task.CompletedTask; return Task.CompletedTask;
}; };
_client.GuildAvailable += g => _client.GuildAvailable += g =>
{ {
_ = Task.Run(() => var tc = g.Channels.Count(cx => cx is ITextChannel and not IVoiceChannel);
{ var vc = g.Channels.Count(cx => cx is IVoiceChannel);
var tc = g.Channels.Count(cx => cx is ITextChannel); Interlocked.Add(ref textChannels, tc);
var vc = g.Channels.Count - tc; Interlocked.Add(ref voiceChannels, vc);
Interlocked.Add(ref textChannels, tc);
Interlocked.Add(ref voiceChannels, vc);
});
return Task.CompletedTask; return Task.CompletedTask;
}; };
_client.JoinedGuild += g => _client.JoinedGuild += g =>
{ {
_ = Task.Run(() => var tc = g.Channels.Count(cx => cx is ITextChannel and not IVoiceChannel);
{ var vc = g.Channels.Count(cx => cx is IVoiceChannel);
var tc = g.Channels.Count(cx => cx is ITextChannel); Interlocked.Add(ref textChannels, tc);
var vc = g.Channels.Count - tc; Interlocked.Add(ref voiceChannels, vc);
Interlocked.Add(ref textChannels, tc);
Interlocked.Add(ref voiceChannels, vc);
});
return Task.CompletedTask; return Task.CompletedTask;
}; };
_client.GuildUnavailable += g => _client.GuildUnavailable += g =>
{ {
_ = Task.Run(() => var tc = g.Channels.Count(cx => cx is ITextChannel and not IVoiceChannel);
{ var vc = g.Channels.Count(cx => cx is IVoiceChannel);
var tc = g.Channels.Count(cx => cx is ITextChannel); Interlocked.Add(ref textChannels, -tc);
var vc = g.Channels.Count - tc; Interlocked.Add(ref voiceChannels, -vc);
Interlocked.Add(ref textChannels, -tc);
Interlocked.Add(ref voiceChannels, -vc);
});
return Task.CompletedTask; return Task.CompletedTask;
}; };
_client.LeftGuild += g => _client.LeftGuild += g =>
{ {
_ = Task.Run(() => var tc = g.Channels.Count(cx => cx is ITextChannel and not IVoiceChannel);
{ var vc = g.Channels.Count(cx => cx is IVoiceChannel);
var tc = g.Channels.Count(cx => cx is ITextChannel); Interlocked.Add(ref textChannels, -tc);
var vc = g.Channels.Count - tc; Interlocked.Add(ref voiceChannels, -vc);
Interlocked.Add(ref textChannels, -tc);
Interlocked.Add(ref voiceChannels, -vc);
});
return Task.CompletedTask; return Task.CompletedTask;
}; };
@@ -133,7 +117,7 @@ public sealed class StatsService : IStatsService, IReadyExecutor, INService
private void InitializeChannelCount() private void InitializeChannelCount()
{ {
var guilds = _client.Guilds; var guilds = _client.Guilds;
textChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is ITextChannel)); textChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is ITextChannel and not IVoiceChannel));
voiceChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is IVoiceChannel)); voiceChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is IVoiceChannel));
} }