/// <summary> /// Обновление времени в голосовых каналах /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void UpdateVoiceTimesOnElapsedAsync(object sender, ElapsedEventArgs e) { Client.Logger.LogDebug(BotLoggerEvents.Timers, $"UpdateVoiceTimesOnElapsedAsync running"); try { foreach (var entry in VoiceListener.VoiceTimeCounters) { try { var time = DateTime.Now - entry.Value; VoiceTimeSQL.AddForUser(entry.Key, time); } catch (Exception ex) { Client.Logger.LogError(BotLoggerEvents.Timers, ex, $"Ошибка при обновлении времени активности пользователя ({entry.Key})."); } } //Clear old values VoiceListener.VoiceTimeCounters.Clear(); var guild = Client.Guilds[Bot.BotSettings.Guild]; foreach (var entry in guild.VoiceStates.Where(x => x.Value.Channel != null && x.Value.Channel.Id != guild.AfkChannel.Id && x.Value.Channel.Id != Bot.BotSettings.WaitingRoom).ToList()) { if (!VoiceListener.VoiceTimeCounters.ContainsKey(entry.Key)) { VoiceListener.VoiceTimeCounters.Add(entry.Key, DateTime.Now); } } } catch (Exception ex) { Client.Logger.LogError(BotLoggerEvents.Timers, ex, $"Ошибка при обновлении времени активности пользователей."); } }
public static TimeSpan GetUpdatedVoiceTime(ulong userId) { //Force update voiceTime if (VoiceTimeCounters.ContainsKey(userId)) { var time = DateTime.Now - VoiceTimeCounters[userId]; VoiceTimeSQL.AddForUser(userId, time); VoiceTimeCounters[userId] = DateTime.Now; } return(VoiceTimeSQL.GetForUser(userId)); }
public static async Task UpdateVoiceTimeOnVoiceStateUpdated(DiscordClient client, VoiceStateUpdateEventArgs e) { // User changed voice channel if (e.Before != null && e.Before.Channel != null && e.After != null && e.After.Channel != null && e.Before.Channel.Id != e.After.Channel.Id) { if (e.After.Channel.Id == e.Guild.AfkChannel.Id || e.After.Channel.Id == Bot.BotSettings.WaitingRoom || e.After.Channel.Users.Count() < 2) { if (VoiceTimeCounters.ContainsKey(e.User.Id)) { var time = DateTime.Now - VoiceTimeCounters[e.User.Id]; VoiceTimeSQL.AddForUser(e.User.Id, time); VoiceTimeCounters.Remove(e.User.Id); } } else if (e.Before.Channel.Id == e.Guild.AfkChannel.Id || e.Before.Channel.Users.Count() < 2) { VoiceTimeCounters[e.User.Id] = DateTime.Now; } } //User left from voice else if (e.Before != null && e.Before.Channel != null && e.Before.Channel.Id != e.Guild.AfkChannel.Id && e.Before.Channel.Id != Bot.BotSettings.WaitingRoom && e.Before.Channel.Users.Count() > 1) { if (VoiceTimeCounters.ContainsKey(e.User.Id)) { var time = DateTime.Now - VoiceTimeCounters[e.User.Id]; VoiceTimeSQL.AddForUser(e.User.Id, time); VoiceTimeCounters.Remove(e.User.Id); } } //User joined to server voice else if (e.After != null && e.After.Channel != null && e.After.Channel.Id != e.Guild.AfkChannel.Id && e.After.Channel.Id != Bot.BotSettings.WaitingRoom && e.After.Channel.Users.Count() > 1) { VoiceTimeCounters[e.User.Id] = DateTime.Now; } await Task.CompletedTask; }