From 35433b0d82c4084b921b90addb78f138bfb24d13 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 31 Jan 2021 15:03:11 +0100 Subject: [PATCH] Convert a few more things --- .../Exceptions/DiscordRequestException.cs | 34 +++++++++---------- .../CommandSystem/ContextChecksExt.cs | 4 +-- PluralKit.Bot/Modules.cs | 31 ++++------------- PluralKit.Bot/Utils/MiscUtils.cs | 12 +++---- 4 files changed, 31 insertions(+), 50 deletions(-) diff --git a/Myriad/Rest/Exceptions/DiscordRequestException.cs b/Myriad/Rest/Exceptions/DiscordRequestException.cs index 6570ad81..0aa94d98 100644 --- a/Myriad/Rest/Exceptions/DiscordRequestException.cs +++ b/Myriad/Rest/Exceptions/DiscordRequestException.cs @@ -6,14 +6,14 @@ namespace Myriad.Rest.Exceptions { public class DiscordRequestException: Exception { - public DiscordRequestException(HttpResponseMessage response, string requestBody, DiscordApiError? apiError) + public DiscordRequestException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) { - RequestBody = requestBody; + ResponseBody = responseBody; Response = response; ApiError = apiError; } - public string RequestBody { get; init; } = null!; + public string ResponseBody { get; init; } = null!; public HttpResponseMessage Response { get; init; } = null!; public HttpStatusCode StatusCode => Response.StatusCode; @@ -29,43 +29,43 @@ namespace Myriad.Rest.Exceptions public class NotFoundException: DiscordRequestException { - public NotFoundException(HttpResponseMessage response, string requestBody, DiscordApiError? apiError): base( - response, requestBody, apiError) { } + public NotFoundException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base( + response, responseBody, apiError) { } } public class UnauthorizedException: DiscordRequestException { - public UnauthorizedException(HttpResponseMessage response, string requestBody, DiscordApiError? apiError): base( - response, requestBody, apiError) { } + public UnauthorizedException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base( + response, responseBody, apiError) { } } public class ForbiddenException: DiscordRequestException { - public ForbiddenException(HttpResponseMessage response, string requestBody, DiscordApiError? apiError): base( - response, requestBody, apiError) { } + public ForbiddenException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base( + response, responseBody, apiError) { } } public class ConflictException: DiscordRequestException { - public ConflictException(HttpResponseMessage response, string requestBody, DiscordApiError? apiError): base( - response, requestBody, apiError) { } + public ConflictException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base( + response, responseBody, apiError) { } } public class BadRequestException: DiscordRequestException { - public BadRequestException(HttpResponseMessage response, string requestBody, DiscordApiError? apiError): base( - response, requestBody, apiError) { } + public BadRequestException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base( + response, responseBody, apiError) { } } public class TooManyRequestsException: DiscordRequestException { - public TooManyRequestsException(HttpResponseMessage response, string requestBody, DiscordApiError? apiError): - base(response, requestBody, apiError) { } + public TooManyRequestsException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): + base(response, responseBody, apiError) { } } public class UnknownDiscordRequestException: DiscordRequestException { - public UnknownDiscordRequestException(HttpResponseMessage response, string requestBody, - DiscordApiError? apiError): base(response, requestBody, apiError) { } + public UnknownDiscordRequestException(HttpResponseMessage response, string responseBody, + DiscordApiError? apiError): base(response, responseBody, apiError) { } } } \ No newline at end of file diff --git a/PluralKit.Bot/CommandSystem/ContextChecksExt.cs b/PluralKit.Bot/CommandSystem/ContextChecksExt.cs index 53ae3015..eb1d0e89 100644 --- a/PluralKit.Bot/CommandSystem/ContextChecksExt.cs +++ b/PluralKit.Bot/CommandSystem/ContextChecksExt.cs @@ -1,6 +1,4 @@ -using DSharpPlus; - -using Myriad.Types; +using Myriad.Types; using PluralKit.Core; diff --git a/PluralKit.Bot/Modules.cs b/PluralKit.Bot/Modules.cs index 7229581d..f3265e2c 100644 --- a/PluralKit.Bot/Modules.cs +++ b/PluralKit.Bot/Modules.cs @@ -3,9 +3,6 @@ using System.Net.Http; using Autofac; -using DSharpPlus; -using DSharpPlus.EventArgs; - using Myriad.Cache; using Myriad.Gateway; @@ -24,17 +21,6 @@ namespace PluralKit.Bot protected override void Load(ContainerBuilder builder) { // Clients - builder.Register(c => new DiscordConfiguration - { - Token = c.Resolve().Token, - TokenType = TokenType.Bot, - MessageCacheSize = 0, - LargeThreshold = 50, - LoggerFactory = c.Resolve() - }).AsSelf(); - builder.Register(c => new DiscordShardedClient(c.Resolve())).AsSelf().SingleInstance(); - builder.Register(c => new DiscordRestClient(c.Resolve())).AsSelf().SingleInstance(); - builder.Register(c => new GatewaySettings { Token = c.Resolve().Token, @@ -82,9 +68,7 @@ namespace PluralKit.Bot builder.RegisterType().As>(); // Event handler queue - builder.RegisterType>().AsSelf().SingleInstance(); builder.RegisterType>().AsSelf().SingleInstance(); - builder.RegisterType>().AsSelf().SingleInstance(); builder.RegisterType>().AsSelf().SingleInstance(); // Bot services @@ -104,14 +88,13 @@ namespace PluralKit.Bot // Sentry stuff builder.Register(_ => new Scope(null)).AsSelf().InstancePerLifetimeScope(); - // TODO: - // builder.RegisterType() - // .As>() - // .As>() - // .As>() - // .As>() - // .As>() - // .SingleInstance(); + builder.RegisterType() + .As>() + .As>() + .As>() + .As>() + .As>() + .SingleInstance(); // Proxy stuff builder.RegisterType().AsSelf().SingleInstance(); diff --git a/PluralKit.Bot/Utils/MiscUtils.cs b/PluralKit.Bot/Utils/MiscUtils.cs index 38a86618..3fb3b995 100644 --- a/PluralKit.Bot/Utils/MiscUtils.cs +++ b/PluralKit.Bot/Utils/MiscUtils.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Net.Sockets; using System.Threading.Tasks; -using DSharpPlus.Exceptions; +using Myriad.Rest.Exceptions; using Newtonsoft.Json; @@ -64,12 +64,12 @@ namespace PluralKit.Bot if (e is JsonReaderException jre && jre.Message == "Unexpected character encountered while parsing value: <. Path '', line 0, position 0.") return false; // And now (2020-05-12), apparently Discord returns these weird responses occasionally. Also not our problem. - if (e is BadRequestException bre && bre.WebResponse.Response.Contains("
nginx
")) return false; - if (e is NotFoundException ne && ne.WebResponse.Response.Contains("
nginx
")) return false; - if (e is UnauthorizedException ue && ue.WebResponse.Response.Contains("
nginx
")) return false; + if (e is BadRequestException bre && bre.ResponseBody.Contains("
nginx
")) return false; + if (e is NotFoundException ne && ne.ResponseBody.Contains("
nginx
")) return false; + if (e is UnauthorizedException ue && ue.ResponseBody.Contains("
nginx
")) return false; - // 500s? also not our problem :^) - if (e is ServerErrorException) return false; + // 5xxs? also not our problem :^) + if (e is UnknownDiscordRequestException udre && (int) udre.StatusCode >= 500) return false; // Webhook server errors are also *not our problem* // (this includes rate limit errors, WebhookRateLimited is a subclass)