From a554d751b4a22774cd6f8b612296cc5a615d659a Mon Sep 17 00:00:00 2001 From: rootdarkarchon Date: Sat, 19 Nov 2022 13:24:20 +0100 Subject: [PATCH] fix service provider dispoal --- .../Discord/DiscordBotServices.cs | 9 ++++++--- .../MareSynchronosServices/Discord/MareModule.cs | 14 +++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs index 59ec3d3..5f5606f 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs @@ -11,21 +11,24 @@ namespace MareSynchronosServices.Discord; public class DiscordBotServices { - public readonly ConcurrentQueue> verificationQueue = new(); + public readonly ConcurrentQueue>> verificationQueue = new(); public ConcurrentDictionary LastVanityChange = new(); public ConcurrentDictionary LastVanityGidChange = new(); public ConcurrentDictionary DiscordLodestoneMapping = new(); public ConcurrentDictionary DiscordRelinkLodestoneMapping = new(); public readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" }; + private readonly IServiceProvider _serviceProvider; + public IConfiguration Configuration { get; init; } public ILogger Logger { get; init; } public MareMetrics Metrics { get; init; } public Random Random { get; init; } private CancellationTokenSource? verificationTaskCts; - public DiscordBotServices(IConfiguration configuration, ILogger logger, MareMetrics metrics) + public DiscordBotServices(IConfiguration configuration, IServiceProvider serviceProvider, ILogger logger, MareMetrics metrics) { Configuration = configuration.GetRequiredSection("MareSynchronos"); + _serviceProvider = serviceProvider; Logger = logger; Metrics = metrics; Random = new(); @@ -50,7 +53,7 @@ public class DiscordBotServices { try { - queueitem.Value.Invoke(); + queueitem.Value.Invoke(_serviceProvider); Logger.LogInformation("Sent login information to user"); } diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs index 6dd326d..8ba4b8c 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs @@ -97,7 +97,7 @@ public class MareModule : InteractionModuleBase else { await DeferAsync(ephemeral: true).ConfigureAwait(false); - _botServices.verificationQueue.Enqueue(new KeyValuePair(Context.User.Id, async () => await HandleVerifyAsync((SocketSlashCommand)Context.Interaction))); + _botServices.verificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, async (sp) => await HandleVerifyAsync((SocketSlashCommand)Context.Interaction, sp))); } } @@ -120,7 +120,7 @@ public class MareModule : InteractionModuleBase else { await DeferAsync(ephemeral: true).ConfigureAwait(false); - _botServices.verificationQueue.Enqueue(new KeyValuePair(Context.User.Id, async () => await HandleVerifyRelinkAsync((SocketSlashCommand)Context.Interaction))); + _botServices.verificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, async (sp) => await HandleVerifyRelinkAsync((SocketSlashCommand)Context.Interaction, sp))); } } @@ -608,11 +608,11 @@ public class MareModule : InteractionModuleBase } } - private async Task HandleVerifyRelinkAsync(SocketSlashCommand cmd) + private async Task HandleVerifyRelinkAsync(SocketSlashCommand cmd, IServiceProvider serviceProvider) { var embedBuilder = new EmbedBuilder(); - using var scope = _services.CreateScope(); + using var scope = serviceProvider.CreateScope(); var req = new HttpClient(); using var db = scope.ServiceProvider.GetService(); @@ -674,7 +674,7 @@ public class MareModule : InteractionModuleBase { embedBuilder.WithTitle("Your auth has expired or something else went wrong"); embedBuilder.WithDescription("Start again with **/relink**"); - _botServices.DiscordLodestoneMapping.TryRemove(cmd.User.Id, out _); + _botServices.DiscordRelinkLodestoneMapping.TryRemove(cmd.User.Id, out _); } var dataEmbed = embedBuilder.Build(); @@ -682,11 +682,11 @@ public class MareModule : InteractionModuleBase await cmd.FollowupAsync(embed: dataEmbed, ephemeral: true).ConfigureAwait(false); } - private async Task HandleVerifyAsync(SocketSlashCommand cmd) + private async Task HandleVerifyAsync(SocketSlashCommand cmd, IServiceProvider serviceProvider) { var embedBuilder = new EmbedBuilder(); - using var scope = _services.CreateScope(); + using var scope = serviceProvider.CreateScope(); var req = new HttpClient(); using var db = scope.ServiceProvider.GetService();