mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-21 21:54:36 +00:00
Reduce amount of automatic processing
These changes make the bot skip over needlessly collecting information for guilds in which it is unnecessary, in most cases guilds that have invited the bot but have not yet invoked any bot command.
This commit is contained in:
parent
73fae41ac9
commit
8f84b09b21
4 changed files with 14 additions and 6 deletions
|
@ -40,6 +40,7 @@ namespace BirthdayBot.BackgroundServices
|
||||||
{
|
{
|
||||||
Log($"{exs.InnerExceptions.Count} exception(s) during bulk processing!");
|
Log($"{exs.InnerExceptions.Count} exception(s) during bulk processing!");
|
||||||
// TODO needs major improvements. output to file?
|
// TODO needs major improvements. output to file?
|
||||||
|
foreach (var iex in exs.InnerExceptions) Log(iex.Message);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -66,7 +67,9 @@ namespace BirthdayBot.BackgroundServices
|
||||||
{
|
{
|
||||||
var diag = new PGDiagnostic();
|
var diag = new PGDiagnostic();
|
||||||
|
|
||||||
var gc = await GuildConfiguration.LoadAsync(guild.Id);
|
// Load guild information - stop if there is none (bot never previously used in guild)
|
||||||
|
var gc = await GuildConfiguration.LoadAsync(guild.Id, true);
|
||||||
|
if (gc == null) return diag;
|
||||||
|
|
||||||
// Check if role settings are correct before continuing with further processing
|
// Check if role settings are correct before continuing with further processing
|
||||||
SocketRole role = null;
|
SocketRole role = null;
|
||||||
|
@ -75,6 +78,7 @@ namespace BirthdayBot.BackgroundServices
|
||||||
if (diag.RoleCheck != null) return diag;
|
if (diag.RoleCheck != null) return diag;
|
||||||
|
|
||||||
// Determine who's currently having a birthday
|
// Determine who's currently having a birthday
|
||||||
|
await guild.DownloadUsersAsync();
|
||||||
var users = await GuildUserConfiguration.LoadAllAsync(guild.Id);
|
var users = await GuildUserConfiguration.LoadAllAsync(guild.Id);
|
||||||
var tz = gc.TimeZone;
|
var tz = gc.TimeZone;
|
||||||
var birthdays = GetGuildCurrentBirthdays(users, tz);
|
var birthdays = GetGuildCurrentBirthdays(users, tz);
|
||||||
|
|
|
@ -117,7 +117,7 @@ namespace BirthdayBot
|
||||||
if (!_dispatchCommands.TryGetValue(csplit[0].Substring(CommandPrefix.Length), out CommandHandler command)) return;
|
if (!_dispatchCommands.TryGetValue(csplit[0].Substring(CommandPrefix.Length), out CommandHandler command)) return;
|
||||||
|
|
||||||
// Load guild information here
|
// Load guild information here
|
||||||
var gconf = await GuildConfiguration.LoadAsync(channel.Guild.Id);
|
var gconf = await GuildConfiguration.LoadAsync(channel.Guild.Id, false);
|
||||||
|
|
||||||
// Ban check
|
// Ban check
|
||||||
if (!gconf.IsBotModerator(author)) // skip check if user is a moderator
|
if (!gconf.IsBotModerator(author)) // skip check if user is a moderator
|
||||||
|
|
|
@ -173,7 +173,11 @@ namespace BirthdayBot.Data
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Fetches guild settings from the database. If no corresponding entry exists, it will be created.
|
/// Fetches guild settings from the database. If no corresponding entry exists, it will be created.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static async Task<GuildConfiguration> LoadAsync(ulong guildId)
|
/// <param name="nullIfUnknown">
|
||||||
|
/// If true, this method shall not create a new entry and will return null if the guild does
|
||||||
|
/// not exist in the database.
|
||||||
|
/// </param>
|
||||||
|
public static async Task<GuildConfiguration> LoadAsync(ulong guildId, bool nullIfUnknown)
|
||||||
{
|
{
|
||||||
using (var db = await Database.OpenConnectionAsync())
|
using (var db = await Database.OpenConnectionAsync())
|
||||||
{
|
{
|
||||||
|
@ -188,6 +192,7 @@ namespace BirthdayBot.Data
|
||||||
using var r = await c.ExecuteReaderAsync();
|
using var r = await c.ExecuteReaderAsync();
|
||||||
if (await r.ReadAsync()) return new GuildConfiguration(r);
|
if (await r.ReadAsync()) return new GuildConfiguration(r);
|
||||||
}
|
}
|
||||||
|
if (nullIfUnknown) return null;
|
||||||
|
|
||||||
// If we got here, no row exists. Create it with default values.
|
// If we got here, no row exists. Create it with default values.
|
||||||
using (var c = db.CreateCommand())
|
using (var c = db.CreateCommand())
|
||||||
|
@ -199,7 +204,7 @@ namespace BirthdayBot.Data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// With a new row created, try this again
|
// With a new row created, try this again
|
||||||
return await LoadAsync(guildId);
|
return await LoadAsync(guildId, nullIfUnknown);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -21,8 +21,7 @@ namespace BirthdayBot
|
||||||
|
|
||||||
var dc = new DiscordSocketConfig()
|
var dc = new DiscordSocketConfig()
|
||||||
{
|
{
|
||||||
AlwaysDownloadUsers = true,
|
DefaultRetryMode = RetryMode.RetryRatelimit,
|
||||||
DefaultRetryMode = Discord.RetryMode.RetryRatelimit,
|
|
||||||
MessageCacheSize = 0,
|
MessageCacheSize = 0,
|
||||||
TotalShards = cfg.ShardCount,
|
TotalShards = cfg.ShardCount,
|
||||||
ExclusiveBulkDelete = true
|
ExclusiveBulkDelete = true
|
||||||
|
|
Loading…
Reference in a new issue