Various fixes

- Fixed roles still not applying in certain occasions
- Suppressed several error messages during role updates
This commit is contained in:
Noi 2022-03-22 12:33:33 -07:00
parent 014b5a601d
commit f4daa63bc5

View file

@ -37,7 +37,9 @@ class BirthdayRoleUpdate : BackgroundService {
try { try {
// Verify that role settings and permissions are usable // Verify that role settings and permissions are usable
SocketRole? role = guild.GetRole((ulong)(settings.RoleId ?? 0)); SocketRole? role = guild.GetRole((ulong)(settings.RoleId ?? 0));
if (role == null || !guild.CurrentUser.GuildPermissions.ManageRoles || role.Position >= guild.CurrentUser.Hierarchy) return; if (role == null
|| !guild.CurrentUser.GuildPermissions.ManageRoles
|| role.Position >= guild.CurrentUser.Hierarchy) continue;
// Load up user configs and begin processing birthdays // Load up user configs and begin processing birthdays
await db.Entry(settings).Collection(t => t.UserEntries).LoadAsync(CancellationToken.None); await db.Entry(settings).Collection(t => t.UserEntries).LoadAsync(CancellationToken.None);
@ -119,10 +121,11 @@ class BirthdayRoleUpdate : BackgroundService {
/// <returns> /// <returns>
/// List of users who had the birthday role applied, used to announce. /// List of users who had the birthday role applied, used to announce.
/// </returns> /// </returns>
private async Task<IEnumerable<SocketGuildUser>> UpdateGuildBirthdayRoles(SocketGuild g, SocketRole r, HashSet<ulong> toApply) { private static async Task<IEnumerable<SocketGuildUser>> UpdateGuildBirthdayRoles(SocketGuild g, SocketRole r, HashSet<ulong> toApply) {
var additions = new List<SocketGuildUser>();
try {
var removals = new List<SocketGuildUser>(); // TODO check if roles can be removed in-place instead of building a list first var removals = new List<SocketGuildUser>(); // TODO check if roles can be removed in-place instead of building a list first
var no_ops = new HashSet<ulong>(); var no_ops = new HashSet<ulong>();
var additions = new List<SocketGuildUser>();
// Scan role for members no longer needing it // Scan role for members no longer needing it
foreach (var user in r.Members) { foreach (var user in r.Members) {
@ -136,15 +139,15 @@ class BirthdayRoleUpdate : BackgroundService {
foreach (var target in toApply) { foreach (var target in toApply) {
if (no_ops.Contains(target)) continue; if (no_ops.Contains(target)) continue;
var user = g.GetUser(target); var user = g.GetUser(target);
if (user == null) { if (user == null) continue; // User existing in database but not in guild
Log($"Encountered null user while processing guild {g.Id}. User: {target}.");
continue;
}
await user.AddRoleAsync(r); await user.AddRoleAsync(r);
additions.Add(user); additions.Add(user);
} }
} catch (Discord.Net.HttpException ex)
when (ex.DiscordCode is DiscordErrorCode.MissingPermissions or DiscordErrorCode.InsufficientPermissions) {
// Encountered access and/or permission issues despite earlier checks. Quit the loop here.
}
return additions; return additions;
} }