private async Task SocketMessageReceived(GatewayConnection conn, EventArgs.SocketMessageEventArgs e)
        {
            _logger.LogTrace(LogEvent.MessageReceived, "Opcode = {}, Event = {}", e.Payload.Opcode, e.Payload.Event ?? "null");

            if (e.Payload.Opcode == 10)
            {
                // hello received, send identify or resume
                if (_sessionId == null)
                {
                    var identify = new IdentifyPayload
                    {
                        Token   = _token,
                        Intents = 512
                    };

                    await _gateway.SendAsync(2, identify);
                }
                else
                {
                    _logger.LogDebug(LogEvent.MessageReceived, "Attempting to resume");
                    var resume = new ResumePayload
                    {
                        SequenceNumber = _gateway.LastSeqNumber,
                        SessionId      = _sessionId,
                        Token          = _token
                    };

                    await _gateway.SendAsync(6, resume);
                }
            }

            if (e.Payload.Opcode == 9)
            {
                _logger.LogDebug(LogEvent.MessageReceived, "Invalid Session received, reconnecting");
                _sessionId = null;

                await _gateway.ClientWebSocket.CloseAsync(WebSocketCloseStatus.Empty, "reconnecting", CancellationToken.None);
            }

            if (e.Payload.Opcode == 0)
            {
                if (e.Payload.Event == "MESSAGE_CREATE")
                {
                    var message = JObject.FromObject(e.Payload.Data).ToObject <DiscordMessage>();
                    MessageReceived?.Invoke(this, new MessageReceivedEventArgs {
                        Message = message, User = message.User
                    });
                }

                if (e.Payload.Event == "READY")
                {
                    var ready = JObject.FromObject(e.Payload.Data).ToObject <ReadyPayload>();

                    _sessionId = ready.SessionId;
                    _logger.LogInformation(LogEvent.Ready, "Connected to discord as {}#{}", ready.User.Username, ready.User.Discriminator);
                }
            }
        }
Example #2
0
        private Task SendIdentifyAsync()
        {
            var Content = new IdentifyPayload()
            {
                Token           = $"{this.TokenType} {this.Token}",
                Properties      = new Properties(),
                Compress        = false,
                LargeThreashold = this.LargeThreashold,
                Shard           = new[] { 0, 1 }
            };

            var pldata = new GatewayEvent()
            {
                Code         = OPCode.Identify,
                EventPayload = Content
            };

            var payload = JsonConvert.SerializeObject(pldata);

            WebSocketClient.Send(payload);
            return(Task.CompletedTask);
        }