예제 #1
0
        public static async Task AddItemToUser(long userId, WheelUserItem item, DiscordContext context = null)
        {
            bool dispose = context == null;

            if (context == null)
            {
#pragma warning disable IDE0068 // Use recommended dispose pattern
                context = new DiscordContext();
#pragma warning restore IDE0068 // Use recommended dispose pattern
            }

            if (await context.WheelUserItem.AnyAsync(cItem => cItem.UserId == userId && cItem.ItemId == item.Id))
            {
                return;
            }

            var userItem = new WheelUserItem()
            {
                UserId = userId,
                ItemId = item.Id
            };

            context.WheelUserItem.Add(userItem);

            if (dispose)
            {
                context.Dispose();
            }

            await context.SaveChangesAsync();
        }
예제 #2
0
        public static async Task <WheelOutcome> GetLastOutcome(UserSetting.Outcome type, long userId, bool?isUserReport = null, DiscordContext context = null)
        {
            var dispose = context == null;

            if (context == null)
            {
#pragma warning disable IDE0068 // Use recommended dispose pattern
                context = new DiscordContext();
#pragma warning restore IDE0068 // Use recommended dispose pattern
            }

            WheelOutcome outcome;

            if (isUserReport != null)
            {
                outcome = await context.WheelOutcome.OrderByDescending(outcome => outcome.Time)
                          .FirstOrDefaultAsync(outcome => outcome.UserId == userId &&
                                               outcome.IsUserReport == (isUserReport.Value == false ? 0 : 1) &&
                                               type.HasFlag((UserSetting.Outcome)outcome.Type));
            }
            else
            {
                outcome = await context.WheelOutcome.OrderByDescending(outcome => outcome.Time)
                          .FirstOrDefaultAsync(outcome => outcome.UserId == userId &&
                                               type.HasFlag((UserSetting.Outcome)outcome.Type));
            }

            if (dispose)
            {
                context.Dispose();
            }

            return(outcome);
        }
예제 #3
0
        public static async Task <IEnumerable <WheelOutcome> > GetLastOutcomesUntil(WheelOutcome lastOutcome, DiscordContext context = null)
        {
            if (context == null)
            {
                context = new DiscordContext();
            }

            return(await context.WheelOutcome.Where(outcome => outcome.UserId == lastOutcome.UserId && outcome.Id > lastOutcome.Id).ToListAsync());
        }
예제 #4
0
        public static async Task <IEnumerable <WheelUserItem> > GetUserItemsAsync(long userId, DiscordContext context = null)
        {
            if (context == null)
            {
                context = new DiscordContext();
            }

            var items = context.WheelUserItem.Where(item => item.UserId == userId);

            return(await items.ToListAsync());
        }
예제 #5
0
        public static async Task <Users> GetUser(long userId, DiscordContext context = null)
        {
            if (context == null)
            {
#pragma warning disable IDE0068 // Use recommended dispose pattern
                context = new DiscordContext();
#pragma warning restore IDE0068 // Use recommended dispose pattern
            }

            var user = await context.Users.FirstOrDefaultAsync(u => u.UserId == userId);

            if (user == null)
            {
                lock (__lockObj)
                {
                    lock (context)
                    {
                        if (!context.Users.Any(u => u.UserId == userId))
                        {
                            user = new Users()
                            {
                                DenialTime  = DateTime.Now,
                                BanTime     = DateTime.Now,
                                LockTime    = DateTime.Now,
                                MuteTime    = DateTime.Now,
                                SpecialTime = DateTime.Now,
                                UserId      = userId
                            };

                            context.Users.Add(user);
                            context.SaveChangesAsync().GetAwaiter().GetResult();
                        }
                        else
                        {
                            user = context.Users.FindAsync(userId).GetAwaiter().GetResult();
                        }
                    }
                }
            }

            return(user);
        }
예제 #6
0
        public static async Task <IEnumerable <WheelOutcome> > GetLastOutcomesUntil(WheelOutcome lastOutcome, DiscordContext context = null)
        {
            var dispose = context == null;

            if (context == null)
            {
#pragma warning disable IDE0068 // Use recommended dispose pattern
                context = new DiscordContext();
#pragma warning restore IDE0068 // Use recommended dispose pattern
            }

            var outcomes = await context.WheelOutcome.Where(outcome => outcome.UserId == lastOutcome.UserId && outcome.Id > lastOutcome.Id).ToListAsync();

            if (dispose)
            {
                context.Dispose();
            }

            return(outcomes);
        }
예제 #7
0
        public static async Task <WheelMode> GetMode(long guildId, DiscordContext context = null)
        {
            var dispose = context == null;

            if (context == null)
            {
#pragma warning disable IDE0068 // Use recommended dispose pattern
                context = new DiscordContext();
#pragma warning restore IDE0068 // Use recommended dispose pattern
            }

            WheelSetting wheelSetting = await context.WheelSetting.FindAsync(guildId);

            if (dispose)
            {
                context.Dispose();
            }

            return(wheelSetting == null ? WheelMode.Default : (WheelMode)wheelSetting.Mode);
        }
예제 #8
0
        public static async Task <IEnumerable <WheelUserItem> > GetUserItemsAsync(long userId, DiscordContext context = null)
        {
            var dispose = context == null;

            if (context == null)
            {
#pragma warning disable IDE0068 // Use recommended dispose pattern
                context = new DiscordContext();
#pragma warning restore IDE0068 // Use recommended dispose pattern
            }

            var items = await context.WheelUserItem.Where(item => item.UserId == userId).ToListAsync();

            if (dispose)
            {
                context.Dispose();
            }

            return(items);
        }
