This commit is contained in:
Stanley Dimant
2025-01-05 02:27:43 +01:00
parent f047435ad9
commit 9760c11e42
2 changed files with 24 additions and 7 deletions

View File

@@ -265,31 +265,42 @@ internal class DiscordBot : IHostedService
int processedUsers = 0; int processedUsers = 0;
int addedRoles = 0; int addedRoles = 0;
int kickedUsers = 0; int kickedUsers = 0;
int totalRoles = 0;
int toRemoveUsers = 0;
int freshUsers = 0;
await _botServices.LogToChannel($"Starting to process registered users: Adding Role {registrationRole.Name}. Kick Stale Unregistered: {kickUnregistered}.").ConfigureAwait(false); 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)) await foreach (var userList in guild.GetUsersAsync(new RequestOptions { CancelToken = token }).ConfigureAwait(false))
{ {
_logger.LogInformation("Processing chunk of {count} users, total processed: {proc}, roles added: {added}, users kicked: {kicked}", _logger.LogInformation("Processing chunk of {count} users, total processed: {proc}, total roles: {total}, roles added: {added}, users kicked: {kicked}, users plan to kick: {planToKick}, fresh user: {fresh}",
userList.Count, processedUsers, addedRoles, kickedUsers); userList.Count, processedUsers, addedRoles, kickedUsers, totalRoles, toRemoveUsers, freshUsers);
foreach (var user in userList) foreach (var user in userList)
{ {
if (registeredUsers.Contains(user.Id)) if (registeredUsers.Contains(user.Id))
{ {
bool roleAdded = await _botServices.AddRegisteredRoleAsync(user, registrationRole).ConfigureAwait(false); bool roleAdded = await _botServices.AddRegisteredRoleAsync(user, registrationRole).ConfigureAwait(false);
if (roleAdded) addedRoles++; if (roleAdded) addedRoles++;
else totalRoles++;
} }
else else
{
if (kickUnregistered)
{ {
if ((executionStartTime - user.JoinedAt.Value).TotalDays > 7) if ((executionStartTime - user.JoinedAt.Value).TotalDays > 7)
{
if (kickUnregistered)
{ {
await _botServices.KickUserAsync(user).ConfigureAwait(false); await _botServices.KickUserAsync(user).ConfigureAwait(false);
kickedUsers++; kickedUsers++;
} }
else
{
toRemoveUsers++;
}
}
else
{
freshUsers++;
} }
} }
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();

View File

@@ -3,6 +3,7 @@ using Discord;
using MareSynchronosShared.Utils; using MareSynchronosShared.Utils;
using MareSynchronosShared.Utils.Configuration; using MareSynchronosShared.Utils.Configuration;
using Discord.WebSocket; using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
namespace MareSynchronosServices.Discord; namespace MareSynchronosServices.Discord;
@@ -89,7 +90,8 @@ public partial class MareWizardModule
var maxGroupsByUser = _mareClientConfigurationService.GetValueOrDefault(nameof(ServerConfiguration.MaxGroupUserCount), 3); var maxGroupsByUser = _mareClientConfigurationService.GetValueOrDefault(nameof(ServerConfiguration.MaxGroupUserCount), 3);
using var db = await GetDbContext().ConfigureAwait(false); using var db = await GetDbContext().ConfigureAwait(false);
var user = db.Users.Single(u => u.UID == uid); var user = await db.Users.SingleAsync(u => u.UID == uid).ConfigureAwait(false);
var lodestone = await db.Users.SingleOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false);
await SharedDbFunctions.PurgeUser(_logger, user, db, maxGroupsByUser).ConfigureAwait(false); await SharedDbFunctions.PurgeUser(_logger, user, db, maxGroupsByUser).ConfigureAwait(false);
EmbedBuilder eb = new(); EmbedBuilder eb = new();
@@ -102,9 +104,13 @@ public partial class MareWizardModule
await _botServices.LogToChannel($"{Context.User.Mention} DELETE SUCCESS: {uid}").ConfigureAwait(false); await _botServices.LogToChannel($"{Context.User.Mention} DELETE SUCCESS: {uid}").ConfigureAwait(false);
// only remove role if deleted uid has lodestone attached (== primary uid)
if (lodestone != null)
{
await _botServices.RemoveRegisteredRoleAsync(Context.Interaction.User).ConfigureAwait(false); await _botServices.RemoveRegisteredRoleAsync(Context.Interaction.User).ConfigureAwait(false);
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error handling modal delete account confirm"); _logger.LogError(ex, "Error handling modal delete account confirm");