示例#1
0
        internal void OnReady(object sender, EventHandlerArgs args)
        {
            Ready ready = args.EventData as Ready;

            sessionIdentifier = ready.SessionIdentifier;
            if (++SessionNumber != 1)
            {
                Interlocked.Exchange(ref lastSequence, 0);
            }
        }
        internal void OnNewClientEventReceived(string eventName, string eventData)
        {
            Enum.TryParse(eventName, out Events eventToRaiseName);
            if (eventToRaiseName == Events.UnknownEvent)
            {
                // TODO : интструмент логирования ($"Unknown dispatch event. /n Event name: {eventName} /n Event data: {eventData}");
            }
            EventHandlerArgs eventArgs;
            object           eventArgsData;

            Console.WriteLine($"Dispatch: {eventName}");
            switch (eventToRaiseName)
            {
            case Events.READY:
                eventArgsData = JsonConvert.DeserializeObject <Ready>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                Ready(this, eventArgs);
                break;

            case Events.RESUMED:
                Console.WriteLine("resumed");
                //TODO
                break;

            case Events.RECONNECT:
                //TODO
                Console.WriteLine("reconnected");
                break;

            case Events.CHANNEL_CREATE:
                eventArgsData = JsonConvert.DeserializeObject <IChannel>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                ChannelCreated(this, eventArgs);
                break;

            case Events.CHANNEL_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <IChannel>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                ChannelUpdated(this, eventArgs);
                break;

            case Events.CHANNEL_DELETE:
                eventArgsData = JsonConvert.DeserializeObject <IChannel>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                ChannelDeleted(this, eventArgs);
                break;

            case Events.CHANNEL_PINS_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <ChannelPinsUpdatedEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                ChannelPinsUpdated(this, eventArgs);
                break;

            case Events.GUILD_CREATE:
                eventArgsData = JsonConvert.DeserializeObject <Guild>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildCreated(this, eventArgs);
                break;

            case Events.GUILD_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <Guild>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildUpdated(this, eventArgs);
                break;

            case Events.GUILD_DELETE:
                eventArgsData = JsonConvert.DeserializeObject <GuildPreview>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildDeleted(this, eventArgs);
                break;

            case Events.GUILD_BAN_ADD:
                eventArgsData = JsonConvert.DeserializeObject <Ban>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildBanAdded(this, eventArgs);
                break;

            case Events.GUILD_BAN_REMOVE:
                eventArgsData = JsonConvert.DeserializeObject <Ban>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildBanRemoved(this, eventArgs);
                break;

            case Events.GUILD_EMOJIS_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <GuildEmojiUpdatedEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildEmojisUpdated(this, eventArgs);
                break;

            case Events.GUILD_INTEGRATIONS_UPDATE:     //TODO : хз что за интеграция нужно помять
                //eventArgsData = JToken.Load(eventData);
                //eventArgs = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                //GuildIntegrationsUpdate(this, eventArgs);
                break;

            case Events.GUILD_MEMBER_ADD:
                eventArgsData = JsonConvert.DeserializeObject <GuildUser>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildMemberAdded(this, eventArgs);
                break;

            case Events.GUILD_MEMBER_REMOVE:
                eventArgsData = JsonConvert.DeserializeObject <GuildMember>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildMemberRemoved(this, eventArgs);
                break;

            case Events.GUILD_MEMBER_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <GuildUser>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildMemberUpdated(this, eventArgs);
                break;

            case Events.GuildMembersChunkReceived:
                eventArgsData = JsonConvert.DeserializeObject <GuildMemberChunk>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildMemberChunksReceived(this, eventArgs);
                break;

            case Events.GUILD_ROLE_CREATE:
                eventArgsData = JsonConvert.DeserializeObject <RoleEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildRoleCreated(this, eventArgs);
                break;

            case Events.GUILD_ROLE_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <RoleEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildRoleUpdated(this, eventArgs);
                break;

            case Events.GUILD_ROLE_DELETE:
                eventArgsData = JsonConvert.DeserializeObject <RoleDeletedEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                GuildRoleDeleted(this, eventArgs);
                break;

            case Events.INVITE_CREATE:
                eventArgsData = JsonConvert.DeserializeObject <Invite>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                InviteCreated(this, eventArgs);
                break;

            case Events.INVITE_DELETE:
                eventArgsData = JsonConvert.DeserializeObject <InviteBase>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                InviteDeleted(this, eventArgs);
                break;

            case Events.MESSAGE_CREATE:
                eventArgsData = JsonConvert.DeserializeObject <Message>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                MessageCreated(this, eventArgs);
                break;

            case Events.MESSAGE_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <Message>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                MessageUpdated(this, eventArgs);
                break;

            case Events.MESSAGE_DELETE:
                eventArgsData = JsonConvert.DeserializeObject <MessageBase>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                MessageDeleted(this, eventArgs);
                break;

            case Events.MESSAGE_DELETE_BULK:
                eventArgsData = JsonConvert.DeserializeObject <MessageDeletedBulk>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                MessageDeletedBulk(this, eventArgs);
                break;

            case Events.MESSAGE_REACTION_ADD:
                eventArgsData = JsonConvert.DeserializeObject <MessageReactionEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                MessageReactionAdded(this, eventArgs);
                break;

            case Events.MESSAGE_REACTION_REMOVE:
                eventArgsData = JsonConvert.DeserializeObject <MessageReactionEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                MessageReactionRemoved(this, eventArgs);
                break;

            case Events.MESSAGE_REACTION_REMOVE_ALL:
                eventArgsData = JsonConvert.DeserializeObject <MessageReactionEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                MessageReactionRemovedAll(this, eventArgs);
                break;

            case Events.MESSAGE_REACTION_REMOVE_EMOJI:
                eventArgsData = JsonConvert.DeserializeObject <MessageReactionEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                MessageReactionEmojiRemoved(this, eventArgs);
                break;

            case Events.PRESENCE_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <IPresence>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                PresenceUpdated(this, eventArgs);
                break;

            case Events.TYPING_START:
                eventArgsData = JsonConvert.DeserializeObject <UserTypingEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                TypingStarted(this, eventArgs);
                break;

            case Events.USER_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <User>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                UserUpdated(this, eventArgs);
                break;

            case Events.VOICE_STATE_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <IVoiceSession>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                VoiceStateUpdated(this, eventArgs);
                break;

            case Events.VOICE_SERVER_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <VoiceServerUpdate>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                VoiceServerUpdated(this, eventArgs);
                break;

            case Events.WEBHOOKS_UPDATE:
                eventArgsData = JsonConvert.DeserializeObject <WebhookUpdatedEvent>(eventData);
                eventArgs     = new EventHandlerArgs(eventArgsData.GetType(), eventToRaiseName, eventArgsData);
                WebhooksUpdated(this, eventArgs);
                break;
            }
        }
 private short identifyLimit; // TODO : метод обновлящий значение при отправке новой идентификации
                              // и изначальное записывание значения полученое при первичном запросе к HTTP API
 #endregion
 #region Event handlers
 private void OnReady(object sender, EventHandlerArgs args)
 {
     sessionStarted = DateTime.Now;
 }