diff --git a/Program.cs b/Program.cs index cd4ff3c..32965b0 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ using System.Text.Json.Serialization; +using Microsoft.Extensions.Caching.Memory; using NetCord; using NetCord.Gateway; using NetCord.Hosting.Gateway; @@ -34,6 +35,7 @@ public class Program }); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(new MemoryCache(new MemoryCacheOptions())); builder.Services.AddDiscordGateway(options => { options.Intents = GatewayIntents.GuildMessages diff --git a/StateMangers/VoiceStateManager.cs b/StateMangers/VoiceStateManager.cs index 3f1572e..abc1a0c 100644 --- a/StateMangers/VoiceStateManager.cs +++ b/StateMangers/VoiceStateManager.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.Caching.Memory; using NetCord; using NetCord.Gateway; using NetCord.Hosting.Gateway; @@ -11,14 +12,14 @@ public class VoiceStateManager // Key: GuildId Value: List of UserId for users in voice channel // TODO: We probably want to persist this somewhere else (Redis/Valkey) private Dictionary> _guildVoiceStates; - private Dictionary _yapperNotificationTimeouts; + private IMemoryCache _yapperNotificationTimeoutCache; private RestClient _restClient; private ILogger _logger; - public VoiceStateManager(ILogger logger, RestClient restClient) + public VoiceStateManager(ILogger logger, RestClient restClient, IMemoryCache yapperNotificationTimeoutCache) { _guildVoiceStates = new Dictionary>(); - _yapperNotificationTimeouts = new Dictionary(); + _yapperNotificationTimeoutCache = yapperNotificationTimeoutCache; _restClient = restClient ?? throw new System.ArgumentNullException(nameof(restClient)); _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); } @@ -36,11 +37,11 @@ public class VoiceStateManager var guildRoles = await _restClient.GetGuildRolesAsync(voice_state.GuildId, new RestRequestProperties() { AuditLogReason = "Role lookup" }); var yapperRoleId = guildRoles.FirstOrDefault(role => role.Name.ToLower() == "yapper")?.Id; - if (alertChannelId is not null && yapperRoleId is not null && (!_yapperNotificationTimeouts.TryGetValue(voice_state.GuildId, out var timeout) || DateTimeOffset.UtcNow.CompareTo(timeout) > 0)) + if (alertChannelId is not null && yapperRoleId is not null && (!_yapperNotificationTimeoutCache.TryGetValue(voice_state.GuildId, out DateTimeOffset timeout) || DateTimeOffset.UtcNow.CompareTo(timeout) > 0)) { // Notify that new yapper has arrived await _restClient.SendMessageAsync( - alertChannelId.GetValueOrDefault(), + alertChannelId.GetValueOrDefault(), new MessageProperties() { AllowedMentions = new AllowedMentionsProperties() {AllowedRoles = [yapperRoleId.GetValueOrDefault()]}, @@ -64,7 +65,8 @@ public class VoiceStateManager { yappers.Remove(voice_state.UserId); _guildVoiceStates[voice_state.GuildId] = yappers; - _yapperNotificationTimeouts[voice_state.GuildId] = DateTimeOffset.UtcNow.AddHours(2); + var expiration = DateTimeOffset.UtcNow.AddHours(2); + _yapperNotificationTimeoutCache.Set(voice_state.GuildId, expiration, expiration); } }