mirror of
https://github.com/Caraxi/mare.server.git
synced 2026-02-17 05:37:41 +01:00
add registered role to bot
This commit is contained in:
parent
600bd1893e
commit
86ae9d40e0
6 changed files with 158 additions and 15 deletions
|
|
@ -22,7 +22,7 @@ internal class DiscordBot : IHostedService
|
|||
private readonly IServiceProvider _services;
|
||||
private InteractionService _interactionModule;
|
||||
private readonly CancellationTokenSource? _processReportQueueCts;
|
||||
private CancellationTokenSource? _updateStatusCts;
|
||||
private CancellationTokenSource? _clientConnectedCts;
|
||||
|
||||
public DiscordBot(DiscordBotServices botServices, IServiceProvider services, IConfigurationService<ServicesConfiguration> configuration,
|
||||
IDbContextFactory<MareDbContext> dbContextFactory,
|
||||
|
|
@ -36,7 +36,8 @@ internal class DiscordBot : IHostedService
|
|||
_connectionMultiplexer = connectionMultiplexer;
|
||||
_discordClient = new(new DiscordSocketConfig()
|
||||
{
|
||||
DefaultRetryMode = RetryMode.AlwaysRetry
|
||||
DefaultRetryMode = RetryMode.AlwaysRetry,
|
||||
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.GuildMembers
|
||||
});
|
||||
|
||||
_discordClient.Log += Log;
|
||||
|
|
@ -76,7 +77,7 @@ internal class DiscordBot : IHostedService
|
|||
{
|
||||
await _botServices.Stop().ConfigureAwait(false);
|
||||
_processReportQueueCts?.Cancel();
|
||||
_updateStatusCts?.Cancel();
|
||||
_clientConnectedCts?.Cancel();
|
||||
|
||||
await _discordClient.LogoutAsync().ConfigureAwait(false);
|
||||
await _discordClient.StopAsync().ConfigureAwait(false);
|
||||
|
|
@ -88,16 +89,17 @@ internal class DiscordBot : IHostedService
|
|||
{
|
||||
var guild = (await _discordClient.Rest.GetGuildsAsync().ConfigureAwait(false)).First();
|
||||
await _interactionModule.RegisterCommandsToGuildAsync(guild.Id, true).ConfigureAwait(false);
|
||||
_updateStatusCts?.Cancel();
|
||||
_updateStatusCts?.Dispose();
|
||||
_updateStatusCts = new();
|
||||
_ = UpdateStatusAsync(_updateStatusCts.Token);
|
||||
_clientConnectedCts?.Cancel();
|
||||
_clientConnectedCts?.Dispose();
|
||||
_clientConnectedCts = new();
|
||||
_ = UpdateStatusAsync(_clientConnectedCts.Token);
|
||||
|
||||
await CreateOrUpdateModal(guild).ConfigureAwait(false);
|
||||
_botServices.UpdateGuild(guild);
|
||||
await _botServices.LogToChannel("Bot startup complete.").ConfigureAwait(false);
|
||||
_ = UpdateVanityRoles(guild, _updateStatusCts.Token);
|
||||
_ = RemoveUsersNotInVanityRole(_updateStatusCts.Token);
|
||||
_ = UpdateVanityRoles(guild, _clientConnectedCts.Token);
|
||||
_ = RemoveUsersNotInVanityRole(_clientConnectedCts.Token);
|
||||
_ = RemoveUnregisteredUsers(_clientConnectedCts.Token);
|
||||
}
|
||||
|
||||
private async Task UpdateVanityRoles(RestGuild guild, CancellationToken token)
|
||||
|
|
@ -207,10 +209,66 @@ internal class DiscordBot : IHostedService
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task RemoveUnregisteredUsers(CancellationToken token)
|
||||
{
|
||||
var guild = (await _discordClient.Rest.GetGuildsAsync().ConfigureAwait(false)).First();
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ProcessUserRoles(guild, token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _botServices.LogToChannel($"Error during user procesing: {ex.Message}").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromDays(1)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessUserRoles(RestGuild guild, CancellationToken token)
|
||||
{
|
||||
using MareDbContext dbContext = await _dbContextFactory.CreateDbContextAsync(token).ConfigureAwait(false);
|
||||
var roleId = _configurationService.GetValueOrDefault<ulong?>(nameof(ServicesConfiguration.DiscordRoleRegistered), 0);
|
||||
var kickUnregistered = _configurationService.GetValueOrDefault(nameof(ServicesConfiguration.KickNonRegisteredUsers), false);
|
||||
if (roleId == null) return;
|
||||
|
||||
var registrationRole = guild.Roles.FirstOrDefault(f => f.Id == roleId.Value);
|
||||
var registeredUsers = new HashSet<ulong>(await dbContext.LodeStoneAuth.AsNoTracking().Select(c => c.DiscordId).ToListAsync().ConfigureAwait(false));
|
||||
|
||||
var executionStartTime = DateTimeOffset.UtcNow;
|
||||
|
||||
await _botServices.LogToChannel($"Starting to process registered users: Adding Role {registrationRole.Name}. Kick Stale Unregistered: {kickUnregistered}.").ConfigureAwait(false);
|
||||
|
||||
await foreach (var userList in guild.GetUsersAsync(new RequestOptions { CancelToken = token }).ConfigureAwait(false))
|
||||
{
|
||||
_logger.LogInformation("Processing chunk of {count} users", userList.Count);
|
||||
foreach (var user in userList)
|
||||
{
|
||||
if (registeredUsers.Contains(user.Id))
|
||||
await _botServices.AddRegisteredRoleAsync(user, registrationRole).ConfigureAwait(false);
|
||||
|
||||
if (kickUnregistered)
|
||||
{
|
||||
if ((executionStartTime - user.JoinedAt.Value).TotalDays > 7)
|
||||
await _botServices.KickUserAsync(user).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
}
|
||||
}
|
||||
|
||||
await _botServices.LogToChannel("Processing registered users finished").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task RemoveUsersNotInVanityRole(CancellationToken token)
|
||||
{
|
||||
var guild = (await _discordClient.Rest.GetGuildsAsync().ConfigureAwait(false)).First();
|
||||
var appId = await _discordClient.GetApplicationInfoAsync().ConfigureAwait(false);
|
||||
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue