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)); } }
static void SetClientEvents() { client.Log += Client_Log; client.Ready += Client_Ready; client.MessageReceived += MessageReceived; client.ReactionAdded += Client_ReactionAdded; client.ReactionRemoved += Client_ReactionRemoved; client.JoinedGuild += Client_JoinedGuild; client.ChannelCreated += (SocketChannel arg) => { OnChannelCreated?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.ChannelDestroyed += (SocketChannel arg) => { OnChannelDestroyed?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.ChannelUpdated += (SocketChannel arg1, SocketChannel arg2) => { OnChannelUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.CurrentUserUpdated += (SocketSelfUser arg1, SocketSelfUser arg2) => { OnCurrentUserUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.GuildAvailable += (SocketGuild arg) => { OnGuildAvailable?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.GuildMembersDownloaded += (SocketGuild arg) => { OnGuildMembersDownloaded?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.GuildMemberUpdated += (Cacheable <SocketGuildUser, ulong> arg1, SocketGuildUser arg2) => { OnGuildMemberUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.GuildUnavailable += (SocketGuild arg) => { OnGuildUnavailable?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.GuildUpdated += (SocketGuild arg1, SocketGuild arg2) => { OnGuildUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.LatencyUpdated += (int arg1, int arg2) => { OnLatencyUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.LeftGuild += (SocketGuild arg) => { OnLeftGuild?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.LoggedIn += () => { OnLoggedIn?.InvokeParallel(); return(Task.FromResult(default(object))); }; client.LoggedOut += () => { OnLoggedOut?.InvokeParallel(); return(Task.FromResult(default(object))); }; client.MessageDeleted += (Cacheable <IMessage, ulong> arg1, Cacheable <IMessageChannel, ulong> arg2) => { OnMessageDeleted?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.MessagesBulkDeleted += (IReadOnlyCollection <Cacheable <IMessage, ulong> > arg1, Cacheable <IMessageChannel, ulong> arg2) => { OnMessagesBulkDeleted?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.MessageUpdated += (Cacheable <IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3) => { OnMessageUpdated?.InvokeParallel(arg1, arg2, arg3); return(Task.FromResult(default(object))); }; client.ReactionsCleared += (Cacheable <IUserMessage, ulong> arg1, Cacheable <IMessageChannel, ulong> arg2) => { OnReactionsCleared?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.RecipientAdded += (SocketGroupUser arg) => { OnRecipientAdded?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.RecipientRemoved += (SocketGroupUser arg) => { OnRecipientRemoved?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.RoleCreated += (SocketRole arg) => { OnRoleCreated?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.RoleDeleted += (SocketRole arg) => { OnRoleDeleted?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.RoleUpdated += (SocketRole arg1, SocketRole arg2) => { OnRoleUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserBanned += (SocketUser arg1, SocketGuild arg2) => { OnUserBanned?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserIsTyping += (Cacheable <IUser, ulong> arg1, Cacheable <IMessageChannel, ulong> arg2) => { OnUserIsTyping?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserJoined += (SocketGuildUser arg) => { OnUserJoined?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.UserLeft += (SocketGuild arg1, SocketUser arg2) => { OnUserLeft?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserUnbanned += (SocketUser arg1, SocketGuild arg2) => { OnUserUnbanned?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserUpdated += (SocketUser arg1, SocketUser arg2) => { OnUserUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserVoiceStateUpdated += (SocketUser arg1, SocketVoiceState arg2, SocketVoiceState arg3) => { OnUserVoiceStateUpdated?.InvokeParallel(arg1, arg2, arg3); return(Task.FromResult(default(object))); }; client.VoiceServerUpdated += (SocketVoiceServer arg) => { OnVoiceServerUpdated?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; }