diff --git a/StateMangers/VoiceStateManager.cs b/StateMangers/VoiceStateManager.cs index abc1a0c..d772433 100644 --- a/StateMangers/VoiceStateManager.cs +++ b/StateMangers/VoiceStateManager.cs @@ -30,31 +30,27 @@ public class VoiceStateManager { // Joining channel _logger.LogInformation($"New yapper detected: {voice_state.User.Username}"); - if (!_guildVoiceStates.TryGetValue(voice_state.GuildId, out var yappers) || yappers.Count < 0) + if (!_guildVoiceStates.TryGetValue(voice_state.GuildId, out var yappers) || yappers.Count == 0) { + yappers ??= new HashSet(); var channels = await _restClient.GetGuildChannelsAsync(voice_state.GuildId); var alertChannelId = channels.FirstOrDefault(channel => channel.Name == "bot-tinkering")?.Id; 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 && (!_yapperNotificationTimeoutCache.TryGetValue(voice_state.GuildId, out DateTimeOffset timeout) || DateTimeOffset.UtcNow.CompareTo(timeout) > 0)) + if (alertChannelId is null || yapperRoleId is null) { - // Notify that new yapper has arrived - await _restClient.SendMessageAsync( - alertChannelId.GetValueOrDefault(), - new MessageProperties() - { - AllowedMentions = new AllowedMentionsProperties() {AllowedRoles = [yapperRoleId.GetValueOrDefault()]}, - Content = $"<@&{yapperRoleId.GetValueOrDefault()}> " + PhraseProvider.GetYapperPhrase() - }); + _logger.LogError($"Found incorrect state, alert channel or yapper role does not exist."); + return; } - yappers = new HashSet() { voice_state.UserId }; - } - else - { - yappers = yappers.Append(voice_state.UserId).ToHashSet(); + if (!CheckIfTimeoutExpired(voice_state.GuildId)) return; + + // Notify that new yapper has arrived + await SendYapperNotification(yapperRoleId.GetValueOrDefault(), alertChannelId.GetValueOrDefault()); } + + yappers.Add(voice_state.UserId); _guildVoiceStates[voice_state.GuildId] = yappers; } else @@ -65,11 +61,41 @@ public class VoiceStateManager { yappers.Remove(voice_state.UserId); _guildVoiceStates[voice_state.GuildId] = yappers; - var expiration = DateTimeOffset.UtcNow.AddHours(2); - _yapperNotificationTimeoutCache.Set(voice_state.GuildId, expiration, expiration); + if (yappers.Count == 0) SetGuildTimeout(voice_state.GuildId); } } return; } + + private void SetGuildTimeout(ulong guild_id) + { + #if DEBUG + var expiration = DateTimeOffset.UtcNow.AddMinutes(1); + #else + var expiration = DateTimeOffset.UtcNow.AddHours(2); + #endif + + _yapperNotificationTimeoutCache.Set(guild_id, expiration, expiration); + } + + private bool CheckIfTimeoutExpired(ulong guild_id) + { + var timeOutCached = + _yapperNotificationTimeoutCache.TryGetValue(guild_id, out DateTimeOffset timeout); + if (timeOutCached == false) return true; + if (timeout < DateTimeOffset.UtcNow) return true; + return false; + } + + private async Task SendYapperNotification(ulong yapperRoleId, ulong alertChannelId) + { + await _restClient.SendMessageAsync( + alertChannelId, + new MessageProperties() + { + AllowedMentions = new AllowedMentionsProperties() {AllowedRoles = [yapperRoleId]}, + Content = $"<@&{yapperRoleId}> " + PhraseProvider.GetYapperPhrase() + }); + } } \ No newline at end of file