fix service provider dispoal

This commit is contained in:
rootdarkarchon
2022-11-19 13:24:20 +01:00
parent 76dded0840
commit a554d751b4
2 changed files with 13 additions and 10 deletions

View File

@@ -11,21 +11,24 @@ namespace MareSynchronosServices.Discord;
public class DiscordBotServices public class DiscordBotServices
{ {
public readonly ConcurrentQueue<KeyValuePair<ulong, Action>> verificationQueue = new(); public readonly ConcurrentQueue<KeyValuePair<ulong, Action<IServiceProvider>>> verificationQueue = new();
public ConcurrentDictionary<ulong, DateTime> LastVanityChange = new(); public ConcurrentDictionary<ulong, DateTime> LastVanityChange = new();
public ConcurrentDictionary<string, DateTime> LastVanityGidChange = new(); public ConcurrentDictionary<string, DateTime> LastVanityGidChange = new();
public ConcurrentDictionary<ulong, string> DiscordLodestoneMapping = new(); public ConcurrentDictionary<ulong, string> DiscordLodestoneMapping = new();
public ConcurrentDictionary<ulong, string> DiscordRelinkLodestoneMapping = new(); public ConcurrentDictionary<ulong, string> DiscordRelinkLodestoneMapping = new();
public readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" }; public readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" };
private readonly IServiceProvider _serviceProvider;
public IConfiguration Configuration { get; init; } public IConfiguration Configuration { get; init; }
public ILogger<DiscordBotServices> Logger { get; init; } public ILogger<DiscordBotServices> Logger { get; init; }
public MareMetrics Metrics { get; init; } public MareMetrics Metrics { get; init; }
public Random Random { get; init; } public Random Random { get; init; }
private CancellationTokenSource? verificationTaskCts; private CancellationTokenSource? verificationTaskCts;
public DiscordBotServices(IConfiguration configuration, ILogger<DiscordBotServices> logger, MareMetrics metrics) public DiscordBotServices(IConfiguration configuration, IServiceProvider serviceProvider, ILogger<DiscordBotServices> logger, MareMetrics metrics)
{ {
Configuration = configuration.GetRequiredSection("MareSynchronos"); Configuration = configuration.GetRequiredSection("MareSynchronos");
_serviceProvider = serviceProvider;
Logger = logger; Logger = logger;
Metrics = metrics; Metrics = metrics;
Random = new(); Random = new();
@@ -50,7 +53,7 @@ public class DiscordBotServices
{ {
try try
{ {
queueitem.Value.Invoke(); queueitem.Value.Invoke(_serviceProvider);
Logger.LogInformation("Sent login information to user"); Logger.LogInformation("Sent login information to user");
} }

View File

@@ -97,7 +97,7 @@ public class MareModule : InteractionModuleBase
else else
{ {
await DeferAsync(ephemeral: true).ConfigureAwait(false); await DeferAsync(ephemeral: true).ConfigureAwait(false);
_botServices.verificationQueue.Enqueue(new KeyValuePair<ulong, Action>(Context.User.Id, async () => await HandleVerifyAsync((SocketSlashCommand)Context.Interaction))); _botServices.verificationQueue.Enqueue(new KeyValuePair<ulong, Action<IServiceProvider>>(Context.User.Id, async (sp) => await HandleVerifyAsync((SocketSlashCommand)Context.Interaction, sp)));
} }
} }
@@ -120,7 +120,7 @@ public class MareModule : InteractionModuleBase
else else
{ {
await DeferAsync(ephemeral: true).ConfigureAwait(false); await DeferAsync(ephemeral: true).ConfigureAwait(false);
_botServices.verificationQueue.Enqueue(new KeyValuePair<ulong, Action>(Context.User.Id, async () => await HandleVerifyRelinkAsync((SocketSlashCommand)Context.Interaction))); _botServices.verificationQueue.Enqueue(new KeyValuePair<ulong, Action<IServiceProvider>>(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(); var embedBuilder = new EmbedBuilder();
using var scope = _services.CreateScope(); using var scope = serviceProvider.CreateScope();
var req = new HttpClient(); var req = new HttpClient();
using var db = scope.ServiceProvider.GetService<MareDbContext>(); using var db = scope.ServiceProvider.GetService<MareDbContext>();
@@ -674,7 +674,7 @@ public class MareModule : InteractionModuleBase
{ {
embedBuilder.WithTitle("Your auth has expired or something else went wrong"); embedBuilder.WithTitle("Your auth has expired or something else went wrong");
embedBuilder.WithDescription("Start again with **/relink**"); embedBuilder.WithDescription("Start again with **/relink**");
_botServices.DiscordLodestoneMapping.TryRemove(cmd.User.Id, out _); _botServices.DiscordRelinkLodestoneMapping.TryRemove(cmd.User.Id, out _);
} }
var dataEmbed = embedBuilder.Build(); var dataEmbed = embedBuilder.Build();
@@ -682,11 +682,11 @@ public class MareModule : InteractionModuleBase
await cmd.FollowupAsync(embed: dataEmbed, ephemeral: true).ConfigureAwait(false); 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(); var embedBuilder = new EmbedBuilder();
using var scope = _services.CreateScope(); using var scope = serviceProvider.CreateScope();
var req = new HttpClient(); var req = new HttpClient();
using var db = scope.ServiceProvider.GetService<MareDbContext>(); using var db = scope.ServiceProvider.GetService<MareDbContext>();