Пример #1
0
        public static User GetOrCreateUser(ulong Id, SoraContext soraContext)
        {
            User result = new User();

            try
            {
                result = soraContext.Users.FirstOrDefault(x => x.UserId == Id);
                if (result == null)
                {
                    //User Not found => CREATE
                    var addedUser = soraContext.Users.Add(new User()
                    {
                        UserId       = Id, ShareCentrals = new List <ShareCentral>(),
                        Interactions = new Interactions(), Votings = new List <Voting>(),
                        Reminders    = new List <Reminders>(), Marriages = new List <Marriage>(),
                        UserWaifus   = new List <UserWaifu>(),
                        HasBg        = false,
                        Notified     = false,
                        ClanStaff    = false,
                        ClanName     = "",
                        Money        = 0,
                        NextDaily    = DateTime.UtcNow
                    });
                    //Set Default action to be false! // Interactions = new Interactions() , Afk = new Afk()
                    //addedUser.Entity.Afk.IsAfk = false; CHANGED
                    //soraContext.SaveChangesThreadSafe();
                    soraContext.SaveChanges();
                    return(addedUser.Entity);
                }
                //NECESSARY SHIT SINCE DB EXTENS PERIODICALLY ;(
                var inter     = soraContext.Interactions.FirstOrDefault(x => x.UserForeignId == Id) ?? new Interactions();
                var afk       = soraContext.Afk.FirstOrDefault(x => x.UserForeignId == Id);
                var marriages = soraContext.Marriages.Where(x => x.UserForeignId == Id)?.ToList() ?? new List <Marriage>();
                var reminders = soraContext.Reminders.Where(x => x.UserForeignId == Id)?.ToList() ?? new List <Reminders>();
                var waifus    = soraContext.UserWaifus.Where(x => x.UserForeignId == Id)?.ToList() ?? new List <UserWaifu>();

                var shareCentral = soraContext.ShareCentrals.Where(x => x.CreatorId == Id)?.ToList() ?? new List <ShareCentral>();

                var votings = soraContext.Votings.Where(x => x.VoterId == Id)?.ToList() ?? new List <Voting>();
                if (result.ClanName == null)
                {
                    result.ClanName = "";
                }

                result.Votings       = votings;
                result.ShareCentrals = shareCentral;
                result.Interactions  = inter;
                result.Afk           = afk;
                result.Marriages     = marriages;
                result.Reminders     = reminders;
                result.UserWaifus    = waifus;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            //soraContext.SaveChangesThreadSafe();
            soraContext.SaveChanges();
            return(result);
        }
Пример #2
0
        public static Guild GetOrCreateGuild(ulong guildId, SoraContext soraContext)
        {
            Guild result = new Guild();

            try
            {
                result = soraContext.Guilds.FirstOrDefault(x => x.GuildId == guildId);
                if (result == null)
                {
                    //Guild not found => Create
                    var addGuild = soraContext.Guilds.Add(new Guild()
                    {
                        GuildId = guildId, Prefix = "$", Tags = new List <Tags>(), Cases = new List <ModCase>(), SelfAssignableRoles = new List <Role>(), IsDjRestricted = false, StarMessages = new List <StarMessage>(), StarMinimum = 1, Users = new List <GuildUser>(), LevelRoles = new List <GuildLevelRole>()
                    });
                    //soraContext.SaveChangesThreadSafe();
                    soraContext.SaveChanges();
                    return(addGuild.Entity);
                }

                //NECESSARY SHIT SINCE DB EXTENS PERIODICALLY ;(
                var foundTags  = soraContext.Tags.Where(x => x.GuildForeignId == guildId)?.ToList() ?? new List <Tags>();
                var foundStars = soraContext.StarMessages.Where(x => x.GuildForeignId == guildId)?.ToList();
                if (foundStars == null)
                {
                    foundStars         = new List <StarMessage>();
                    result.StarMinimum = 1;
                }

                var foundRoles = soraContext.SelfAssignableRoles.Where(x => x.GuildForeignId == guildId)?.ToList() ?? new List <Role>();

                var modCases = soraContext.Cases.Where(x => x.GuildForeignId == guildId)?.ToList() ??
                               new List <ModCase>();

                var users = soraContext.GuildUsers.Where(x => x.GuildId == guildId).ToList() ?? new List <GuildUser>();

                var levelRoles = soraContext.GuildLevelRoles.Where(x => x.GuildId == guildId).ToList() ??
                                 new List <GuildLevelRole>();

                result.LevelRoles          = levelRoles;
                result.Users               = users;
                result.Cases               = modCases;
                result.SelfAssignableRoles = foundRoles;
                result.Tags         = foundTags;
                result.StarMessages = foundStars;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            //guild found
            //soraContext.SaveChangesThreadSafe();
            soraContext.SaveChanges();
            return(result);
        }
Пример #3
0
        public static GuildUser GetOrCreateGuildUser(ulong userId, ulong guildId, SoraContext soraContext)
        {
            GuildUser gUser = null;

            try
            {
                var guildDb = GetOrCreateGuild(guildId, soraContext);
                gUser = guildDb.Users.FirstOrDefault(x => x.UserId == userId);
                if (gUser == null)
                {
                    gUser = new GuildUser()
                    {
                        Exp = 0, Guild = guildDb, GuildId = guildId, UserId = userId
                    };
                    guildDb.Users.Add(gUser);
                    soraContext.GuildUsers.Add(gUser);
                    soraContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(gUser);
        }
Пример #4
0
 public bool ChangePrefix([FromBody] WebPrefix prefix)
 {
     try
     {
         var permGuilds = GetPermGuilds(ulong.Parse(prefix.UserId));
         if (permGuilds == null || permGuilds.Count == 0)
         {
             Console.WriteLine("FOUND NO PERM GUILDS");
             return(false);
         }
         var guildId = ulong.Parse(prefix.GuildId);
         var guild   = permGuilds.FirstOrDefault(x => x.Id == guildId);
         if (guild == null)
         {
             Console.WriteLine("FOUND NO GUILD WITH ID");
             return(false);
         }
         using (var soraContext = new SoraContext())
         {
             var guildDb = Utility.GetOrCreateGuild(guildId, soraContext);
             guildDb.Prefix = prefix.Prefix;
             soraContext.SaveChanges();
             return(true);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
     return(false);
 }
Пример #5
0
        public bool EditAnnouncement([FromBody] EditAnn announc)
        {
            try
            {
                var permGuilds = GetPermGuilds(ulong.Parse(announc.UserId));
                if (permGuilds == null || permGuilds.Count == 0)
                {
                    Console.WriteLine("FOUND NO PERM GUILDS");
                    return(false);
                }
                var guildId = ulong.Parse(announc.GuildId);
                var guild   = permGuilds.FirstOrDefault(x => x.Id == guildId);
                if (guild == null)
                {
                    Console.WriteLine("FOUND NO GUILD WITH ID");
                    return(false);
                }
                var channel = guild.GetTextChannel(ulong.Parse(announc.ChannelId));
                if (channel == null)
                {
                    Console.WriteLine("FOUND NO CHANNEL WITH ID");
                    return(false);
                }
                using (var soraContext = new SoraContext())
                {
                    var guildDb = Utility.GetOrCreateGuild(guildId, soraContext);
                    switch (announc.Type)
                    {
                    case "w":
                        guildDb.WelcomeChannelId = announc.Disabled ? 0 : channel.Id;
                        guildDb.EmbedWelcome     = announc.Embed;
                        guildDb.WelcomeMessage   = announc.Message;

                        break;

                    case "l":
                        guildDb.EmbedLeave     = announc.Embed;
                        guildDb.LeaveChannelId = announc.Disabled ? 0 : channel.Id;
                        guildDb.LeaveMessage   = announc.Message;
                        break;

                    default:
                        return(false);
                    }
                    soraContext.SaveChanges();
                    return(true);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(false);
        }
Пример #6
0
 public bool EditStarboard([FromBody] WebStarboardEdit star)
 {
     try
     {
         var permGuilds = GetPermGuilds(ulong.Parse(star.UserId));
         if (permGuilds == null || permGuilds.Count == 0)
         {
             Console.WriteLine("FOUND NO PERM GUILDS");
             return(false);
         }
         var guildId = ulong.Parse(star.GuildId);
         var guild   = permGuilds.FirstOrDefault(x => x.Id == guildId);
         if (guild == null)
         {
             Console.WriteLine("FOUND NO GUILD WITH ID");
             return(false);
         }
         var channel = guild.GetTextChannel(ulong.Parse(star.ChannelId));
         if (channel == null)
         {
             Console.WriteLine("FOUND NO CHANNEL WITH ID");
             return(false);
         }
         using (var soraContext = new SoraContext())
         {
             var guildDb = Utility.GetOrCreateGuild(guildId, soraContext);
             //change
             guildDb.StarChannelId = star.Disabled ? 0 : channel.Id;
             guildDb.StarMinimum   = star.StarMin;
             soraContext.SaveChanges();
             return(true);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
     return(false);
 }
Пример #7
0
        public bool EditGuildLevels([FromBody] GuildLevelEdit lvlEdit)
        {
            try
            {
                var permGuilds = GetPermGuilds(ulong.Parse(lvlEdit.UserId));
                if (permGuilds == null || permGuilds.Count == 0)
                {
                    Console.WriteLine("FOUND NO PERM GUILDS");
                    return(false);
                }
                var guildId = ulong.Parse(lvlEdit.GuildId);
                var guild   = permGuilds.FirstOrDefault(x => x.Id == guildId);
                if (guild == null)
                {
                    Console.WriteLine("FOUND NO GUILD WITH ID");
                    return(false);
                }
                using (var soraContext = new SoraContext())
                {
                    var guildDb = Utility.GetOrCreateGuild(guildId, soraContext);
                    var banned  = guildDb.LevelRoles.Where(x => x.Banned).ToList();
                    //clear the banned list
                    foreach (var role in banned)
                    {
                        if (!lvlEdit.BannedRoles.Contains(role.RoleId.ToString()))
                        {
                            guildDb.LevelRoles.Remove(role);
                        }
                    }

                    foreach (var bannedRole in lvlEdit.BannedRoles)
                    {
                        var id = ulong.Parse(bannedRole);
                        //check if already in the list
                        if (banned.Any(x => x.RoleId == id))
                        {
                            continue;
                        }
                        //otherwise add
                        //check if role exists!
                        var role = guild.GetRole(id);
                        if (role == null)
                        {
                            continue;
                        }

                        guildDb.LevelRoles.Add(new GuildLevelRole()
                        {
                            Banned        = true,
                            GuildId       = guildId,
                            RequiredLevel = 0,
                            RoleId        = id
                        });
                    }
                    //check for all the other roles now
                    foreach (var lvlRole in lvlEdit.Roles)
                    {
                        //check if role exists
                        var id   = ulong.Parse(lvlRole.RoleId);
                        var role = guild.GetRole(id);
                        var fR   = guildDb.LevelRoles.FirstOrDefault(x => x.RoleId == id);
                        if (role == null)
                        {
                            //role didnt exist BUT it was in the rewards list
                            if (fR != null)
                            {
                                guildDb.LevelRoles.Remove(fR);
                            }
                            continue;
                        }
                        //otherwise update or create new entry for new role!
                        //if required level is 0 then remove it
                        if (fR != null)
                        {
                            if (lvlRole.LvlReq == 0)
                            {
                                if (!fR.Banned)
                                {
                                    guildDb.LevelRoles.Remove(fR);
                                }
                            }
                            else
                            {
                                fR.RequiredLevel = lvlRole.LvlReq;
                            }
                        } //otherwise add
                        else
                        {
                            if (lvlRole.LvlReq == 0)
                            {
                                continue;
                            }
                            guildDb.LevelRoles.Add(new GuildLevelRole()
                            {
                                Banned        = false,
                                GuildId       = guild.Id,
                                RequiredLevel = lvlRole.LvlReq,
                                RoleId        = role.Id
                            });
                        }
                    }
                    guildDb.EnabledLvlUpMessage = lvlEdit.EnableAnn;
                    guildDb.SendLvlDm           = lvlEdit.SendDm;
                    guildDb.LevelUpMessage      = lvlEdit.LvlUpMsg;
                    soraContext.SaveChanges();
                    return(true);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(false);
        }