void HandleMessageDeleteEvent(DiscordApiData data) { Snowflake messageId = data.GetSnowflake("id").Value; Snowflake channelId = data.GetSnowflake("channel_id").Value; OnMessageDeleted?.Invoke(this, new MessageDeleteEventArgs(shard, messageId, channelId)); }
void HandleChannelUpdateEvent(DiscordApiData data) { Snowflake id = data.GetSnowflake("id").Value; DiscordChannelType type = (DiscordChannelType)data.GetInteger("type"); Snowflake guildId = data.GetSnowflake("guild_id").Value; DiscordGuildChannel channel = null; if (type == DiscordChannelType.GuildText) { channel = new DiscordGuildTextChannel(http, data, guildId); } else if (type == DiscordChannelType.GuildVoice) { channel = new DiscordGuildVoiceChannel(http, data, guildId); } else if (type == DiscordChannelType.GuildCategory) { channel = new DiscordGuildCategoryChannel(http, data, guildId); } if (channel != null) { cache.GuildChannels[id] = channel; OnGuildChannelUpdated?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel)); } else { log.LogWarning($"Failed to update channel {id} because the type ({type}) doesn't have an implementation!"); } }
void HandleMessageReactionRemoveAllEvent(DiscordApiData data) { Snowflake channelId = data.GetSnowflake("channel_id").Value; Snowflake messageId = data.GetSnowflake("message_id").Value; OnMessageAllReactionsRemoved?.Invoke(this, new MessageReactionRemoveAllEventArgs(shard, messageId, channelId)); }
void HandleWebhooksUpdate(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; Snowflake channelId = data.GetSnowflake("channel_id").Value; OnWebhookUpdated?.Invoke(this, new WebhooksUpdateEventArgs(shard, guildId, channelId)); }
void HandleChannelDeleteEvent(DiscordApiData data) { Snowflake id = data.GetSnowflake("id").Value; DiscordChannelType type = (DiscordChannelType)data.GetInteger("type").Value; if (type == DiscordChannelType.DirectMessage) { // DM channel DiscordDMChannel dm; if (cache.DMChannels.TryRemove(id, out MutableDMChannel mutableDM)) { mutableDM.ClearReferences(); dm = mutableDM.ImmutableEntity; } else { dm = new DiscordDMChannel(http, data); } OnDMChannelRemoved?.Invoke(this, new DMChannelEventArgs(shard, dm)); } else if (type == DiscordChannelType.GuildText || type == DiscordChannelType.GuildVoice || type == DiscordChannelType.GuildCategory) { // Guild channel Snowflake guildId = data.GetSnowflake("guild_id").Value; DiscordGuildChannel channel; if (type == DiscordChannelType.GuildText) { if (!cache.GuildChannels.TryRemove(id, out channel)) { channel = new DiscordGuildTextChannel(http, data, guildId); } } else if (type == DiscordChannelType.GuildVoice) { if (!cache.GuildChannels.TryRemove(id, out channel)) { channel = new DiscordGuildVoiceChannel(http, data, guildId); } } else if (type == DiscordChannelType.GuildCategory) { if (!cache.GuildChannels.TryRemove(id, out channel)) { channel = new DiscordGuildCategoryChannel(http, data, guildId); } } else { throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!"); } OnGuildChannelRemoved?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel)); } }
void HandleTypingStartEvent(DiscordApiData data) { Snowflake userId = data.GetSnowflake("user_id").Value; Snowflake channelId = data.GetSnowflake("channel_id").Value; int timestamp = data.GetInteger("timestamp").Value; OnTypingStarted?.Invoke(this, new TypingStartEventArgs(shard, userId, channelId, timestamp)); }
void HandleChannelCreateEvent(DiscordApiData data) { Snowflake id = data.GetSnowflake("id").Value; DiscordChannelType type = (DiscordChannelType)data.GetInteger("type").Value; if (type == DiscordChannelType.DirectMessage) { // DM channel DiscordApiData recipientData = data.GetArray("recipients").First(); Snowflake recipientId = recipientData.GetSnowflake("id").Value; MutableUser recipient; if (!cache.Users.TryGetValue(recipientId, out recipient)) { recipient = new MutableUser(recipientId, false, http); cache.Users[recipientId] = recipient; } recipient.Update(recipientData); MutableDMChannel mutableDMChannel; if (!cache.DMChannels.TryGetValue(id, out mutableDMChannel)) { mutableDMChannel = new MutableDMChannel(id, recipient, http); cache.DMChannels[id] = mutableDMChannel; } OnDMChannelCreated?.Invoke(this, new DMChannelEventArgs(shard, mutableDMChannel.ImmutableEntity)); } else if (type == DiscordChannelType.GuildText || type == DiscordChannelType.GuildVoice) { // Guild channel Snowflake guildId = data.GetSnowflake("guild_id").Value; DiscordGuildChannel channel; if (type == DiscordChannelType.GuildText) { channel = new DiscordGuildTextChannel(http, data, guildId); } else if (type == DiscordChannelType.GuildVoice) { channel = new DiscordGuildVoiceChannel(http, data, guildId); } else if (type == DiscordChannelType.GuildCategory) { channel = new DiscordGuildCategoryChannel(http, data, guildId); } else { throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!"); } cache.GuildChannels[id] = channel; OnGuildChannelCreated?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel)); } }
void HandleMessageReactionRemoveEvent(DiscordApiData data) { Snowflake userId = data.GetSnowflake("user_id").Value; Snowflake channelId = data.GetSnowflake("channel_id").Value; Snowflake messageId = data.GetSnowflake("message_id").Value; DiscordApiData emojiData = data.Get("emoji"); DiscordReactionEmoji emoji = new DiscordReactionEmoji(emojiData); OnMessageReactionRemoved?.Invoke(this, new MessageReactionEventArgs(shard, messageId, channelId, userId, emoji)); }
internal DiscordVoiceState(Snowflake guildId, DiscordApiData data) { GuildId = guildId; ChannelId = data.GetSnowflake("channel_id"); UserId = data.GetSnowflake("user_id").Value; SessionId = data.GetString("session_id"); IsServerDeaf = data.GetBoolean("deaf").Value; IsServerMute = data.GetBoolean("mute").Value; IsSelfDeaf = data.GetBoolean("self_deaf").Value; IsSelfMute = data.GetBoolean("self_mute").Value; IsSuppressed = data.GetBoolean("suppress").Value; }
void HandleGuildMemberRemoveEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; DiscordApiData userData = data.Get("user"); Snowflake userId = userData.GetSnowflake("id").Value; // Get user MutableUser mutableUser; if (!cache.Users.TryGetValue(userId, out mutableUser)) { mutableUser = new MutableUser(userId, false, http); cache.Users[userId] = mutableUser; } mutableUser.Update(userData); // Get and remove member if (cache.GuildMembers.TryRemove(guildId, userId, out MutableGuildMember mutableMember)) { // Ensure all references are removed mutableMember.ClearReferences(); // Fire event OnGuildMemberRemoved?.Invoke(this, new GuildMemberEventArgs(shard, guildId, mutableMember.ImmutableEntity)); } }
void HandleGuildMemberAddEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; DiscordApiData userData = data.Get("user"); Snowflake userId = userData.GetSnowflake("id").Value; // Get user MutableUser mutableUser; if (!cache.Users.TryGetValue(userId, out mutableUser)) { mutableUser = new MutableUser(userId, false, http); cache.Users[userId] = mutableUser; } // Update user mutableUser.Update(userData); // Get or create member MutableGuildMember mutableMember; if (!cache.GuildMembers.TryGetValue(guildId, userId, out mutableMember)) { mutableMember = new MutableGuildMember(mutableUser, guildId, http); cache.GuildMembers[guildId, userId] = mutableMember; } // Update member mutableMember.Update(data); // Fire event OnGuildMemberAdded?.Invoke(this, new GuildMemberEventArgs(shard, guildId, mutableMember.ImmutableEntity)); }
void HandleGuildEmojisUpdateEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; if (cache.Guilds.TryGetValue(guildId, out MutableGuild mutableGuild)) { // Clear existing emojis mutableGuild.Emojis.Clear(); // Deseralize new emojis IList <DiscordApiData> emojisArray = data.GetArray("emojis"); for (int i = 0; i < emojisArray.Count; i++) { DiscordEmoji emoji = new DiscordEmoji(emojisArray[i]); mutableGuild.Emojis[emoji.Id] = emoji; } // Dirty the guild mutableGuild.Dirty(); // Invoke the event OnGuildEmojisUpdated?.Invoke(this, new GuildEventArgs(shard, mutableGuild.ImmutableEntity)); } else { throw new ShardCacheException($"Guild {guildId} was not in the cache!"); } }
async Task HandleVoiceServerUpdateEvent(DiscordApiData data) { Snowflake?guildId = data.GetSnowflake("guild_id"); if (guildId.HasValue) // Only guild voice channels are supported so far. { string token = data.GetString("token"); string endpoint = data.GetString("endpoint"); DiscordVoiceConnection connection; if (shard.Voice.TryGetVoiceConnection(guildId.Value, out connection)) { // Notify the connection of the server update await connection.OnVoiceServerUpdated(token, endpoint).ConfigureAwait(false); } else { throw new ShardCacheException($"Voice connection for guild {guildId.Value} was not in the cache!"); } } else { throw new NotImplementedException("Non-guild voice channels are not supported yet."); } }
void HandlePresenceUpdateEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; // Update user DiscordApiData userData = data.Get("user"); Snowflake userId = userData.GetSnowflake("id").Value; if (cache.Users.TryGetValue(userId, out MutableUser mutableUser)) { mutableUser.PartialUpdate(userData); } else { // Don't throw exception since we can still update everything else... log.LogError($"[PRESENCE_UPDATE] Failed to update user {userId}, they were not in the cache!"); } // Update presence DiscordUserPresence presence = new DiscordUserPresence(userId, data); cache.GuildPresences[guildId, userId] = presence; // Update member if (cache.GuildMembers.TryGetValue(guildId, userId, out MutableGuildMember mutableMember)) { mutableMember.PartialUpdate(data); // Fire event OnPresenceUpdated?.Invoke(this, new PresenceEventArgs(shard, guildId, mutableMember.ImmutableEntity, presence)); } // It is technically valid for the member to not exist here, especially if the guild is considered large. }
internal DiscordChannelMention(DiscordApiData data) : base(data) { GuildId = data.GetSnowflake("guild_id").GetValueOrDefault(); Type = (DiscordChannelType)(data.GetInteger("type") ?? 0); Name = data.GetString("name"); }
void HandleGuildMemberUpdateEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; DiscordApiData userData = data.Get("user"); Snowflake userId = userData.GetSnowflake("id").Value; // Get user MutableUser mutableUser; if (!cache.Users.TryGetValue(userId, out mutableUser)) { mutableUser = new MutableUser(userId, false, http); cache.Users[userId] = mutableUser; } mutableUser.Update(userData); // Get member if (cache.GuildMembers.TryGetValue(guildId, userId, out MutableGuildMember mutableMember)) { // Update member mutableMember.PartialUpdate(data); // Fire event OnGuildMemberUpdated?.Invoke(this, new GuildMemberEventArgs(shard, guildId, mutableMember.ImmutableEntity)); } // It is technically valid for the member to not exist here, especially if the guild is considered large. }
internal DiscordGuildMetadata(DiscordApiData data) { GuildId = data.GetSnowflake("id").Value; IsLarge = data.GetBoolean("large") ?? false; JoinedAt = data.GetDateTime("joined_at").Value; MemberCount = data.GetInteger("member_count").Value; }
void HandleChannelPinsUpdateEvent(DiscordApiData data) { DateTime? lastPinTimestamp = data.GetDateTime("last_pin_timestamp"); Snowflake channelId = data.GetSnowflake("channel_id").Value; OnChannelPinsUpdated?.Invoke(this, new ChannelPinsUpdateEventArgs(shard, channelId, lastPinTimestamp)); }
void HandleSpeaking(DiscordApiData payload, DiscordApiData data) { Snowflake userId = data.GetSnowflake("user_id").Value; int ssrc = data.GetInteger("ssrc").Value; bool isSpeaking = data.GetBoolean("speaking").Value; OnUserSpeaking?.Invoke(this, new VoiceSpeakingEventArgs(userId, ssrc, isSpeaking)); }
void HandleMessageDeleteBulkEvent(DiscordApiData data) { Snowflake channelId = data.GetSnowflake("channel_id").Value; IList <DiscordApiData> idArray = data.GetArray("ids"); for (int i = 0; i < idArray.Count; i++) { Snowflake messageId = idArray[i].ToSnowflake().Value; OnMessageDeleted?.Invoke(this, new MessageDeleteEventArgs(shard, messageId, channelId)); } }
async Task HandleVoiceStateUpdateEvent(DiscordApiData data) { Snowflake?guildId = data.GetSnowflake("guild_id"); if (guildId.HasValue) // Only guild voice channels are supported so far. { Snowflake userId = data.GetSnowflake("user_id").Value; // Update the voice state DiscordVoiceState voiceState = new DiscordVoiceState(guildId.Value, data); UpdateMemberVoiceState(voiceState); // If this voice state belongs to the current bot, // then we need to notify the connection of the session ID. if (userId == shard.UserId) { DiscordVoiceConnection connection; if (shard.Voice.TryGetVoiceConnection(guildId.Value, out connection)) { if (voiceState.ChannelId.HasValue) { // Notify the connection of the new state await connection.OnVoiceStateUpdated(voiceState).ConfigureAwait(false); } else if (connection.IsConnected) { // The user has left the channel, so make sure they are disconnected. await connection.DisconnectAsync().ConfigureAwait(false); } } } // Fire event OnVoiceStateUpdated?.Invoke(this, new VoiceStateEventArgs(shard, voiceState)); } else { throw new NotImplementedException("Non-guild voice channels are not supported yet."); } }
void HandleGuildRoleDeleteEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; if (cache.Guilds.TryGetValue(guildId, out MutableGuild mutableGuild)) { Snowflake roleId = data.GetSnowflake("role_id").Value; if (mutableGuild.Roles.TryRemove(roleId, out DiscordRole role)) { OnGuildRoleDeleted?.Invoke(this, new GuildRoleEventArgs(shard, mutableGuild.ImmutableEntity, role)); } else { throw new ShardCacheException($"Role {roleId} was not in the guild {guildId} cache!"); } } else { throw new ShardCacheException($"Guild {guildId} was not in the cache!"); } }
void HandleUserUpdateEvent(DiscordApiData data) { Snowflake userId = data.GetSnowflake("id").Value; MutableUser mutableUser; if (!cache.Users.TryGetValue(userId, out mutableUser)) { mutableUser = new MutableUser(userId, false, http); cache.Users[userId] = mutableUser; } mutableUser.Update(data); OnUserUpdated?.Invoke(this, new UserEventArgs(shard, mutableUser.ImmutableEntity)); }
public void Update(DiscordApiData data) { Name = data.GetString("name"); Icon = data.GetString("icon"); Splash = data.GetString("splash"); RegionId = data.GetString("region"); AfkTimeout = data.GetInteger("afk_timeout").Value; IsEmbedEnabled = data.GetBoolean("embed_enabled") ?? false; OwnerId = data.GetSnowflake("owner_id").Value; AfkChannelId = data.GetSnowflake("afk_channel_id"); EmbedChannelId = data.GetSnowflake("embed_channel_id"); ApplicationId = data.GetSnowflake("application_id"); IsWidgetEnabled = data.GetBoolean("widget_enabled") ?? false; WidgetChannelId = data.GetSnowflake("widget_channel_id"); SystemChannelId = data.GetSnowflake("system_channel_id"); ExplicitContentFilter = (GuildExplicitContentFilterLevel)data.GetInteger("explicit_content_filter").Value; VerificationLevel = (GuildVerificationLevel)data.GetInteger("verification_level").Value; DefaultMessageNotifications = (GuildNotificationOption)(data.GetInteger("default_message_notifications") ?? 0); MfaLevel = (GuildMfaLevel)data.GetInteger("mfa_level").Value; // Deserialize features IList <DiscordApiData> featuresArray = data.GetArray("features"); List <string> features = new List <string>(featuresArray.Count); for (int i = 0; i < features.Count; i++) { features[i] = featuresArray[i].ToString(); } Features = features; // Deserialize roles Roles.Clear(); IList <DiscordApiData> rolesArray = data.GetArray("roles"); for (int i = 0; i < rolesArray.Count; i++) { DiscordRole role = new DiscordRole(Http, Id, rolesArray[i]); Roles[role.Id] = role; } // Deserialize emojis Emojis.Clear(); IList <DiscordApiData> emojisArray = data.GetArray("emojis"); for (int i = 0; i < emojisArray.Count; i++) { DiscordEmoji emoji = new DiscordEmoji(emojisArray[i]); Emojis[emoji.Id] = emoji; } Dirty(); }
void HandleMessageUpdateEvent(DiscordApiData data) { // Get author DiscordApiData authorData = data.Get("author"); if (authorData != null) { Snowflake authorId = authorData.GetSnowflake("id").Value; bool isWebhookUser = !string.IsNullOrWhiteSpace(data.GetString("webhook_id")); MutableUser mutableAuthor; if (!cache.Users.TryGetValue(authorId, out mutableAuthor)) { mutableAuthor = new MutableUser(authorId, isWebhookUser, http); cache.Users[authorId] = mutableAuthor; } mutableAuthor.Update(authorData); } // Get mentioned users IList <DiscordApiData> mentionsArray = data.GetArray("mentions"); if (mentionsArray != null) { for (int i = 0; i < mentionsArray.Count; i++) { DiscordApiData userData = mentionsArray[i]; Snowflake userId = userData.GetSnowflake("id").Value; MutableUser mutableUser; if (!cache.Users.TryGetValue(userId, out mutableUser)) { mutableUser = new MutableUser(userId, false, http); cache.Users[userId] = mutableUser; } mutableUser.Update(userData); } } // Create message DiscordMessage message = new DiscordMessage(http, data); OnMessageUpdated?.Invoke(this, new MessageUpdateEventArgs(shard, message)); }
void HandleGuildBanRemoveEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; DiscordApiData userData = data.Get("user"); Snowflake userId = userData.GetSnowflake("id").Value; MutableUser mutableUser; if (!cache.Users.TryGetValue(userId, out mutableUser)) { mutableUser = new MutableUser(userId, false, http); cache.Users[userId] = mutableUser; } mutableUser.Update(userData); OnGuildBanRemoved?.Invoke(this, new GuildUserEventArgs(shard, guildId, mutableUser.ImmutableEntity)); }
void HandleGuildUpdateEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("id").Value; MutableGuild mutableGuild; if (cache.Guilds.TryGetValue(guildId, out mutableGuild)) { // Update guild mutableGuild.Update(data); // Fire event OnGuildUpdated?.Invoke(this, new GuildEventArgs(shard, mutableGuild.ImmutableEntity)); } else { throw new ShardCacheException($"Guild {guildId} was not in the cache!"); } }
void HandleGuildRoleUpdateEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; if (cache.Guilds.TryGetValue(guildId, out MutableGuild mutableGuild)) { DiscordApiData roleData = data.Get("role"); DiscordRole role = new DiscordRole(http, guildId, roleData); mutableGuild.Roles[role.Id] = role; mutableGuild.Dirty(); OnGuildRoleUpdated?.Invoke(this, new GuildRoleEventArgs(shard, mutableGuild.ImmutableEntity, role)); } else { throw new ShardCacheException($"Guild {guildId} was not in the cache!"); } }
void HandleGuildMembersChunkEvent(DiscordApiData data) { Snowflake guildId = data.GetSnowflake("guild_id").Value; // Get every member and ensure they are cached IList <DiscordApiData> membersData = data.GetArray("members"); DiscordGuildMember[] members = new DiscordGuildMember[membersData.Count]; for (int i = 0; i < membersData.Count; i++) { DiscordApiData memberData = membersData[i]; DiscordApiData userData = memberData.Get("user"); Snowflake userId = userData.GetSnowflake("id").Value; // Get user MutableUser mutableUser; if (!cache.Users.TryGetValue(userId, out mutableUser)) { mutableUser = new MutableUser(userId, false, http); cache.Users[userId] = mutableUser; } mutableUser.Update(userData); // Get or create member MutableGuildMember mutableMember; if (!cache.GuildMembers.TryGetValue(guildId, userId, out mutableMember)) { mutableMember = new MutableGuildMember(mutableUser, guildId, http); mutableMember.Update(memberData); cache.GuildMembers[guildId, userId] = mutableMember; } members[i] = mutableMember.ImmutableEntity; } // Fire event OnGuildMembersChunk?.Invoke(this, new GuildMemberChunkEventArgs(shard, guildId, members)); }
internal DiscordIdEntity(DiscordApiData data) { Id = data.GetSnowflake("id").Value; }