private bool CanIncrementPoints(SocketGuild guild, SocketGuildUser user) { var key = GenerateKey(guild, user); lock (locker) { if (!LastPointsCalculatedAt.ContainsKey(key)) { return(true); } var lastMessageAt = LastPointsCalculatedAt[key]; return((DateTime.Now - lastMessageAt).TotalMinutes > 1.0); } }
public void IncrementMessage(SocketGuildUser guildUser, SocketGuild guild, SocketGuildChannel channel) { lock (locker) { using var scope = Services.CreateScope(); using var repository = scope.ServiceProvider.GetService <UsersRepository>(); var user = repository.GetOrCreateUser(guild.Id, guildUser.Id, true, false, false, false); var channelEntity = user.Channels.FirstOrDefault(o => o.ChannelIDSnowflake == channel.Id); if (channelEntity == null) { channelEntity = new DBUserChannel() { ChannelIDSnowflake = channel.Id, Count = 1, LastMessageAt = DateTime.Now, DiscordUserIDSnowflake = guildUser.Id, GuildIDSnowflake = guild.Id }; user.Channels.Add(channelEntity); } else { channelEntity.Count++; channelEntity.LastMessageAt = DateTime.Now; } if (CanIncrementPoints(guild, guildUser)) { user.Points += Random.Next(15, 25); var key = GenerateKey(guild, guildUser); if (LastPointsCalculatedAt.ContainsKey(key)) { LastPointsCalculatedAt[key] = DateTime.Now; } else { LastPointsCalculatedAt.Add(key, DateTime.Now); } } repository.SaveChanges(); } }