Ejemplo n.º 1
0
        public PayloadDecompressor(GatewayCompressionLevel compressionLevel)
        {
            if (compressionLevel == GatewayCompressionLevel.None)
            {
                throw new InvalidOperationException("Decompressor requires a valid compression mode.");
            }

            this.CompressionLevel = compressionLevel;
            this.CompressedStream = new MemoryStream();
            if (this.CompressionLevel == GatewayCompressionLevel.Stream)
            {
                this.DecompressorStream = new DeflateStream(this.CompressedStream, CompressionMode.Decompress);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method for configuring <see cref="DiscordClient"/>, accessing each configuration individually.
        /// </summary>
        /// <param name="token">Sets the token used to identify the client.</param>
        /// <param name="tokenType">Sets the type of the token used to identify the client. Defaults to <see cref="TokenType.Bot"/>.</param>
        /// <param name="minimumLogLevel">Sets the maximum logging level for messages. If left as <see langword="null"/>, and the <paramref name="logLevelDebugOnDebugging"/> property as <see langword="true"/>, the bot will use <see cref="LogLevel.Debug"/> when debugging Visual Studio and will <see cref="LogLevel.Information"/> when it starts without debugging.</param>
        /// <param name="logLevelDebugOnDebugging">Set if the bot will start using <see cref="LogLevel.Debug"/> when debugging Visual Studio, if <see langword="false"/>, the bot will always start at <see cref="LogLevel.Info"/></param>
        /// <param name="useRelativeRateLimit">Sets whether to rely on Discord for NTP (Network Time Protocol) synchronization with the "X-Ratelimit-Reset-After" header. If the system clock is unsynced, setting this to true will ensure ratelimits are synced with Discord and reduce the risk of hitting one. This should only be set to <see langword="false"/> if the system clock is synced with NTP. Defaults to <see langword="true"/>.</param>
        /// <param name="logTimestampFormat">Allows you to overwrite the time format used by the internal debug logger. The default is the format of your PC's date.</param>
        /// <param name="largeThreshold">Sets the member count threshold at which guilds are considered large. Defaults to 1000.></param>
        /// <param name="autoReconnect">Sets whether to automatically reconnect in case a connection is lost. Defaults to <see langword="false"/>.</param>
        /// <param name="shardId">Sets the ID of the shard to connect to. If not sharding, or sharding automatically, this value should be left with the default value of 0.</param>
        /// <param name="shardCount">Sets the total number of shards the bot is on. If not sharding, this value should be left with a default value of 1. If sharding automatically, this value will indicate how many shards to boot. If left default for automatic sharding, the client will determine the shard count automatically.</param>
        /// <param name="gatewayCompressionLevel">Sets the level of compression for WebSocket traffic. Disabling this option will increase the amount of traffic sent via WebSocket. Setting <see cref="GatewayCompressionLevel.Payload"/> will enable compression for READY and GUILD_CREATE payloads. Setting <see cref="GatewayCompressionLevel.Stream"/> will enable compression for the entire WebSocket stream, drastically reducing amount of traffic. Defaults to <see cref="GatewayCompressionLevel.Stream"/>.</param>
        /// <param name="messageCacheSize">Sets the size of the global message cache. Setting this to 0 will disable message caching entirely. Defaults to 1024.</param>
        /// <param name="webProxy">Sets the proxy to use for HTTP and WebSocket connections to Discord. Defaults to <see langword="null"/>.</param>
        /// <param name="httpTimeout">Sets the timeout for HTTP requests. Set to <see cref="Timeout.InfiniteTimeSpan"/> to disable timeouts. Defaults to 10 seconds.</param>
        /// <param name="reconnectIndefinitely">Defines that the client should attempt to reconnect indefinitely. This is typically a very bad idea to set to <see langword="true"/>, as it will swallow all connection errors. Defaults to <see langword="false"/>.</param>
        /// <param name="discordIntents">Sets the gateway intents for this client. If set, the client will only receive events that they specify with intents. Defaults to <see langword="null"/>.</param>
        /// <param name="webSocketClientFactory">Sets the factory method used to create instances of WebSocket clients. Use <see cref="WebSocketClient.CreateNew(IWebProxy)"/> and equivalents on other implementations to switch out client implementations. Defaults to <see cref="WebSocketClient.CreateNew(IWebProxy)"/></param>
        /// <param name="udpClientFactory">Sets the factory method used to create instances of UDP clients. Use <see cref="DspUdpClient.CreateNew"/> and equivalents on other implementations to switch out client implementations. Defaults to <see cref="DspUdpClient.CreateNew"/>.</param>
        /// <param name="loggerFactory">Sets the logger implementation to use. To create your own logger, implement the <see cref="ILoggerFactory"/> instance. Defaults to built-in implementation.</param>
        public void DiscordSetup(string token, TokenType tokenType = TokenType.Bot, LogLevel?minimumLogLevel = null, bool logLevelDebugOnDebugging = true,
                                 bool useRelativeRateLimit         = true, string logTimestampFormat = null, int largeThreshold                     = 1000, bool autoReconnect = false,
                                 int shardId                               = 0, int shardCount = 1, GatewayCompressionLevel gatewayCompressionLevel = GatewayCompressionLevel.Stream,
                                 int messageCacheSize                      = 1024, IWebProxy webProxy = null, TimeSpan?httpTimeout                  = null, bool reconnectIndefinitely = false,
                                 DiscordIntents discordIntents             = DiscordIntents.All, WebSocketClientFactoryDelegate webSocketClientFactory = null,
                                 UdpClientFactoryDelegate udpClientFactory = null, ILoggerFactory loggerFactory = null)

        {
            var discordConfiguration = new DiscordConfiguration();

            if (udpClientFactory != null)
            {
                discordConfiguration.UdpClientFactory = udpClientFactory;
            }

            discordConfiguration.Token     = token;
            discordConfiguration.TokenType = tokenType;

#if DEBUG
            discordConfiguration.MinimumLogLevel = logLevelDebugOnDebugging ? LogLevel.Debug : minimumLogLevel ?? LogLevel.Information; // (Debugger.IsAttached ? LogLevel.Debug : LogLevel.Information) : (minimumLogLevel ?? LogLevel.Information);
#else
            discordConfiguration.MinimumLogLevel = LogLevel.Information;
#endif
            discordConfiguration.UseRelativeRatelimit = useRelativeRateLimit;
            discordConfiguration.LogTimestampFormat   = _logTimestampFormat = logTimestampFormat.IsNullOrEmptyOrWhiteSpace() ? $"{this._dateTimeFormatInfo.ShortDatePattern} " +
                                                                              $"{this._dateTimeFormatInfo.ShortTimePattern}" :
                                                                              logTimestampFormat;
            discordConfiguration.LargeThreshold = largeThreshold;
            discordConfiguration.AutoReconnect  = autoReconnect;
            if (!autoReconnect)
            {
                _baseConfiguration = new TarsBaseConfiguration();
            }

            discordConfiguration.ShardId    = shardId;
            discordConfiguration.ShardCount = shardCount;
            discordConfiguration.GatewayCompressionLevel = gatewayCompressionLevel;
            discordConfiguration.MessageCacheSize        = messageCacheSize;
            discordConfiguration.Proxy                  = webProxy;
            discordConfiguration.HttpTimeout            = httpTimeout ?? TimeSpan.FromSeconds(10);
            discordConfiguration.ReconnectIndefinitely  = reconnectIndefinitely;
            discordConfiguration.Intents                = discordIntents;
            discordConfiguration.WebSocketClientFactory = webSocketClientFactory ?? WebSocketClient.CreateNew;
            discordConfiguration.LoggerFactory          = loggerFactory;

            _discordClient = new DiscordClient(discordConfiguration);
        }