diff --git a/DisCatSharp/Clients/BaseDiscordClient.cs b/DisCatSharp/Clients/BaseDiscordClient.cs index ec8090222e..66e083a406 100644 --- a/DisCatSharp/Clients/BaseDiscordClient.cs +++ b/DisCatSharp/Clients/BaseDiscordClient.cs @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; +using System.Net; using System.Net.Http; using System.Reflection; using System.Threading.Tasks; @@ -230,13 +231,23 @@ protected BaseDiscordClient(DiscordConfiguration config) this.InternalVoiceRegions = new(); this.VoiceRegionsLazy = new(() => new ReadOnlyDictionary(this.InternalVoiceRegions)); - this.RestClient = new(); - this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Utilities.GetUserAgent()); - this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-locale", this.Configuration.Locale); + var httphandler = new HttpClientHandler + { + UseCookies = false, + AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, + UseProxy = this.Configuration.Proxy != null, + Proxy = this.Configuration.Proxy + }; + this.RestClient = new() + { + Timeout = this.Configuration.HttpTimeout + }; + this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent()); + this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this.Configuration.Locale); if (!string.IsNullOrWhiteSpace(this.Configuration.Timezone)) - this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-timezone", this.Configuration.Timezone); + this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this.Configuration.Timezone); if (this.Configuration.Override is not null) - this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-super-properties", this.Configuration.Override); + this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this.Configuration.Override); var a = typeof(DiscordClient).GetTypeInfo().Assembly; diff --git a/DisCatSharp/Clients/DiscordOAuth2Client.cs b/DisCatSharp/Clients/DiscordOAuth2Client.cs index b4bd81fa76..89e2cd8b60 100644 --- a/DisCatSharp/Clients/DiscordOAuth2Client.cs +++ b/DisCatSharp/Clients/DiscordOAuth2Client.cs @@ -140,7 +140,7 @@ public DiscordOAuth2Client( } this.VersionHeader = $"DiscordBot (https://github.com/Aiko-IT-Systems/DisCatSharp, v{vs})"; - this.ApiClient.Rest.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", this.VersionHeader); + this.ApiClient.Rest.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, this.VersionHeader); this.OAuth2ClientErroredInternal = new("CLIENT_ERRORED", EventExecutionLimit, this.Goof); diff --git a/DisCatSharp/Clients/DiscordShardedClient.cs b/DisCatSharp/Clients/DiscordShardedClient.cs index 7f49d1aaed..0f8d30d7ab 100644 --- a/DisCatSharp/Clients/DiscordShardedClient.cs +++ b/DisCatSharp/Clients/DiscordShardedClient.cs @@ -397,13 +397,13 @@ private async Task GetGatewayInfoAsync() Timeout = this._configuration.HttpTimeout }; - http.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Utilities.GetUserAgent()); - http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", Utilities.GetFormattedToken(this._configuration)); - http.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-locale", this._configuration.Locale); + http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent()); + http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.AUTHORIZATION, Utilities.GetFormattedToken(this._configuration)); + http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this._configuration.Locale); if (!string.IsNullOrWhiteSpace(this._configuration.Timezone)) - http.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-timezone", this._configuration.Timezone); + http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this._configuration.Timezone); if (this._configuration.Override != null) - http.DefaultRequestHeaders.TryAddWithoutValidation("x-super-properties", this._configuration.Override); + http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this._configuration.Override); this.Logger.LogDebug(LoggerEvents.ShardRest, $"Obtaining gateway information from GET {Endpoints.GATEWAY}{Endpoints.BOT}..."); var resp = await http.GetAsync(url).ConfigureAwait(false); diff --git a/DisCatSharp/Net/Rest/CommonHeaders.cs b/DisCatSharp/Net/Rest/CommonHeaders.cs index 089dedbba7..52542277fc 100644 --- a/DisCatSharp/Net/Rest/CommonHeaders.cs +++ b/DisCatSharp/Net/Rest/CommonHeaders.cs @@ -63,7 +63,7 @@ public static class CommonHeaders /// /// Gets or sets the user agent header. /// - public const string USER_AGENT = "UserAgent"; + public const string USER_AGENT = "User-Agent"; /// /// Gets or sets the authorization header. diff --git a/DisCatSharp/Net/Rest/DiscordApiClient.cs b/DisCatSharp/Net/Rest/DiscordApiClient.cs index eceecefa22..79ad62e6e4 100644 --- a/DisCatSharp/Net/Rest/DiscordApiClient.cs +++ b/DisCatSharp/Net/Rest/DiscordApiClient.cs @@ -56,7 +56,13 @@ internal DiscordApiClient(DiscordOAuth2Client client, IWebProxy proxy, TimeSpan { this.OAuth2Client = client; this.Discord = null!; - this.Rest = new(proxy, timeout, useRelativeRateLimit, logger); + this.Rest = new(new DiscordConfiguration() { + Proxy = proxy, + HttpTimeout = timeout, + UseRelativeRatelimit = useRelativeRateLimit, + ApiChannel = ApiChannel.Stable, + ApiVersion = "10" + }, logger); } /// @@ -70,7 +76,13 @@ internal DiscordApiClient(IWebProxy proxy, TimeSpan timeout, bool useRelativeRat { this.Discord = null!; this.OAuth2Client = null!; - this.Rest = new(proxy, timeout, useRelativeRateLimit, logger); + this.Rest = new(new DiscordConfiguration() { + Proxy = proxy, + HttpTimeout = timeout, + UseRelativeRatelimit = useRelativeRateLimit, + ApiChannel = ApiChannel.Stable, + ApiVersion = "10" + }, logger); } /// @@ -7089,7 +7101,7 @@ internal async Task CreateInteractionRespons attachments.Add(att); fileId++; } - + if (pld.Data is not null) pld.Data.Attachments = attachments; } diff --git a/DisCatSharp/Net/Rest/RestClient.cs b/DisCatSharp/Net/Rest/RestClient.cs index 4fbe2bc953..04c6f16f81 100644 --- a/DisCatSharp/Net/Rest/RestClient.cs +++ b/DisCatSharp/Net/Rest/RestClient.cs @@ -91,7 +91,7 @@ internal sealed class RestClient : IDisposable /// /// The client. internal RestClient(BaseDiscordClient client) - : this(client.Configuration.Proxy, client.Configuration.HttpTimeout, client.Configuration.UseRelativeRatelimit, client.Logger) + : this(client.Configuration, client.Logger) { this._discord = client; @@ -109,14 +109,10 @@ internal RestClient(BaseDiscordClient client) /// Initializes a new instance of the class. /// This is for meta-clients, such as the and . /// - /// The proxy. - /// The timeout. - /// Whether to use relative ratelimit. + /// The configuration. /// The logger. internal RestClient( - IWebProxy? proxy, - TimeSpan timeout, - bool useRelativeRatelimit, + DiscordConfiguration configuration, ILogger logger ) { @@ -126,24 +122,24 @@ ILogger logger { UseCookies = false, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, - UseProxy = proxy != null, - Proxy = proxy + UseProxy = configuration.Proxy != null, + Proxy = configuration.Proxy }; this.HttpClient = new(httphandler) { - BaseAddress = new(Utilities.GetApiBaseUri(this._discord?.Configuration)), - Timeout = timeout + BaseAddress = new(Utilities.GetApiBaseUri(configuration)), + Timeout = configuration.HttpTimeout }; this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent()); if (this._discord is { Configuration: not null }) { - this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this._discord.Configuration.Locale); - if (!string.IsNullOrWhiteSpace(this._discord.Configuration.Timezone)) - this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this._discord.Configuration.Timezone); - if (!string.IsNullOrWhiteSpace(this._discord.Configuration.Override)) - this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this._discord.Configuration.Override); + this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, configuration.Locale); + if (!string.IsNullOrWhiteSpace(configuration.Timezone)) + this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, configuration.Timezone); + if (!string.IsNullOrWhiteSpace(configuration.Override)) + this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, configuration.Override); } this._routesToHashes = new(); @@ -151,7 +147,7 @@ ILogger logger this._requestQueue = new(); this._globalRateLimitEvent = new(true); - this._useResetAfter = useRelativeRatelimit; + this._useResetAfter = configuration.UseRelativeRatelimit; } ///