mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-21 21:54:36 +00:00
Various fixes
- Fixed roles still not applying in certain occasions - Suppressed several error messages during role updates
This commit is contained in:
parent
014b5a601d
commit
f4daa63bc5
1 changed files with 26 additions and 23 deletions
|
@ -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,31 +121,32 @@ 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 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 additions = new List<SocketGuildUser>();
|
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 no_ops = new HashSet<ulong>();
|
||||||
|
|
||||||
// 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) {
|
||||||
if (!toApply.Contains(user.Id)) removals.Add(user);
|
if (!toApply.Contains(user.Id)) removals.Add(user);
|
||||||
else no_ops.Add(user.Id);
|
else no_ops.Add(user.Id);
|
||||||
}
|
|
||||||
foreach (var user in removals) {
|
|
||||||
// TODO this gets hit with rate limits sometimes. figure something out.
|
|
||||||
await user.RemoveRoleAsync(r);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var target in toApply) {
|
|
||||||
if (no_ops.Contains(target)) continue;
|
|
||||||
|
|
||||||
var user = g.GetUser(target);
|
|
||||||
if (user == null) {
|
|
||||||
Log($"Encountered null user while processing guild {g.Id}. User: {target}.");
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
await user.AddRoleAsync(r);
|
foreach (var user in removals) {
|
||||||
additions.Add(user);
|
// TODO this gets hit with rate limits sometimes. figure something out.
|
||||||
|
await user.RemoveRoleAsync(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var target in toApply) {
|
||||||
|
if (no_ops.Contains(target)) continue;
|
||||||
|
var user = g.GetUser(target);
|
||||||
|
if (user == null) continue; // User existing in database but not in guild
|
||||||
|
await user.AddRoleAsync(r);
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue