/// <summary> /// Updates the last activity timestamp for the given user. /// </summary> /// <param name="userStatistics">The statistics service.</param> /// <param name="guildUser">The guild user.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private async Task UpdateLastActivityTimestampForUser ( UserStatisticsService userStatistics, IGuildUser guildUser ) { var getGlobalStats = await userStatistics.GetOrCreateUserServerStatisticsAsync(guildUser); if (!getGlobalStats.IsSuccess) { this.Log.LogError(getGlobalStats.Exception, getGlobalStats.ErrorReason); return; } var globalStats = getGlobalStats.Entity; globalStats.LastActivityTime = DateTimeOffset.UtcNow; }
private async Task <OperationResult> UpdateServerMessageCountAsync ( UserStatisticsService userStatistics, SocketGuildUser guildUser, CancellationToken ct = default ) { var getGlobalStats = await userStatistics.GetOrCreateUserServerStatisticsAsync(guildUser, ct); if (!getGlobalStats.IsSuccess) { return(OperationResult.FromError(getGlobalStats)); } var globalStats = getGlobalStats.Entity; long?totalMessageCount; if (!(globalStats.TotalMessageCount is null)) { totalMessageCount = globalStats.TotalMessageCount.Value + 1; }
/// <inheritdoc /> public async Task <Result> RespondAsync(IMessageCreate gatewayEvent, CancellationToken ct = default) { if (!gatewayEvent.GuildID.IsDefined(out var guildID)) { return(Result.FromSuccess()); } using var transaction = TransactionFactory.Create(); var user = gatewayEvent.Author.ID; var getUserStatistics = await _statistics.GetOrCreateUserServerStatisticsAsync(guildID, user, ct); if (!getUserStatistics.IsSuccess) { return(Result.FromError(getUserStatistics)); } var userStatistics = getUserStatistics.Entity; var setTotalCount = await _statistics.SetTotalMessageCountAsync ( userStatistics, (userStatistics.TotalMessageCount ?? 0) + 1, ct ); if (!setTotalCount.IsSuccess) { return(setTotalCount); } var channel = gatewayEvent.ChannelID; var getChannelStatistics = await _statistics.GetOrCreateUserChannelStatisticsAsync ( guildID, user, channel, ct ); if (!getChannelStatistics.IsSuccess) { return(Result.FromError(getChannelStatistics)); } var channelStatistics = getChannelStatistics.Entity; var setChannelCount = await _statistics.SetChannelMessageCountAsync ( channelStatistics, (channelStatistics.MessageCount ?? 0) + 1, ct ); if (!setChannelCount.IsSuccess) { return(setChannelCount); } // Finally, update the relevant autoroles var autoroles = await _autoroles.GetAutorolesAsync ( guildID, q => q .Where(a => a.IsEnabled) .Where ( a => a.Conditions.Any ( c => c.GetType() == typeof(MessageCountInGuildCondition) || c.GetType() == typeof(MessageCountInChannelCondition) ) ), ct ); foreach (var autorole in autoroles) { var updateAutorole = await _autoroleUpdates.UpdateAutoroleForUserAsync(autorole, guildID, user, ct); if (!updateAutorole.IsSuccess) { return(Result.FromError(updateAutorole)); } } transaction.Complete(); return(Result.FromSuccess()); }
private async Task <Result> UpdateTimestampAndRelevantAutorolesAsync ( Snowflake guild, Snowflake user, CancellationToken ct = default ) { { using var timestampTransaction = TransactionFactory.Create(ReadCommitted); var getServerStatistics = await _statistics.GetOrCreateUserServerStatisticsAsync(guild, user, ct); if (!getServerStatistics.IsSuccess) { return(Result.FromError(getServerStatistics)); } var serverStatistics = getServerStatistics.Entity; var updateTimestamp = await _statistics.UpdateTimestampAsync(serverStatistics, ct); if (!updateTimestamp.IsSuccess) { return(updateTimestamp); } timestampTransaction.Complete(); } var autoroles = await _autoroles.GetAutorolesAsync ( guild, q => q .Where(a => a.IsEnabled) .Where ( a => a.Conditions.Any ( c => c.GetType() == typeof(TimeSinceLastActivityCondition) || c.GetType() == typeof(TimeSinceJoinCondition) ) ), ct ); foreach (var autorole in autoroles) { using var transaction = TransactionFactory.Create(); var updateAutorole = await _autoroleUpdates.UpdateAutoroleForUserAsync(autorole, guild, user, ct); if (!updateAutorole.IsSuccess) { return(Result.FromError(updateAutorole)); } transaction.Complete(); } return(Result.FromSuccess()); }