diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs index 69d02a8..21bbe10 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBotServices.cs @@ -17,16 +17,15 @@ public class DiscordBotServices private readonly IServiceProvider _serviceProvider; private CancellationTokenSource verificationTaskCts; - public DiscordBotServices(IServiceProvider serviceProvider, ILogger logger, MareMetrics metrics) + public DiscordBotServices(ILogger logger, MareMetrics metrics) { - _serviceProvider = serviceProvider; Logger = logger; Metrics = metrics; } public ILogger Logger { get; init; } public MareMetrics Metrics { get; init; } - public ConcurrentQueue>> VerificationQueue { get; } = new(); + public ConcurrentQueue>> VerificationQueue { get; } = new(); public Task Start() { @@ -46,16 +45,21 @@ public class DiscordBotServices while (!verificationTaskCts.IsCancellationRequested) { Logger.LogDebug("Processing Verification Queue, Entries: {entr}", VerificationQueue.Count); - if (VerificationQueue.TryDequeue(out var queueitem)) + if (VerificationQueue.TryPeek(out var queueitem)) { try { - queueitem.Value.Invoke(_serviceProvider); + queueitem.Value.Invoke(this); + Logger.LogInformation("Processed Verification for {key}", queueitem.Key); } catch (Exception e) { Logger.LogError(e, "Error during queue work"); } + finally + { + VerificationQueue.TryDequeue(out _); + } } await Task.Delay(TimeSpan.FromSeconds(2), verificationTaskCts.Token).ConfigureAwait(false); diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs index 341d386..a0e8792 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs @@ -14,6 +14,8 @@ using StackExchange.Redis; namespace MareSynchronosServices.Discord; +// todo: remove all this crap at some point + public class LodestoneModal : IModal { public string Title => "Verify with Lodestone"; @@ -168,7 +170,7 @@ public class MareModule : InteractionModuleBase else { await DeferAsync(ephemeral: true).ConfigureAwait(false); - _botServices.VerificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, async (sp) => await HandleVerifyAsync((SocketSlashCommand)Context.Interaction, sp))); + //_botServices.VerificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, async (sp) => await HandleVerifyAsync((SocketSlashCommand)Context.Interaction, sp))); } } catch (Exception ex) @@ -205,7 +207,7 @@ public class MareModule : InteractionModuleBase else { await DeferAsync(ephemeral: true).ConfigureAwait(false); - _botServices.VerificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, async (sp) => await HandleVerifyRelinkAsync((SocketSlashCommand)Context.Interaction, sp))); + //_botServices.VerificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, async (sp) => await HandleVerifyRelinkAsync((SocketSlashCommand)Context.Interaction, sp))); } } catch (Exception ex) diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Register.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Register.cs index 26cd336..a501536 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Register.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Register.cs @@ -75,8 +75,8 @@ public partial class MareWizardModule _logger.LogInformation("{method}:{userId}:{verificationcode}", nameof(ComponentRegisterVerify), Context.Interaction.User.Id, verificationCode); - _botServices.VerificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, - async (_) => await HandleVerifyAsync(Context.User.Id, verificationCode).ConfigureAwait(false))); + _botServices.VerificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, + async (service) => await HandleVerifyAsync(Context.User.Id, verificationCode, service).ConfigureAwait(false))); EmbedBuilder eb = new(); ComponentBuilder cb = new(); eb.WithColor(Color.Purple); @@ -204,15 +204,15 @@ public partial class MareWizardModule return (true, lodestoneAuth); } - private async Task HandleVerifyAsync(ulong userid, string authString) + private async Task HandleVerifyAsync(ulong userid, string authString, DiscordBotServices services) { var req = new HttpClient(); - _botServices.DiscordVerifiedUsers.Remove(userid, out _); - if (_botServices.DiscordLodestoneMapping.ContainsKey(userid)) + services.DiscordVerifiedUsers.Remove(userid, out _); + if (services.DiscordLodestoneMapping.ContainsKey(userid)) { - var randomServer = _botServices.LodestoneServers[random.Next(_botServices.LodestoneServers.Length)]; - var url = $"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{_botServices.DiscordLodestoneMapping[userid]}"; + var randomServer = services.LodestoneServers[random.Next(services.LodestoneServers.Length)]; + var url = $"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{services.DiscordLodestoneMapping[userid]}"; var response = await req.GetAsync(url).ConfigureAwait(false); _logger.LogInformation("Verifying {userid} with URL {url}", userid, url); if (response.IsSuccessStatusCode) @@ -220,14 +220,14 @@ public partial class MareWizardModule var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (content.Contains(authString)) { - _logger.LogInformation("Verified {userid} from lodestone {lodestone}", userid, _botServices.DiscordLodestoneMapping[userid]); - _botServices.DiscordVerifiedUsers[userid] = true; - _botServices.DiscordLodestoneMapping.TryRemove(userid, out _); + services.DiscordVerifiedUsers[userid] = true; + services.DiscordLodestoneMapping.TryRemove(userid, out _); + _logger.LogInformation("Verified {userid} from lodestone {lodestone}", userid, services.DiscordLodestoneMapping[userid]); } else { - _logger.LogInformation("Could not verify {userid} from lodestone {lodestone}, did not find authString: {authString}", userid, _botServices.DiscordLodestoneMapping[userid], authString); - _botServices.DiscordVerifiedUsers[userid] = false; + services.DiscordVerifiedUsers[userid] = false; + _logger.LogInformation("Could not verify {userid} from lodestone {lodestone}, did not find authString: {authString}", userid, services.DiscordLodestoneMapping[userid], authString); } } else diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Relink.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Relink.cs index ef0d0ab..4025471 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Relink.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Relink.cs @@ -70,8 +70,8 @@ public partial class MareWizardModule _logger.LogInformation("{method}:{userId}:{uid}:{verificationCode}", nameof(ComponentRelinkVerify), Context.Interaction.User.Id, uid, verificationCode); - _botServices.VerificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, - async (_) => await HandleVerifyRelinkAsync(Context.User.Id, verificationCode).ConfigureAwait(false))); + _botServices.VerificationQueue.Enqueue(new KeyValuePair>(Context.User.Id, + async (services) => await HandleVerifyRelinkAsync(Context.User.Id, verificationCode, services).ConfigureAwait(false))); EmbedBuilder eb = new(); ComponentBuilder cb = new(); eb.WithColor(Color.Purple); @@ -200,26 +200,26 @@ public partial class MareWizardModule return (true, lodestoneAuth, expectedUser.User.UID); } - private async Task HandleVerifyRelinkAsync(ulong userid, string authString) + private async Task HandleVerifyRelinkAsync(ulong userid, string authString, DiscordBotServices services) { var req = new HttpClient(); - _botServices.DiscordVerifiedUsers.Remove(userid, out _); - if (_botServices.DiscordRelinkLodestoneMapping.ContainsKey(userid)) + services.DiscordVerifiedUsers.Remove(userid, out _); + if (services.DiscordRelinkLodestoneMapping.ContainsKey(userid)) { - var randomServer = _botServices.LodestoneServers[random.Next(_botServices.LodestoneServers.Length)]; - var response = await req.GetAsync($"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{_botServices.DiscordRelinkLodestoneMapping[userid]}").ConfigureAwait(false); + var randomServer = services.LodestoneServers[random.Next(services.LodestoneServers.Length)]; + var response = await req.GetAsync($"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{services.DiscordRelinkLodestoneMapping[userid]}").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (content.Contains(authString)) { - _botServices.DiscordVerifiedUsers[userid] = true; - _botServices.DiscordRelinkLodestoneMapping.TryRemove(userid, out _); + services.DiscordVerifiedUsers[userid] = true; + services.DiscordRelinkLodestoneMapping.TryRemove(userid, out _); } else { - _botServices.DiscordVerifiedUsers[userid] = false; + services.DiscordVerifiedUsers[userid] = false; } } }