这似乎有效,但是当我退出语音通道时,时间仍在数据库中计算。现在我将编辑问题并添加我的代码 _client.UserVoiceStateUpdated + = UserConnection;
public static async Task UserConnection(SocketUser arg, SocketVoiceState stat1, SocketVoiceState stat2)
{
var user = arg as SocketGuildUser;
var profile = UsrDb.GetUserHours(user.Id, user.Guild.Id);
if (profile == null)
{
profile = new Usr
{
UserId = user.Id,
GuildId = user.Guild.Id
};
}
profile.startTime = DateTime.UtcNow;
await UsrDb.SetUsr(profile); //recording time in the database
}
[Command("check user time")]
public async Task UserTimeCheck()
{
var profile = UsrDb.GetUserHours(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");
}
profile.endTime = DateTime.UtcNow;
TimeSpan total = profile.endTime - profile.startTime;
eb.WithAuthor($"{Context.User.Username} - Your voice time");
eb.WithDescription($"{total.Hours} hrs | {total.Minutes} min | {total.Seconds} sec.");
await ReplyAsync(embed: eb.Build());
}