Edit obsolete tags, remove soon-to-be-unneeded extension methods

This commit is contained in:
Noi 2022-03-19 14:53:53 -07:00
parent 135a554bc5
commit 948c6955ec
6 changed files with 27 additions and 34 deletions

View file

@ -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<string>(jc, KeySqlDatabase, false);
if (sqldb != null) csb.Database = sqldb; // Optional database setting
Database.DBConnectionString = csb.ToString();
DatabaseConnectionString = csb.ToString();
}
private static T? ReadConfKey<T>(JObject jc, string key, [DoesNotReturnIf(true)] bool failOnEmpty) {

View file

@ -3,13 +3,27 @@
namespace BirthdayBot.Data;
public class BotDatabaseContext : DbContext {
public virtual DbSet<BlocklistEntry> BlocklistEntries { get; set; } = null!;
public virtual DbSet<GuildConfig> GuildConfigurations { get; set; } = null!;
public virtual DbSet<UserEntry> 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<BlocklistEntry> BlocklistEntries { get; set; } = null!;
public DbSet<GuildConfig> GuildConfigurations { get; set; } = null!;
public DbSet<UserEntry> 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) {

View file

@ -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; }
/// <summary>

View file

@ -1,21 +0,0 @@
namespace BirthdayBot.Data;
internal static class Extensions {
/// <summary>
/// Retrieves the database-backed bot configuration for this guild.
/// </summary>
internal static async Task<GuildConfiguration> GetConfigAsync(this SocketGuild guild)
=> await GuildConfiguration.LoadAsync(guild.Id, false);
/// <summary>
/// Retrieves a collection of all existing user configurations for this guild.
/// </summary>
internal static async Task<IEnumerable<GuildUserConfiguration>> GetUserConfigurationsAsync(this SocketGuild guild)
=> await GuildUserConfiguration.LoadAllAsync(guild.Id);
/// <summary>
/// Retrieves the database-backed bot configuration (birthday info) for this guild user.
/// </summary>
internal static async Task<GuildUserConfiguration> GetConfigAsync(this SocketGuildUser user)
=> await GuildUserConfiguration.LoadAsync(user.Guild.Id, user.Id);
}

View file

@ -8,6 +8,7 @@ namespace BirthdayBot.Data;
/// Represents guild-specific configuration as exists in the database.
/// Updating any property requires a call to <see cref="UpdateAsync"/> for changes to take effect.
/// </summary>
[Obsolete(Database.ObsoleteReason, error: false)]
class GuildConfiguration {
/// <summary>
/// Gets this configuration's corresponding guild ID.
@ -73,7 +74,6 @@ class GuildConfiguration {
/// <summary>
/// Checks if the specified user is blocked by current guild policy (block list or moderated mode).
/// </summary>
[Obsolete("Block lists should be reimplemented in a more resource-efficient manner later.", false)]
public async Task<bool> IsUserBlockedAsync(ulong userId) {
if (IsModerated) return true;
return await IsUserInBlocklistAsync(userId).ConfigureAwait(false);
@ -82,7 +82,6 @@ class GuildConfiguration {
/// <summary>
/// Checks if the given user exists in the block list.
/// </summary>
[Obsolete("Block lists should be reimplemented in a more resource-efficient manner later.", false)]
public async Task<bool> IsUserInBlocklistAsync(ulong userId) {
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
using var c = db.CreateCommand();
@ -99,7 +98,6 @@ class GuildConfiguration {
/// <summary>
/// Adds the specified user to the block list corresponding to this guild.
/// </summary>
[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.
/// </summary>
/// <returns>True if a user has been removed, false if the requested user was not in this list.</returns>
[Obsolete("Block lists will be reimplemented in a more practical manner later.", false)]
public async Task<bool> 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.
/// </summary>
[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.
/// </param>
[Obsolete("Begin using extension method to retrieve necessary data instead.", false)]
public static async Task<GuildConfiguration?> 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)) {

View file

@ -7,6 +7,7 @@ namespace BirthdayBot.Data;
/// <summary>
/// Represents configuration for a guild user as may exist in the database.
/// </summary>
[Obsolete(Database.ObsoleteReason, error: false)]
class GuildUserConfiguration {
public ulong GuildId { get; }
public ulong UserId { get; }
@ -106,7 +107,6 @@ class GuildUserConfiguration {
/// <summary>
/// Attempts to retrieve a user's configuration. Returns a new, updateable instance if none is found.
/// </summary>
[Obsolete("Migrate to using extension methods to retrieve necessary data instead.", false)]
public static async Task<GuildUserConfiguration> LoadAsync(ulong guildId, ulong userId) {
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
using var c = db.CreateCommand();
@ -123,7 +123,6 @@ class GuildUserConfiguration {
/// <summary>
/// Gets all known user configuration records associated with the specified guild.
/// </summary>
[Obsolete("Migrate to using extension methods to retrieve necessary data instead.", false)]
public static async Task<IEnumerable<GuildUserConfiguration>> LoadAllAsync(ulong guildId) {
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
using var c = db.CreateCommand();