adapt register and relink module

This commit is contained in:
rootdarkarchon
2023-10-21 00:17:02 +02:00
parent 4e88c6e68f
commit 48ec4f0c27
4 changed files with 35 additions and 29 deletions

View File

@@ -17,16 +17,15 @@ public class DiscordBotServices
private readonly IServiceProvider _serviceProvider;
private CancellationTokenSource verificationTaskCts;
public DiscordBotServices(IServiceProvider serviceProvider, ILogger<DiscordBotServices> logger, MareMetrics metrics)
public DiscordBotServices(ILogger<DiscordBotServices> logger, MareMetrics metrics)
{
_serviceProvider = serviceProvider;
Logger = logger;
Metrics = metrics;
}
public ILogger<DiscordBotServices> Logger { get; init; }
public MareMetrics Metrics { get; init; }
public ConcurrentQueue<KeyValuePair<ulong, Action<IServiceProvider>>> VerificationQueue { get; } = new();
public ConcurrentQueue<KeyValuePair<ulong, Action<DiscordBotServices>>> 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);

View File

@@ -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<ulong, Action<IServiceProvider>>(Context.User.Id, async (sp) => await HandleVerifyAsync((SocketSlashCommand)Context.Interaction, sp)));
//_botServices.VerificationQueue.Enqueue(new KeyValuePair<ulong, Action<IServiceProvider>>(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<ulong, Action<IServiceProvider>>(Context.User.Id, async (sp) => await HandleVerifyRelinkAsync((SocketSlashCommand)Context.Interaction, sp)));
//_botServices.VerificationQueue.Enqueue(new KeyValuePair<ulong, Action<IServiceProvider>>(Context.User.Id, async (sp) => await HandleVerifyRelinkAsync((SocketSlashCommand)Context.Interaction, sp)));
}
}
catch (Exception ex)

View File

@@ -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<ulong, Action<IServiceProvider>>(Context.User.Id,
async (_) => await HandleVerifyAsync(Context.User.Id, verificationCode).ConfigureAwait(false)));
_botServices.VerificationQueue.Enqueue(new KeyValuePair<ulong, Action<DiscordBotServices>>(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

View File

@@ -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<ulong, Action<IServiceProvider>>(Context.User.Id,
async (_) => await HandleVerifyRelinkAsync(Context.User.Id, verificationCode).ConfigureAwait(false)));
_botServices.VerificationQueue.Enqueue(new KeyValuePair<ulong, Action<DiscordBotServices>>(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;
}
}
}