Ejemplo n.º 1
0
        /// <summary> Initializes a new instance of the DiscordClient class. </summary>
        public DiscordClient(DiscordConfig config = null)
        {
            Config = config ?? new DiscordConfig();
            Config.Lock();

            State = (int)ConnectionState.Disconnected;
            Status = UserStatus.Online;

            //Logging
            Log = new LogManager(this);
            Logger = Log.CreateLogger("Discord");

            //Async
            _taskManager = new TaskManager(Cleanup);
            _connectionLock = new AsyncLock();
            _disconnectedEvent = new ManualResetEvent(true);
            _connectedEvent = new ManualResetEventSlim(false);
            CancelToken = new CancellationToken(true);

            //Cache
            _servers = new ConcurrentDictionary<ulong, Server>();
            _channels = new ConcurrentDictionary<ulong, Channel>();
            _privateChannels = new ConcurrentDictionary<ulong, Channel>();

            //Serialization
            _serializer = new JsonSerializer();
            _serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
#if TEST_RESPONSES
            _serializer.CheckAdditionalContent = true;
            _serializer.MissingMemberHandling = MissingMemberHandling.Error;
#else
            _serializer.Error += (s, e) =>
            {
                e.ErrorContext.Handled = true;
                Logger.Error("Serialization Failed", e.ErrorContext.Error);
            };
#endif

            //Networking
            ClientAPI = new RestClient(Config, DiscordConfig.ClientAPIUrl, Log.CreateLogger("ClientAPI"));
            StatusAPI = new RestClient(Config, DiscordConfig.StatusAPIUrl, Log.CreateLogger("StatusAPI"));
            GatewaySocket = new GatewaySocket(this, _serializer, Log.CreateLogger("Gateway"));
            GatewaySocket.Connected += (s, e) =>
            {
                if (State == ConnectionState.Connecting)
                    EndConnect();
            };
            GatewaySocket.Disconnected += (s, e) => OnDisconnected(e.WasUnexpected, e.Exception);
            GatewaySocket.ReceivedDispatch += (s, e) => OnReceivedEvent(e);

            if (Config.UseMessageQueue)
                MessageQueue = new MessageQueue(this, Log.CreateLogger("MessageQueue"));

            //Extensibility
            Services = new ServiceManager(this);

            //Import/Export
            //_messageImporter = new JsonSerializer();
            //_messageImporter.ContractResolver = new Message.ImportResolver();
        }
Ejemplo n.º 2
0
        /// <summary> Initializes a new instance of the DiscordClient class. </summary>
        public DiscordClient(DiscordConfig config)
        {
            Config = config;

            State  = (int)ConnectionState.Disconnected;
            Status = UserStatus.Online;

            //Logging
            Log    = new LogManager(this);
            Logger = Log.CreateLogger("Discord");
            if (config.LogLevel >= LogSeverity.Verbose)
            {
                _connectionStopwatch = new Stopwatch();
            }

            //Async
            _taskManager       = new TaskManager(Cleanup);
            _connectionLock    = new AsyncLock();
            _disconnectedEvent = new ManualResetEvent(true);
            _connectedEvent    = new ManualResetEventSlim(false);
            CancelToken        = new CancellationToken(true);

            //Cache
            //ConcurrentLevel = 2 (only REST and WebSocket can add/remove)
            _servers         = new ConcurrentDictionary <ulong, Server>(2, 0);
            _channels        = new ConcurrentDictionary <ulong, IChannel>(2, 0);
            _privateChannels = new ConcurrentDictionary <ulong, PrivateChannel>(2, 0);

            //Serialization
            Serializer = new JsonSerializer();
            Serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
#if TEST_RESPONSES
            Serializer.CheckAdditionalContent = true;
            Serializer.MissingMemberHandling  = MissingMemberHandling.Error;
#else
            Serializer.CheckAdditionalContent = false;
            Serializer.MissingMemberHandling  = MissingMemberHandling.Ignore;
#endif
            Serializer.Error += (s, e) =>
            {
                e.ErrorContext.Handled = true;
                Logger.Error("Serialization Failed", e.ErrorContext.Error);
            };

            //Networking
            ClientAPI     = new JsonRestClient(Config, DiscordConfig.ClientAPIUrl, Log.CreateLogger("ClientAPI"));
            StatusAPI     = new JsonRestClient(Config, DiscordConfig.StatusAPIUrl, Log.CreateLogger("StatusAPI"));
            GatewaySocket = new GatewaySocket(Config, Serializer, Log.CreateLogger("Gateway"));

            //GatewaySocket.Disconnected += (s, e) => OnDisconnected(e.WasUnexpected, e.Exception);
            GatewaySocket.ReceivedDispatch += (s, e) => OnReceivedEvent(e);

            MessageQueue = new MessageQueue(ClientAPI, Log.CreateLogger("MessageQueue"));

            //Extensibility
            _services = new ServiceCollection(this);
        }
Ejemplo n.º 3
0
        public DiscordClient(DiscordConfig config = null) : this()
        {
            if (config == null)
            {
                config = new DiscordConfig();
            }

            Config = new LockedDiscordConfig(config);
            FinishConfig();
        }
Ejemplo n.º 4
0
        public DiscordClient(DiscordConfig config = null)
        {
            if (config == null)
            {
                config = new DiscordConfig();
            }

            config.SuperProperties.Base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(config.SuperProperties)));

            Config = config;

            HttpClient = new DiscordHttpClient(this);

            if (Config.GetFingerprint)
            {
                HttpClient.UpdateFingerprint();
            }
        }
Ejemplo n.º 5
0
 public DiscordClient(string token, DiscordConfig config = null) : this(config)
 {
     Token = token;
 }
Ejemplo n.º 6
0
 private static DiscordConfig ProcessConfig(Action<DiscordConfig> func)
 {
     var config = new DiscordConfig();
     func(config);
     return config;
 }
Ejemplo n.º 7
0
 public DiscordClient(DiscordConfig config)
 {
 }