예제 #9
0
        public static async Task <WheelOutcome> GetLastOutcome(WheelExtension.Outcome type, long userId, bool?isUserReport = null, DiscordContext context = null)
        {
            if (context == null)
            {
                context = new DiscordContext();
            }

            if (isUserReport != null)
            {
                return(context.WheelOutcome.OrderByDescending(outcome => outcome.Time)
                       .First(outcome => outcome.UserId == userId &&
                              outcome.IsUserReport == (isUserReport.Value == false ? 0 : 1) &&
                              type.HasFlag((WheelExtension.Outcome)outcome.Type)));
            }
            else
            {
                return(await context.WheelOutcome.OrderByDescending(outcome => outcome.Time)
                       .FirstOrDefaultAsync(outcome => outcome.UserId == userId &&
                                            type.HasFlag((WheelExtension.Outcome)outcome.Type)));
            }
        }
예제 #10
0
        public static async Task <Users> GetUser(long userId, DiscordContext context = null)
        {
            if (context == null)
            {
                context = new DiscordContext();
            }

            var user = await context.Users.FirstOrDefaultAsync(u => u.UserId == userId);

            if (user == null)
            {
                lock (__lockObj)
                {
                    lock (context)
                    {
                        if (!context.Users.Any(u => u.UserId == userId))
                        {
                            user = new Users()
                            {
                                DenialTime  = DateTime.Now,
                                BanTime     = DateTime.Now,
                                LockTime    = DateTime.Now,
                                MuteTime    = DateTime.Now,
                                SpecialTime = DateTime.Now,
                                UserId      = userId
                            };

                            context.Users.Add(user);
                            context.SaveChangesAsync().GetAwaiter().GetResult();
                        }
                        else
                        {
                            user = context.Users.FindAsync(userId).GetAwaiter().GetResult();
                        }
                    }
                }
            }

            return(user);
        }
예제 #11
0
        public static async Task AddItemToUser(long userId, WheelUserItem item, DiscordContext context = null)
        {
            if (context == null)
            {
                context = new DiscordContext();
            }

            if (await context.WheelUserItem.AnyAsync(cItem => cItem.UserId == userId && cItem.ItemId == item.Id))
            {
                return;
            }

            var userItem = new WheelUserItem()
            {
                UserId = userId,
                ItemId = item.Id
            };

            context.WheelUserItem.Add(userItem);

            await context.SaveChangesAsync();
        }
예제 #12
0
        public static async Task SetMode(WheelMode newMode, long guildId, DiscordContext context = null, bool save = true)
        {
            var dispose = context == null;

            if (context == null)
            {
#pragma warning disable IDE0068 // Use recommended dispose pattern
                context = new DiscordContext();
#pragma warning restore IDE0068 // Use recommended dispose pattern
            }

            WheelSetting wheelSetting = await context.WheelSetting.FindAsync(guildId);

            if (wheelSetting == null)
            {
                wheelSetting = new WheelSetting()
                {
                    GuildId = guildId,
                    Mode    = (int)newMode
                };

                context.WheelSetting.Add(wheelSetting);
            }
            else
            {
                wheelSetting.Mode = (int)newMode;
            }

            if (save)
            {
                await context.SaveChangesAsync();
            }

            if (dispose)
            {
                context.Dispose();
            }
        }
예제 #13
0
 public static IQueryable <UserSetting> GetAllSettings(ulong userId, DiscordContext context)
 {
     return(GetAllSettings(Convert.ToInt64(userId), context));
 }
예제 #14
0
 public static Task SetMode(WheelMode newMode, ulong guildId, DiscordContext context = null, bool save = true)
 {
     return(SetMode(newMode, Convert.ToInt64(guildId), context, save));
 }
예제 #15
0
 public static Task <WheelMode> GetMode(ulong guildId, DiscordContext context = null)
 {
     return(GetMode(Convert.ToInt64(guildId), context));
 }
예제 #16
0
 public static async Task RemoveItemFromUser(ulong userId, WheelUserItem item, DiscordContext context = null)
 {
     await RemoveItemFromUserAsync(Convert.ToInt64(userId), item, context);
 }
예제 #17
0
 public static async Task <WheelOutcome> GetLastOutcome(long userId, bool isUserReport, DiscordContext context = null)
 {
     return(await GetLastOutcome(UserSetting.Outcome.All, userId, isUserReport, context));
 }
예제 #18
0
        public static async Task RemoveItemFromUserAsync(long userId, WheelUserItem item, DiscordContext context = null)
        {
            if (!await context.WheelUserItem.AnyAsync(cItem => cItem.UserId == userId && cItem.ItemId == item.Id))
            {
                return;
            }

            context.WheelUserItem.Remove(await context.WheelUserItem.SingleAsync(uItem => uItem.ItemId == item.Id && uItem.UserId == userId));

            await context.SaveChangesAsync();
        }
예제 #19
0
 public static async Task AddItemToUser(ulong userId, WheelUserItem item, DiscordContext context = null)
 {
     await AddItemToUser(Convert.ToInt64(userId), item, context);
 }
예제 #20
0
 public static async Task <IEnumerable <WheelUserItem> > GetUserItemsAsync(ulong userId, DiscordContext context = null)
 {
     return(await GetUserItemsAsync(Convert.ToInt64(userId), context));
 }
예제 #21
0
 public static async Task <Users> GetUser(ulong userId, DiscordContext context = null)
 {
     return(await GetUser(Convert.ToInt64(userId), context));
 }
예제 #22
0
 public static IQueryable <UserSetting> GetAllSettings(long userId, DiscordContext context)
 {
     return(context.UserSetting.Where(setting => setting.UserId == userId));
 }