private void UpdateCachedGuild(DiscordGuild newGuild, JArray rawMembers) { if (this._disposed) { return; } if (!this._guilds.ContainsKey(newGuild.Id)) { this._guilds[newGuild.Id] = newGuild; } var guild = this._guilds[newGuild.Id]; if (newGuild._channels != null && newGuild._channels.Count > 0) { foreach (var channel in newGuild._channels.Values) { if (guild._channels.TryGetValue(channel.Id, out _)) { continue; } foreach (var overwrite in channel._permissionOverwrites) { overwrite.Discord = this; overwrite._channel_id = channel.Id; } guild._channels[channel.Id] = channel; } } foreach (var newEmoji in newGuild._emojis.Values) { _ = guild._emojis.GetOrAdd(newEmoji.Id, _ => newEmoji); } if (rawMembers != null) { guild._members.Clear(); foreach (var xj in rawMembers) { var xtm = xj.ToObject <TransportMember>(); var xu = new DiscordUser(xtm.User) { Discord = this }; _ = this.UserCache.AddOrUpdate(xtm.User.Id, xu, (id, old) => { old.Username = xu.Username; old.Discriminator = xu.Discriminator; old.AvatarHash = xu.AvatarHash; old.PremiumType = xu.PremiumType; return(old); }); guild._members[xtm.User.Id] = new DiscordMember(xtm) { Discord = this, _guild_id = guild.Id }; } } foreach (var role in newGuild._roles.Values) { if (guild._roles.TryGetValue(role.Id, out _)) { continue; } role._guild_id = guild.Id; guild._roles[role.Id] = role; } guild.Name = newGuild.Name; guild.AfkChannelId = newGuild.AfkChannelId; guild.AfkTimeout = newGuild.AfkTimeout; guild.DefaultMessageNotifications = newGuild.DefaultMessageNotifications; guild.Features = newGuild.Features; guild.IconHash = newGuild.IconHash; guild.MfaLevel = newGuild.MfaLevel; guild.OwnerId = newGuild.OwnerId; guild.VoiceRegionId = newGuild.VoiceRegionId; guild.SplashHash = newGuild.SplashHash; guild.VerificationLevel = newGuild.VerificationLevel; guild.WidgetEnabled = newGuild.WidgetEnabled; guild.WidgetChannelId = newGuild.WidgetChannelId; guild.ExplicitContentFilter = newGuild.ExplicitContentFilter; guild.PremiumTier = newGuild.PremiumTier; guild.PremiumSubscriptionCount = newGuild.PremiumSubscriptionCount; guild.Banner = newGuild.Banner; guild.Description = newGuild.Description; guild.VanityUrlCode = newGuild.VanityUrlCode; guild.Banner = newGuild.Banner; guild.SystemChannelId = newGuild.SystemChannelId; guild.SystemChannelFlags = newGuild.SystemChannelFlags; guild.DiscoverySplashHash = newGuild.DiscoverySplashHash; guild.MaxMembers = newGuild.MaxMembers; guild.MaxPresences = newGuild.MaxPresences; guild.ApproximateMemberCount = newGuild.ApproximateMemberCount; guild.ApproximatePresenceCount = newGuild.ApproximatePresenceCount; guild.MaxVideoChannelUsers = newGuild.MaxVideoChannelUsers; guild.PreferredLocale = newGuild.PreferredLocale; guild.RulesChannelId = newGuild.RulesChannelId; guild.PublicUpdatesChannelId = newGuild.PublicUpdatesChannelId; // fields not sent for update: // - guild.Channels // - voice states // - guild.JoinedAt = new_guild.JoinedAt; // - guild.Large = new_guild.Large; // - guild.MemberCount = Math.Max(new_guild.MemberCount, guild._members.Count); // - guild.Unavailable = new_guild.Unavailable; }
/// <summary> /// Gets a shard from a guild. /// <para>This method uses the <see cref="Utilities.GetShardId(ulong, int)"/> method and will not iterate through the shard guild caches.</para> /// </summary> /// <param name="guild">The guild for the shard.</param> /// <returns>The found <see cref="DiscordClient"/> shard.</returns> public DiscordClient GetShard(DiscordGuild guild) => this.GetShard(guild.Id);