Ejemplo n.º 1
0
        // Quick fix for user entity manipulation outside of this db context
        // TODO: Fix properly
        static public async Task <User> GetUserEntityAsync(this BotDatabaseContext context, IUser user)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            // Update cached displayname
            var displayName = user.Username;

            var result = await Patiently.HandleDbConcurrency(async() => {
                var userEntity = await context.Users.AsQueryable().FirstOrDefaultAsync(o => o.Id == user.Id);

                if (userEntity != null && userEntity.Name != displayName)
                {
                    userEntity.Name = displayName;
                    await context.SaveChangesAsync();
                }

                return(userEntity);
            });

            return(result);
        }
Ejemplo n.º 2
0
        static private async Task <IUserMessage> LocateVoteReply(BotDatabaseContext dbContext, ITextChannel channel, VoteReplyRecord voteReplyRecord)
        {
            var voteReply = await channel.GetMessageAsync(voteReplyRecord.ReplyId) as IUserMessage;

            if (voteReply is null)
            {
                dbContext.VoteReplyRecords.Remove(voteReplyRecord);
                await dbContext.SaveChangesAsync();
            }

            return(voteReply);
        }
Ejemplo n.º 3
0
        static public async Task UpdateCommandConsentAsync(this BotDatabaseContext context, IUser user, Func <CommandConsent, CommandConsent> consentUpdaterFunc)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (user is null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            await Patiently.HandleDbConcurrency(async() =>
            {
                var userEntity             = await GetOrCreateUserEntityAsync(context, user);
                userEntity.CommandConsents = consentUpdaterFunc(userEntity.CommandConsents);
                await context.SaveChangesAsync();
            });
        }
Ejemplo n.º 4
0
        static public async Task <DailyUserActivity> GetUserActivityAsync(this BotDatabaseContext context, IUser user, DateTime day)
        {
            day = day.Date;

            var todayActivity = await context.DailyUsersActivities.AsQueryable()
                                .FirstOrDefaultAsync(a => a.UserId == user.Id && a.Day == day);

            if (todayActivity == null)
            {
                todayActivity = new DailyUserActivity()
                {
                    User = await context.GetOrCreateUserEntityAsync(user)
                };

                await context.DailyUsersActivities.AddAsync(todayActivity);
            }

            return(todayActivity);
        }
Ejemplo n.º 5
0
        // Quick fix for user entity manipulation outside of this db context
        // TODO: Fix properly
        static public async Task <User> GetOrCreateUserEntityAsync(this BotDatabaseContext context, IUser user)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var userEntity = await context.GetUserEntityAsync(user);

            if (userEntity != null)
            {
                return(userEntity);
            }

            userEntity = new User()
            {
                Id   = user.Id,
                Name = user.Username
            };

            // Do not store bot data
            if (user.IsBot)
            {
                return(userEntity);
            }

            await context.AddAsync(userEntity);

            await context.SaveChangesAsync();

            return(userEntity);
        }
Ejemplo n.º 6
0
 static public Task <DailyUserActivity> GetTodayUserActivityAsync(this BotDatabaseContext context, IUser user)
 => GetUserActivityAsync(context, user, DateTime.Now);