mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-21 21:54:36 +00:00
Extremely minor refactoring
This commit is contained in:
parent
b87602f8f6
commit
1aaca8545e
1 changed files with 20 additions and 20 deletions
|
@ -15,8 +15,8 @@ class BirthdayRoleUpdate : BackgroundService {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override async Task OnTick(int tickCount, CancellationToken token) {
|
public override async Task OnTick(int tickCount, CancellationToken token) {
|
||||||
try {
|
try {
|
||||||
await ConcurrentSemaphore.WaitAsync(token);
|
await ConcurrentSemaphore.WaitAsync(token).ConfigureAwait(false);
|
||||||
await ProcessBirthdaysAsync(token);
|
await ProcessBirthdaysAsync(token).ConfigureAwait(false);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
ConcurrentSemaphore.Release();
|
ConcurrentSemaphore.Release();
|
||||||
|
@ -25,7 +25,7 @@ class BirthdayRoleUpdate : BackgroundService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ProcessBirthdaysAsync(CancellationToken token) {
|
private async Task ProcessBirthdaysAsync(CancellationToken token) {
|
||||||
// For database efficiency, fetch all database information at once before proceeding
|
// For database efficiency, fetch all pertinent 'global' database information at once before proceeding
|
||||||
using var db = new BotDatabaseContext();
|
using var db = new BotDatabaseContext();
|
||||||
var shardGuilds = Shard.DiscordClient.Guilds.Select(g => g.Id).ToHashSet();
|
var shardGuilds = Shard.DiscordClient.Guilds.Select(g => g.Id).ToHashSet();
|
||||||
var presentGuildSettings = db.GuildConfigurations.Where(s => shardGuilds.Contains(s.GuildId));
|
var presentGuildSettings = db.GuildConfigurations.Where(s => shardGuilds.Contains(s.GuildId));
|
||||||
|
@ -39,35 +39,35 @@ class BirthdayRoleUpdate : BackgroundService {
|
||||||
// Check task cancellation here. Processing during a single guild is never interrupted.
|
// Check task cancellation here. Processing during a single guild is never interrupted.
|
||||||
if (token.IsCancellationRequested) throw new TaskCanceledException();
|
if (token.IsCancellationRequested) throw new TaskCanceledException();
|
||||||
|
|
||||||
if (Shard.DiscordClient.ConnectionState != ConnectionState.Connected) {
|
// Stop if we've disconnected.
|
||||||
Log("Client is not connected. Stopping early.");
|
if (Shard.DiscordClient.ConnectionState != ConnectionState.Connected) break;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Verify that role settings and permissions are usable
|
// Verify that role settings and permissions are usable
|
||||||
SocketRole? role = guild.GetRole((ulong)(settings.BirthdayRole ?? 0));
|
SocketRole? role = guild.GetRole(settings.BirthdayRole ?? 0);
|
||||||
if (role == null
|
if (role == null) continue; // Role not set.
|
||||||
|| !guild.CurrentUser.GuildPermissions.ManageRoles
|
if (!guild.CurrentUser.GuildPermissions.ManageRoles || role.Position >= guild.CurrentUser.Hierarchy) {
|
||||||
|| role.Position >= guild.CurrentUser.Hierarchy) continue;
|
// Quit this guild if insufficient role permissions.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (role.IsEveryone || role.IsManaged) {
|
if (role.IsEveryone || role.IsManaged) {
|
||||||
// Invalid role was configured. Clear the setting and quit.
|
// Invalid role was configured. Clear the setting and quit.
|
||||||
settings.BirthdayRole = null;
|
settings.BirthdayRole = null;
|
||||||
db.Update(settings);
|
db.Update(settings);
|
||||||
await db.SaveChangesAsync(CancellationToken.None);
|
await db.SaveChangesAsync(CancellationToken.None).ConfigureAwait(false);
|
||||||
continue;
|
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).ConfigureAwait(false);
|
||||||
var birthdays = GetGuildCurrentBirthdays(settings.UserEntries, settings.GuildTimeZone);
|
var birthdays = GetGuildCurrentBirthdays(settings.UserEntries, settings.GuildTimeZone);
|
||||||
|
|
||||||
// Add or remove roles as appropriate
|
// Add or remove roles as appropriate
|
||||||
var announcementList = await UpdateGuildBirthdayRoles(guild, role, birthdays);
|
var announcementList = await UpdateGuildBirthdayRoles(guild, role, birthdays).ConfigureAwait(false);
|
||||||
|
|
||||||
// Process birthday announcement
|
// Process birthday announcement
|
||||||
if (announcementList.Any()) {
|
if (announcementList.Any()) {
|
||||||
await AnnounceBirthdaysAsync(settings, guild, announcementList);
|
await AnnounceBirthdaysAsync(settings, guild, announcementList).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
// Catch all exceptions per-guild but continue processing, throw at end.
|
// Catch all exceptions per-guild but continue processing, throw at end.
|
||||||
|
@ -93,9 +93,9 @@ class BirthdayRoleUpdate : BackgroundService {
|
||||||
var checkNow = SystemClock.Instance.GetCurrentInstant().InZone(tz);
|
var checkNow = SystemClock.Instance.GetCurrentInstant().InZone(tz);
|
||||||
// Special case: If user's birthday is 29-Feb and it's currently not a leap year, check against 1-Mar
|
// Special case: If user's birthday is 29-Feb and it's currently not a leap year, check against 1-Mar
|
||||||
if (!DateTime.IsLeapYear(checkNow.Year) && record.BirthMonth == 2 && record.BirthDay == 29) {
|
if (!DateTime.IsLeapYear(checkNow.Year) && record.BirthMonth == 2 && record.BirthDay == 29) {
|
||||||
if (checkNow.Month == 3 && checkNow.Day == 1) birthdayUsers.Add((ulong)record.UserId);
|
if (checkNow.Month == 3 && checkNow.Day == 1) birthdayUsers.Add(record.UserId);
|
||||||
} else if (record.BirthMonth == checkNow.Month && record.BirthDay== checkNow.Day) {
|
} else if (record.BirthMonth == checkNow.Month && record.BirthDay== checkNow.Day) {
|
||||||
birthdayUsers.Add((ulong)record.UserId);
|
birthdayUsers.Add(record.UserId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,14 +120,14 @@ class BirthdayRoleUpdate : BackgroundService {
|
||||||
else no_ops.Add(user.Id);
|
else no_ops.Add(user.Id);
|
||||||
}
|
}
|
||||||
foreach (var user in removals) {
|
foreach (var user in removals) {
|
||||||
await user.RemoveRoleAsync(r);
|
await user.RemoveRoleAsync(r).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) continue; // User existing in database but not in guild
|
if (user == null) continue; // User existing in database but not in guild
|
||||||
await user.AddRoleAsync(r);
|
await user.AddRoleAsync(r).ConfigureAwait(false);
|
||||||
additions.Add(user);
|
additions.Add(user);
|
||||||
}
|
}
|
||||||
} catch (Discord.Net.HttpException ex)
|
} catch (Discord.Net.HttpException ex)
|
||||||
|
@ -144,7 +144,7 @@ class BirthdayRoleUpdate : BackgroundService {
|
||||||
/// Attempts to send an announcement message.
|
/// Attempts to send an announcement message.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static async Task AnnounceBirthdaysAsync(GuildConfig settings, SocketGuild g, IEnumerable<SocketGuildUser> names) {
|
internal static async Task AnnounceBirthdaysAsync(GuildConfig settings, SocketGuild g, IEnumerable<SocketGuildUser> names) {
|
||||||
var c = g.GetTextChannel((ulong)(settings.AnnouncementChannel ?? 0));
|
var c = g.GetTextChannel(settings.AnnouncementChannel ?? 0);
|
||||||
if (c == null) return;
|
if (c == null) return;
|
||||||
if (!c.Guild.CurrentUser.GetPermissions(c).SendMessages) return;
|
if (!c.Guild.CurrentUser.GetPermissions(c).SendMessages) return;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue