Counting the total time the user spent in the discord voice channel

I think the code will not be clear what I wrote because the function itself is large, I will try to briefly tell you. In general, the method is tied to the event that is triggered when the user entered/exited the voice channel, so when the user entered the voice for the first time, the bot writes to the startTime column in the datetime database, similarly when the user first exited the voice. When the user logs in 2 times vois will take his past time record and write it to the column OldStartTime and a new time record will start similarly with the exit. So that's what my problem is the time is read 1 time, that is, when I next go to vois, the time will be different. But I think my question is still not clear, so I will tell you in another way how do I make a time account of a user who is in vois without losing past progress.

  private async Task CountUserVoiceTime(SocketUser arg, SocketVoiceState stat1, SocketVoiceState stat2)
        {
            var user = arg as SocketGuildUser;

            var UserVoiceTime = VoiceDb.GetUserVoiceTime(user.Id, user.Guild.Id);

            if (UserVoiceTime == null)
            {
                UserVoiceTime = new Voice
                {
                    UserId = user.Id,
                    GuildId = user.Guild.Id
                }; 
            }

            DateTime dateTime = new DateTime();

            ISocketMessageChannel socketMessageChannel = user.Guild.GetTextChannel(myidchannel);

            if(stat1.VoiceChannel?.Name != null && stat2.VoiceChannel?.Name == null)
            {
                if (UserVoiceTime.endTime == null)
                {
                    UserVoiceTime.endTime = DateTime.Now;
                    await VoiceDb.StopTimeUser(UserVoiceTime);
                    await socketMessageChannel.SendMessageAsync("Юзер первый раз покинул войс");
                }
                else
                {
                    var result = VoiceDb.OldGetUserEndTime(user.Id, user.Guild.Id);
                    result.OldendTime = result.endTime;

                    await VoiceDb.OldStopTimeUser(result);
                    UserVoiceTime.endTime = DateTime.Now;
                    await VoiceDb.StopTimeUser(UserVoiceTime);

                    await socketMessageChannel.SendMessageAsync("Юзер покинул войс");
                } 
            }
            else
            {
                if (UserVoiceTime.startTime == dateTime)
                {
                    UserVoiceTime.startTime = DateTime.Now;
                    await VoiceDb.StartTimeUser(UserVoiceTime);
                    await socketMessageChannel.SendMessageAsync("Юзер первый раз зашёл в войс");
                }
                else
                {
                    var result = VoiceDb.OldGetUserStartTime(user.Id, user.Guild.Id);
                    result.OldstartTime = result.startTime;
                    UserVoiceTime.startTime = DateTime.Now;

                    await VoiceDb.OldStartTimeUser(result);
                    await VoiceDb.StartTimeUser(UserVoiceTime);
                    await socketMessageChannel.SendMessageAsync("Юзер зашёл в войс");
                } 
            }

            await Task.CompletedTask;
        }

   //запись времени в БД
   public static async Task StartTimeUser(Voice user)
        {
            using var DbContext = new SqlDbContext();
            DbContext.Update(user);
            await DbContext.SaveChangesAsync();
        }
  //команда выводящая время пользователя
[Command("usertime")]
        public async Task UserTimeCheck()
        {
            var profile = VoiceDb.GetUserVoiceTime(Context.User.Id, Context.Guild.Id);
            var eb = new EmbedBuilder();

            if (profile == null)
            {
                await ReplyAsync($"{Context.User.Username} your time 0 hours 0 minutes 0 second");

                TimeSpan total1 = profile.endTime - profile.startTime;

                TimeSpan total2 = profile.OldendTime - profile.OldstartTime;


                TimeSpan total = total1 - total2;

                eb.WithAuthor($"{Context.User.Username} - Your voice time");
                eb.WithDescription($"{total.Hours} hrs | {total.Minutes} min | {total.Seconds} sec.");
                await ReplyAsync(embed: eb.Build());
            }
        }
Author: white tears, 2020-12-12