minor adjustments

This commit is contained in:
Stanley Dimant
2022-08-03 19:16:01 +02:00
parent edd91d19f6
commit d5b7dd69e8
2 changed files with 39 additions and 24 deletions

View File

@@ -19,7 +19,8 @@ using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MareSynchronosServer.Discord { namespace MareSynchronosServer.Discord
{
public class DiscordBot : IHostedService public class DiscordBot : IHostedService
{ {
private readonly IServiceProvider services; private readonly IServiceProvider services;
@@ -31,7 +32,6 @@ namespace MareSynchronosServer.Discord {
ConcurrentDictionary<ulong, string> DiscordLodestoneMapping = new(); ConcurrentDictionary<ulong, string> DiscordLodestoneMapping = new();
private Timer _timer; private Timer _timer;
private CancellationTokenSource verificationTaskCts; private CancellationTokenSource verificationTaskCts;
private Task verificationTaskWorker;
private readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" }; private readonly string[] LodestoneServers = new[] { "eu", "na", "jp", "fr", "de" };
private readonly ConcurrentQueue<SocketSlashCommand> verificationQueue = new(); private readonly ConcurrentQueue<SocketSlashCommand> verificationQueue = new();
@@ -57,11 +57,14 @@ namespace MareSynchronosServer.Discord {
} }
private async Task DiscordClient_SlashCommandExecuted(SocketSlashCommand arg) private async Task DiscordClient_SlashCommandExecuted(SocketSlashCommand arg)
{ {
await semaphore.WaitAsync(); await semaphore.WaitAsync();
try { try
if (arg.Data.Name == "register") { {
if (arg.Data.Options.FirstOrDefault(f => f.Name == "overwrite_old_account") != null) { if (arg.Data.Name == "register")
{
if (arg.Data.Options.FirstOrDefault(f => f.Name == "overwrite_old_account") != null)
{
await DeletePreviousUserAccount(arg.User.Id); await DeletePreviousUserAccount(arg.User.Id);
} }
@@ -70,27 +73,37 @@ namespace MareSynchronosServer.Discord {
modal.WithCustomId("register_modal"); modal.WithCustomId("register_modal");
modal.AddTextInput("Enter the Lodestone URL of your Character", "lodestoneurl", TextInputStyle.Short, "https://*.finalfantasyxiv.com/lodestone/character/<CHARACTERID>/", required: true); modal.AddTextInput("Enter the Lodestone URL of your Character", "lodestoneurl", TextInputStyle.Short, "https://*.finalfantasyxiv.com/lodestone/character/<CHARACTERID>/", required: true);
await arg.RespondWithModalAsync(modal.Build()); await arg.RespondWithModalAsync(modal.Build());
} else if (arg.Data.Name == "verify") { }
else if (arg.Data.Name == "verify")
{
EmbedBuilder eb = new(); EmbedBuilder eb = new();
if (verificationQueue.Any(u => u.User.Id == arg.User.Id)) { if (verificationQueue.Any(u => u.User.Id == arg.User.Id))
{
eb.WithTitle("Already queued for verfication"); eb.WithTitle("Already queued for verfication");
eb.WithDescription("You are already queued for verification. Please wait."); eb.WithDescription("You are already queued for verification. Please wait.");
await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true); await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true);
} else if (!DiscordLodestoneMapping.ContainsKey(arg.User.Id)) { }
else if (!DiscordLodestoneMapping.ContainsKey(arg.User.Id))
{
eb.WithTitle("Cannot verify registration"); eb.WithTitle("Cannot verify registration");
eb.WithDescription("You need to **/register** first before you can **/verify**"); eb.WithDescription("You need to **/register** first before you can **/verify**");
await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true); await arg.RespondAsync(embeds: new[] { eb.Build() }, ephemeral: true);
} else { }
else
{
await arg.DeferAsync(ephemeral: true); await arg.DeferAsync(ephemeral: true);
verificationQueue.Enqueue(arg); verificationQueue.Enqueue(arg);
} }
} else { }
else
{
await arg.RespondAsync("idk what you did to get here to start, just follow the instructions as provided.", ephemeral: true); await arg.RespondAsync("idk what you did to get here to start, just follow the instructions as provided.", ephemeral: true);
} }
} finally { }
finally
{
semaphore.Release(); semaphore.Release();
} }
} }
private async Task DeletePreviousUserAccount(ulong id) private async Task DeletePreviousUserAccount(ulong id)
@@ -258,6 +271,8 @@ namespace MareSynchronosServer.Discord {
+ Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine
+ $"**{lodestoneAuth}**" + $"**{lodestoneAuth}**"
+ Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine
+ $"**! THIS IS NOT THE KEY YOU HAVE TO ENTER IN MARE !**"
+ Environment.NewLine + Environment.NewLine
+ "Once added and saved, use command **/verify** to finish registration and receive a secret key to use for Mare Synchronos." + "Once added and saved, use command **/verify** to finish registration and receive a secret key to use for Mare Synchronos."
+ Environment.NewLine + Environment.NewLine
+ "You can delete the entry from your profile after verification." + "You can delete the entry from your profile after verification."
@@ -272,7 +287,7 @@ namespace MareSynchronosServer.Discord {
private async Task<string> GenerateLodestoneAuth(ulong discordid, string hashedLodestoneId, MareDbContext dbContext) private async Task<string> GenerateLodestoneAuth(ulong discordid, string hashedLodestoneId, MareDbContext dbContext)
{ {
var auth = MareHub.GenerateRandomString(64); var auth = MareHub.GenerateRandomString(32);
LodeStoneAuth lsAuth = new LodeStoneAuth() LodeStoneAuth lsAuth = new LodeStoneAuth()
{ {
DiscordId = discordid, DiscordId = discordid,
@@ -353,7 +368,7 @@ namespace MareSynchronosServer.Discord {
_timer = new Timer(UpdateStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(15)); _timer = new Timer(UpdateStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(15));
verificationTaskWorker = ProcessQueueWork(); ProcessQueueWork();
} }
} }
@@ -369,15 +384,20 @@ namespace MareSynchronosServer.Discord {
private async Task ProcessQueueWork() private async Task ProcessQueueWork()
{ {
verificationTaskCts = new CancellationTokenSource(); verificationTaskCts = new CancellationTokenSource();
while(!verificationTaskCts.IsCancellationRequested) { while (!verificationTaskCts.IsCancellationRequested)
{
if (verificationQueue.TryDequeue(out var queueitem)) { if (verificationQueue.TryDequeue(out var queueitem))
try { {
try
{
var dataEmbed = await HandleVerifyAsync(queueitem.User.Id); var dataEmbed = await HandleVerifyAsync(queueitem.User.Id);
await queueitem.FollowupAsync(embed: dataEmbed, ephemeral: true); await queueitem.FollowupAsync(embed: dataEmbed, ephemeral: true);
logger.LogInformation("Sent login information to user"); logger.LogInformation("Sent login information to user");
} catch(Exception e) { }
catch (Exception e)
{
logger.LogError(e.Message); logger.LogError(e.Message);
} }

View File

@@ -16,7 +16,6 @@ using Microsoft.Extensions.Logging;
namespace MareSynchronosServer.Hubs namespace MareSynchronosServer.Hubs
{ {
[AllowAnonymous]
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)] [Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
public partial class MareHub : Hub public partial class MareHub : Hub
{ {
@@ -33,7 +32,6 @@ namespace MareSynchronosServer.Hubs
_dbContext = mareDbContext; _dbContext = mareDbContext;
} }
[AllowAnonymous]
[HubMethodName(Api.InvokeHeartbeat)] [HubMethodName(Api.InvokeHeartbeat)]
public async Task<ConnectionDto> Heartbeat(string? characterIdentification) public async Task<ConnectionDto> Heartbeat(string? characterIdentification)
{ {
@@ -81,13 +79,11 @@ namespace MareSynchronosServer.Hubs
} }
[HubMethodName(Api.InvokeGetSystemInfo)] [HubMethodName(Api.InvokeGetSystemInfo)]
[AllowAnonymous]
public async Task<SystemInfoDto> GetSystemInfo() public async Task<SystemInfoDto> GetSystemInfo()
{ {
return _systemInfoService.SystemInfoDto; return _systemInfoService.SystemInfoDto;
} }
[AllowAnonymous]
public override Task OnConnectedAsync() public override Task OnConnectedAsync()
{ {
var feature = Context.Features.Get<IHttpConnectionFeature>(); var feature = Context.Features.Get<IHttpConnectionFeature>();
@@ -96,7 +92,6 @@ namespace MareSynchronosServer.Hubs
return base.OnConnectedAsync(); return base.OnConnectedAsync();
} }
[AllowAnonymous]
public override async Task OnDisconnectedAsync(Exception exception) public override async Task OnDisconnectedAsync(Exception exception)
{ {
MareMetrics.Connections.Dec(); MareMetrics.Connections.Dec();