set user role on join

This commit is contained in:
Stanley Dimant
2025-01-03 02:00:35 +01:00
parent 86ae9d40e0
commit 98ed5cb472

View File

@@ -7,6 +7,7 @@ using MareSynchronosShared.Models;
using MareSynchronosShared.Services; using MareSynchronosShared.Services;
using MareSynchronosShared.Utils.Configuration; using MareSynchronosShared.Utils.Configuration;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using StackExchange.Redis; using StackExchange.Redis;
namespace MareSynchronosServices.Discord; namespace MareSynchronosServices.Discord;
@@ -66,11 +67,29 @@ internal class DiscordBot : IHostedService
var ctx = new SocketInteractionContext(_discordClient, x); var ctx = new SocketInteractionContext(_discordClient, x);
await _interactionModule.ExecuteCommandAsync(ctx, _services).ConfigureAwait(false); await _interactionModule.ExecuteCommandAsync(ctx, _services).ConfigureAwait(false);
}; };
_discordClient.UserJoined += OnUserJoined;
await _botServices.Start().ConfigureAwait(false); await _botServices.Start().ConfigureAwait(false);
} }
} }
private async Task OnUserJoined(SocketGuildUser arg)
{
try
{
using MareDbContext dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false);
var alreadyRegistered = await dbContext.LodeStoneAuth.AnyAsync(u => u.DiscordId == arg.Id).ConfigureAwait(false);
if (alreadyRegistered)
{
await _botServices.AddRegisteredRoleAsync(arg).ConfigureAwait(false);
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to set user role on join");
}
}
public async Task StopAsync(CancellationToken cancellationToken) public async Task StopAsync(CancellationToken cancellationToken)
{ {
if (!string.IsNullOrEmpty(_configurationService.GetValueOrDefault(nameof(ServicesConfiguration.DiscordBotToken), string.Empty))) if (!string.IsNullOrEmpty(_configurationService.GetValueOrDefault(nameof(ServicesConfiguration.DiscordBotToken), string.Empty)))