Esempio n. 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();
        }
Esempio n. 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);
        }
Esempio n. 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());
        }
Esempio n. 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());
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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)));
            }
        }
Esempio n. 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);
        }
Esempio n. 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();
        }
Esempio n. 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();
            }
        }
Esempio n. 13
0
 public static IQueryable <UserSetting> GetAllSettings(ulong userId, DiscordContext context)
 {
     return(GetAllSettings(Convert.ToInt64(userId), context));
 }
Esempio n. 14
0
 public static Task SetMode(WheelMode newMode, ulong guildId, DiscordContext context = null, bool save = true)
 {
     return(SetMode(newMode, Convert.ToInt64(guildId), context, save));
 }
Esempio n. 15
0
 public static Task <WheelMode> GetMode(ulong guildId, DiscordContext context = null)
 {
     return(GetMode(Convert.ToInt64(guildId), context));
 }
Esempio n. 16
0
 public static async Task RemoveItemFromUser(ulong userId, WheelUserItem item, DiscordContext context = null)
 {
     await RemoveItemFromUserAsync(Convert.ToInt64(userId), item, context);
 }
Esempio n. 17
0
 public static async Task <WheelOutcome> GetLastOutcome(long userId, bool isUserReport, DiscordContext context = null)
 {
     return(await GetLastOutcome(UserSetting.Outcome.All, userId, isUserReport, context));
 }
Esempio n. 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();
        }
Esempio n. 19
0
 public static async Task AddItemToUser(ulong userId, WheelUserItem item, DiscordContext context = null)
 {
     await AddItemToUser(Convert.ToInt64(userId), item, context);
 }
Esempio n. 20
0
 public static async Task <IEnumerable <WheelUserItem> > GetUserItemsAsync(ulong userId, DiscordContext context = null)
 {
     return(await GetUserItemsAsync(Convert.ToInt64(userId), context));
 }
Esempio n. 21
0
 public static async Task <Users> GetUser(ulong userId, DiscordContext context = null)
 {
     return(await GetUser(Convert.ToInt64(userId), context));
 }
Esempio n. 22
0
 public static IQueryable <UserSetting> GetAllSettings(long userId, DiscordContext context)
 {
     return(context.UserSetting.Where(setting => setting.UserId == userId));
 }