internal DiscordGuild(DiscordHttpClient http, MutableGuild guild) { this.http = http; Id = guild.Id; Name = guild.Name; RegionId = guild.RegionId; AfkTimeout = guild.AfkTimeout; IsEmbedEnabled = guild.IsEmbedEnabled; VerificationLevel = guild.VerificationLevel; MfaLevel = guild.MfaLevel; ApplicationId = guild.ApplicationId; IsWidgetEnabled = guild.IsWidgetEnabled; WidgetChannelId = guild.WidgetChannelId; SystemChannelId = guild.SystemChannelId; DefaultMessageNotifications = guild.DefaultMessageNotifications; ExplicitContentFilter = guild.ExplicitContentFilter; OwnerId = guild.OwnerId; AfkChannelId = guild.AfkChannelId; EmbedChannelId = guild.EmbedChannelId; MaxPresences = guild.MaxPresences; MaxMembers = guild.MaxMembers; VanityUrlCode = guild.VanityUrlCode; Description = guild.Description; PremiumTier = guild.PremiumTier; PremiumSubscriptionCount = guild.PremiumSubscriptionCount; PreferredLocale = guild.PreferredLocale; if (guild.Icon != null) { Icon = DiscordCdnUrl.ForGuildIcon(guild.Id, guild.Icon); } if (guild.Splash != null) { Splash = DiscordCdnUrl.ForGuildSplash(guild.Id, guild.Splash); } if (guild.Banner != null) { Splash = DiscordCdnUrl.ForGuildBanner(guild.Id, guild.Banner); } Features = new List <string>(guild.Features); Roles = guild.Roles.CreateReadonlyCopy(); Emojis = guild.Emojis.CreateReadonlyCopy(); }
internal DiscordGuild(DiscordHttpClient http, MutableGuild guild) { this.http = http; Id = guild.Id; Name = guild.Name; RegionId = guild.RegionId; AfkTimeout = guild.AfkTimeout; IsEmbedEnabled = guild.IsEmbedEnabled; VerificationLevel = guild.VerificationLevel; MfaLevel = guild.MfaLevel; ApplicationId = guild.ApplicationId; IsWidgetEnabled = guild.IsWidgetEnabled; WidgetChannelId = guild.WidgetChannelId; SystemChannelId = guild.SystemChannelId; DefaultMessageNotifications = guild.DefaultMessageNotifications; ExplicitContentFilter = guild.ExplicitContentFilter; OwnerId = guild.OwnerId; AfkChannelId = guild.AfkChannelId; EmbedChannelId = guild.EmbedChannelId; if (guild.Icon != null) { Icon = DiscordCdnUrl.ForGuildIcon(guild.Id, guild.Icon); } if (guild.Splash != null) { Splash = DiscordCdnUrl.ForGuildSplash(guild.Id, guild.Splash); } Features = new List <string>(guild.Features); Roles = guild.Roles.CreateReadonlyCopy(); Emojis = guild.Emojis.CreateReadonlyCopy(); }
void HandleGuildCreateEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("id").Value; bool wasUnavailable = !cache.IsGuildAvailable(guildId); // Update guild MutableGuild mutableGuild; if (!cache.Guilds.TryGetValue(guildId, out mutableGuild)) { mutableGuild = new MutableGuild(guildId, http); cache.Guilds[guildId] = mutableGuild; } mutableGuild.Update(data); // Ensure the cache guildId list contains this guild (it uses a hashset so don't worry about duplicates). cache.AddGuildId(guildId); // GUILD_CREATE specifics // Update metadata cache.GuildMetadata[guildId] = new DiscordGuildMetadata(data); // Deserialize members cache.GuildMembers.Clear(guildId); IList <DiscordApiData> membersArray = data.GetArray("members"); for (int i = 0; i < membersArray.Count; i++) { DiscordApiData memberData = membersArray[i]; DiscordApiData userData = memberData.Get("user"); Snowflake userId = userData.GetSnowflake("id").Value; MutableUser user; if (!cache.Users.TryGetValue(userId, out user)) { user = new MutableUser(userId, false, http); cache.Users[userId] = user; } user.Update(userData); MutableGuildMember member; if (!cache.GuildMembers.TryGetValue(guildId, userId, out member)) { member = new MutableGuildMember(user, guildId, http); cache.GuildMembers[guildId, userId] = member; } member.Update(memberData); } // Deserialize channels cache.ClearGuildChannels(guildId); IList <DiscordApiData> channelsArray = data.GetArray("channels"); for (int i = 0; i < channelsArray.Count; i++) { DiscordApiData channelData = channelsArray[i]; DiscordChannelType channelType = (DiscordChannelType)channelData.GetInteger("type"); DiscordGuildChannel channel = null; if (channelType == DiscordChannelType.GuildText) { channel = new DiscordGuildTextChannel(http, channelData, guildId); } else if (channelType == DiscordChannelType.GuildVoice) { channel = new DiscordGuildVoiceChannel(http, channelData, guildId); } else if (channelType == DiscordChannelType.GuildCategory) { channel = new DiscordGuildCategoryChannel(http, channelData, guildId); } if (channel != null) { cache.AddGuildChannel(channel); } } // Deserialize voice states cache.GuildVoiceStates.Clear(guildId); IList <DiscordApiData> voiceStatesArray = data.GetArray("voice_states"); for (int i = 0; i < voiceStatesArray.Count; i++) { DiscordVoiceState state = new DiscordVoiceState(guildId, voiceStatesArray[i]); UpdateMemberVoiceState(state); } // Deserialize presences cache.GuildPresences.Clear(guildId); IList <DiscordApiData> presencesArray = data.GetArray("presences"); for (int i = 0; i < presencesArray.Count; i++) { // Presence's in GUILD_CREATE do not contain full user objects, // so don't attempt to update them here. DiscordApiData presenceData = presencesArray[i]; Snowflake userId = presenceData.LocateSnowflake("user.id").Value; cache.GuildPresences[guildId, userId] = new DiscordUserPresence(userId, presenceData); } // Mark the guild as available cache.SetGuildAvailability(guildId, true); // Fire event if (wasUnavailable) { OnGuildAvailable?.Invoke(this, new GuildEventArgs(shard, mutableGuild.ImmutableEntity)); } else { OnGuildCreated?.Invoke(this, new GuildEventArgs(shard, mutableGuild.ImmutableEntity)); } }