diff --git a/Configuration.cs b/Configuration.cs index a591e2a..5f799c3 100644 --- a/Configuration.cs +++ b/Configuration.cs @@ -26,6 +26,8 @@ class Configuration { public int ShardStart { get; } public int ShardAmount { get; } public int ShardTotal { get; } + + public string DatabaseConnectionString { get; } public Configuration(string[] args) { var cmdline = CmdLineOpts.Parse(args); @@ -74,7 +76,7 @@ class Configuration { }; var sqldb = ReadConfKey(jc, KeySqlDatabase, false); if (sqldb != null) csb.Database = sqldb; // Optional database setting - Database.DBConnectionString = csb.ToString(); + DatabaseConnectionString = csb.ToString(); } private static T? ReadConfKey(JObject jc, string key, [DoesNotReturnIf(true)] bool failOnEmpty) { diff --git a/Data/BotDatabaseContext.cs b/Data/BotDatabaseContext.cs index 1e54958..8d50f0b 100644 --- a/Data/BotDatabaseContext.cs +++ b/Data/BotDatabaseContext.cs @@ -3,13 +3,27 @@ namespace BirthdayBot.Data; public class BotDatabaseContext : DbContext { - public virtual DbSet BlocklistEntries { get; set; } = null!; - public virtual DbSet GuildConfigurations { get; set; } = null!; - public virtual DbSet UserEntries { get; set; } = null!; + private static string? _npgsqlConnectionString; + internal static string NpgsqlConnectionString { +#if DEBUG + get { + if (_npgsqlConnectionString != null) return _npgsqlConnectionString; + Program.Log(nameof(BotDatabaseContext), "Using hardcoded connection string!"); + return _npgsqlConnectionString ?? "Host=localhost;Username=birthdaybot;Password=bb"; + } +#else + get => _npgsqlConnectionString!; +#endif + set => _npgsqlConnectionString ??= value; + } + + public DbSet BlocklistEntries { get; set; } = null!; + public DbSet GuildConfigurations { get; set; } = null!; + public DbSet UserEntries { get; set; } = null!; protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder - .UseNpgsql("Host=localhost;Username=birthdaybot;Password=bb") // TODO use actual connection string + .UseNpgsql(NpgsqlConnectionString) .UseSnakeCaseNamingConvention(); protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/Data/Database.cs b/Data/Database.cs index 1c82ebd..689a6de 100644 --- a/Data/Database.cs +++ b/Data/Database.cs @@ -3,7 +3,10 @@ using System.Threading.Tasks; namespace BirthdayBot.Data; +[Obsolete(ObsoleteReason, error: false)] internal static class Database { + public const string ObsoleteReason = "Will be removed in favor of EF6 stuff when text commands are removed"; + public static string DBConnectionString { get; set; } /// diff --git a/Data/Extensions.cs b/Data/Extensions.cs deleted file mode 100644 index 9d29259..0000000 --- a/Data/Extensions.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace BirthdayBot.Data; - -internal static class Extensions { - /// - /// Retrieves the database-backed bot configuration for this guild. - /// - internal static async Task GetConfigAsync(this SocketGuild guild) - => await GuildConfiguration.LoadAsync(guild.Id, false); - - /// - /// Retrieves a collection of all existing user configurations for this guild. - /// - internal static async Task> GetUserConfigurationsAsync(this SocketGuild guild) - => await GuildUserConfiguration.LoadAllAsync(guild.Id); - - /// - /// Retrieves the database-backed bot configuration (birthday info) for this guild user. - /// - internal static async Task GetConfigAsync(this SocketGuildUser user) - => await GuildUserConfiguration.LoadAsync(user.Guild.Id, user.Id); -} diff --git a/Data/GuildConfiguration.cs b/Data/GuildConfiguration.cs index b29cce4..b844fb2 100644 --- a/Data/GuildConfiguration.cs +++ b/Data/GuildConfiguration.cs @@ -8,6 +8,7 @@ namespace BirthdayBot.Data; /// Represents guild-specific configuration as exists in the database. /// Updating any property requires a call to for changes to take effect. /// +[Obsolete(Database.ObsoleteReason, error: false)] class GuildConfiguration { /// /// Gets this configuration's corresponding guild ID. @@ -73,7 +74,6 @@ class GuildConfiguration { /// /// Checks if the specified user is blocked by current guild policy (block list or moderated mode). /// - [Obsolete("Block lists should be reimplemented in a more resource-efficient manner later.", false)] public async Task IsUserBlockedAsync(ulong userId) { if (IsModerated) return true; return await IsUserInBlocklistAsync(userId).ConfigureAwait(false); @@ -82,7 +82,6 @@ class GuildConfiguration { /// /// Checks if the given user exists in the block list. /// - [Obsolete("Block lists should be reimplemented in a more resource-efficient manner later.", false)] public async Task IsUserInBlocklistAsync(ulong userId) { using var db = await Database.OpenConnectionAsync().ConfigureAwait(false); using var c = db.CreateCommand(); @@ -99,7 +98,6 @@ class GuildConfiguration { /// /// Adds the specified user to the block list corresponding to this guild. /// - [Obsolete("Block lists will be reimplemented in a more practical manner later.", false)] public async Task BlockUserAsync(ulong userId) { using var db = await Database.OpenConnectionAsync().ConfigureAwait(false); using var c = db.CreateCommand(); @@ -117,7 +115,6 @@ class GuildConfiguration { /// Removes the specified user from the block list corresponding to this guild. /// /// True if a user has been removed, false if the requested user was not in this list. - [Obsolete("Block lists will be reimplemented in a more practical manner later.", false)] public async Task UnblockUserAsync(ulong userId) { using var db = await Database.OpenConnectionAsync().ConfigureAwait(false); using var c = db.CreateCommand(); @@ -134,7 +131,6 @@ class GuildConfiguration { /// Checks if the given user can be considered a bot moderator. /// Checks for either the Manage Guild permission or if the user is within a predetermined role. /// - [Obsolete("Usage should be phased out when text commands are removed. Use PreconditionAttribute from now on.", error: false)] public bool IsBotModerator(SocketGuildUser user) => user.GuildPermissions.ManageGuild || (ModeratorRole.HasValue && user.Roles.Any(r => r.Id == ModeratorRole.Value)); @@ -142,6 +138,7 @@ class GuildConfiguration { public const string BackingTable = "settings"; public const string BackingTableBans = "banned_users"; + [Obsolete("DELETE THIS", error: true)] internal static async Task DatabaseSetupAsync(NpgsqlConnection db) { using (var c = db.CreateCommand()) { c.CommandText = $"create table if not exists {BackingTable} (" @@ -175,7 +172,6 @@ class GuildConfiguration { /// If true, this method shall not create a new entry and will return null if the guild does /// not exist in the database. /// - [Obsolete("Begin using extension method to retrieve necessary data instead.", false)] public static async Task LoadAsync(ulong guildId, bool nullIfUnknown) { // TODO nullable static analysis problem: how to indicate non-null return when nullIfUnknown parameter is true? using (var db = await Database.OpenConnectionAsync().ConfigureAwait(false)) { diff --git a/Data/GuildUserConfiguration.cs b/Data/GuildUserConfiguration.cs index e11448b..930b264 100644 --- a/Data/GuildUserConfiguration.cs +++ b/Data/GuildUserConfiguration.cs @@ -7,6 +7,7 @@ namespace BirthdayBot.Data; /// /// Represents configuration for a guild user as may exist in the database. /// +[Obsolete(Database.ObsoleteReason, error: false)] class GuildUserConfiguration { public ulong GuildId { get; } public ulong UserId { get; } @@ -106,7 +107,6 @@ class GuildUserConfiguration { /// /// Attempts to retrieve a user's configuration. Returns a new, updateable instance if none is found. /// - [Obsolete("Migrate to using extension methods to retrieve necessary data instead.", false)] public static async Task LoadAsync(ulong guildId, ulong userId) { using var db = await Database.OpenConnectionAsync().ConfigureAwait(false); using var c = db.CreateCommand(); @@ -123,7 +123,6 @@ class GuildUserConfiguration { /// /// Gets all known user configuration records associated with the specified guild. /// - [Obsolete("Migrate to using extension methods to retrieve necessary data instead.", false)] public static async Task> LoadAllAsync(ulong guildId) { using var db = await Database.OpenConnectionAsync().ConfigureAwait(false); using var c = db.CreateCommand();