public List <PublicChannelShort> CreateOrUpdateChannels(Dictionary <ulong, IReadOnlyList <DiscordChannel> > channels) { using (var ctx = new DiscordLoggerContext(_mssqlConnectionString)) { var existingChannels = ctx.Channels.Select(x => new PublicChannelShort { ChannelId = x.ChannelId, GuildId = x.GuildId }).ToList(); var existingGuilds = ctx.Guilds.ToList(); // Loop over all the guilds first foreach (var guildChannel in channels) { var newChannels = new List <Channel>(); // f**k this is stupid looking, why the f**k did I pass data in this way // Loop over all the roles in the guild foreach (var pendingChannel in guildChannel.Value) { if (existingChannels.Any(x => x.ChannelId == pendingChannel.Id && x.GuildId == guildChannel.Key) == false) { var newChannel = new Channel { GuildId = guildChannel.Key, ChannelId = pendingChannel.Id, Created = pendingChannel.CreationTimestamp.UtcDateTime, IsCategory = pendingChannel.IsCategory, IsNsfw = pendingChannel.IsNSFW, IsPrivate = pendingChannel.IsPrivate, Name = pendingChannel.Name, Position = pendingChannel.Position, Topic = pendingChannel.Topic, Type = pendingChannel.Type.ToString() }; newChannel.Guild = new Guild_Channel { Guild = existingGuilds.First(x => x.GuildId == guildChannel.Key), Channel = newChannel }; newChannels.Add(newChannel); } } ctx.Channels.AddRange(newChannels); } ctx.SaveChanges(); existingChannels.AddRange(ctx.Channels.Select(x => new PublicChannelShort { ChannelId = x.ChannelId, GuildId = x.GuildId })); return(existingChannels); } }
public List <RoleShort> CreateOrUpdateRoles(Dictionary <ulong, IReadOnlyList <DiscordRole> > roles) { using (var ctx = new DiscordLoggerContext(_mssqlConnectionString)) { var existingRoles = ctx.Roles.Select(x => new RoleShort { RoleId = x.RoleId, GuildId = x.GuildId }).ToList(); var existingGuilds = ctx.Guilds.ToList(); // Loop over all the guilds first foreach (var guildRoles in roles) { var newRoles = new List <Role>(); // f**k this is stupid looking, why the f**k did I pass data in this way // Loop over all the roles in the guild foreach (var pendingRole in guildRoles.Value) { if (existingRoles.Any(x => x.RoleId == pendingRole.Id && x.GuildId == guildRoles.Key) == false) { var newRole = new Role { GuildId = guildRoles.Key, Color = pendingRole.Color.Value, Created = pendingRole.CreationTimestamp.UtcDateTime, Name = pendingRole.Name, RoleId = pendingRole.Id }; newRole.Guild = new Guild_Role { Guild = existingGuilds.First(x => x.GuildId == guildRoles.Key), Role = newRole }; newRoles.Add(newRole); } } ctx.Roles.AddRange(newRoles); } ctx.SaveChanges(); existingRoles.AddRange(ctx.Roles.Select(x => new RoleShort { RoleId = x.RoleId, GuildId = x.GuildId })); return(existingRoles); } }
public List <ulong> CreateOrUpdateGuilds(List <DiscordGuild> guildList) { using (var ctx = new DiscordLoggerContext(_mssqlConnectionString)) { var existingGuilds = ctx.Guilds.Select(x => x.GuildId).ToList(); var a = ctx.Guilds.Include("Users").Include("Channels").Include("Roles").ToList(); foreach (var pendingGuild in guildList) { if (existingGuilds.Any(x => x == pendingGuild.Id) == false) { var newGuild = new Guild { Created = pendingGuild.CreationTimestamp.UtcDateTime, GuildId = pendingGuild.Id, IconUrl = pendingGuild.IconUrl ?? "", Name = pendingGuild.Name }; ctx.Guilds.Add(newGuild); } else { UpdateGuild(ctx.Guilds.First(x => x.GuildId == pendingGuild.Id), pendingGuild); } } try { ctx.SaveChanges(); existingGuilds.AddRange(ctx.Guilds.Select(x => x.GuildId)); return(existingGuilds); } catch (Exception e) { throw; } } }
public List <PublicDiscordUser> CreateOrUpdateUsers(Dictionary <ulong, IReadOnlyList <DiscordMember> > members) { using (var ctx = new DiscordLoggerContext(_mssqlConnectionString)) { var newUsers = new Dictionary <ulong, User>(); foreach (var guild in members.Keys) { var pendingMembers = members[guild].Select(x => x.Id).ToArray(); var existingGuildMembers = ctx.Users.Where(x => pendingMembers.Contains(x.UserId)).ToList(); var guildDetails = ctx.Guilds.Include("Roles.Role").First(x => x.GuildId == guild); // The below code is the same as the above, but is more explicit with its including //var guildDetails2 = ctx.Guilds.Include(x => x.Roles).ThenInclude(x => x.Role).First(x => x.GuildId == guild); foreach (var pendingMember in members[guild]) { // Only add new members for now if (existingGuildMembers.Any(x => x.UserId == pendingMember.Id) == false) { if (newUsers.ContainsKey(pendingMember.Id) == false) { var newMember = new User { AvatarUrl = pendingMember.AvatarUrl, Created = pendingMember.CreationTimestamp.UtcDateTime, Discriminator = int.Parse(pendingMember.Discriminator), IsBot = pendingMember.IsBot, UserId = pendingMember.Id, Username = pendingMember.Username, Guilds = new List <Guild_User>() }; // Double picking from a collection. Gross, but a dictionary lookup is quicker than // replacing it with pendingMember.Roles.First(y => y.Id == x.RoleId).Position // as .First loops over all elements, and the select is already a loop. var roles = pendingMember.Roles.Select(x => x.Id).ToArray(); var rolePositions = pendingMember.Roles.ToDictionary(x => x.Id, x => x.Position); // Roles are sorted by their position descending - some servers use this to create // pseudo groups in profiles newMember.Roles = guildDetails.Roles .Where(x => roles.Contains(x.RoleId)) .Select(x => new User_Role { Role = x.Role, User = newMember, RoleOrder = rolePositions[x.RoleId] }) .ToList(); newMember.Guilds.Add(new Guild_User { Guild = guildDetails, User = newMember, Nickname = pendingMember.Nickname, Joined = pendingMember.JoinedAt.UtcDateTime, Colour = pendingMember.Color.Value }); newUsers.Add(newMember.UserId, newMember); } else { newUsers[pendingMember.Id].Guilds.Add(new Guild_User { Guild = guildDetails, User = newUsers[pendingMember.Id], Nickname = pendingMember.Nickname, Joined = pendingMember.JoinedAt.UtcDateTime, Colour = pendingMember.Color.Value }); } } } } try { ctx.Users.AddRange(newUsers.Select(x => x.Value)); ctx.SaveChanges(); return(ctx.Users.Select(x => new PublicDiscordUser { Guilds = x.Guilds.Select(y => new PublicDiscordGuild { GuildId = y.GuildId, Name = y.Guild.Name, Nickname = y.Nickname, Joined = y.Joined, Colour = y.Colour }).ToList(), Discriminator = x.Discriminator, Id = x.UserId, Username = x.Username, Created = x.Created, Roles = x.Roles.Select(y => new PublicDiscordRole { Color = y.Role.Color, GuildId = y.Role.GuildId, Id = y.RoleId, Name = y.Role.Name, Position = y.RoleOrder }).ToList() }) .ToList()); } catch { throw; } } }