mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-22 05:54:36 +00:00
Edit obsolete tags, remove soon-to-be-unneeded extension methods
This commit is contained in:
parent
135a554bc5
commit
948c6955ec
6 changed files with 27 additions and 34 deletions
|
@ -27,6 +27,8 @@ class Configuration {
|
||||||
public int ShardAmount { get; }
|
public int ShardAmount { get; }
|
||||||
public int ShardTotal { get; }
|
public int ShardTotal { get; }
|
||||||
|
|
||||||
|
public string DatabaseConnectionString { get; }
|
||||||
|
|
||||||
public Configuration(string[] args) {
|
public Configuration(string[] args) {
|
||||||
var cmdline = CmdLineOpts.Parse(args);
|
var cmdline = CmdLineOpts.Parse(args);
|
||||||
|
|
||||||
|
@ -74,7 +76,7 @@ class Configuration {
|
||||||
};
|
};
|
||||||
var sqldb = ReadConfKey<string>(jc, KeySqlDatabase, false);
|
var sqldb = ReadConfKey<string>(jc, KeySqlDatabase, false);
|
||||||
if (sqldb != null) csb.Database = sqldb; // Optional database setting
|
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) {
|
private static T? ReadConfKey<T>(JObject jc, string key, [DoesNotReturnIf(true)] bool failOnEmpty) {
|
||||||
|
|
|
@ -3,13 +3,27 @@
|
||||||
namespace BirthdayBot.Data;
|
namespace BirthdayBot.Data;
|
||||||
|
|
||||||
public class BotDatabaseContext : DbContext {
|
public class BotDatabaseContext : DbContext {
|
||||||
public virtual DbSet<BlocklistEntry> BlocklistEntries { get; set; } = null!;
|
private static string? _npgsqlConnectionString;
|
||||||
public virtual DbSet<GuildConfig> GuildConfigurations { get; set; } = null!;
|
internal static string NpgsqlConnectionString {
|
||||||
public virtual DbSet<UserEntry> UserEntries { get; set; } = null!;
|
#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)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
=> optionsBuilder
|
=> optionsBuilder
|
||||||
.UseNpgsql("Host=localhost;Username=birthdaybot;Password=bb") // TODO use actual connection string
|
.UseNpgsql(NpgsqlConnectionString)
|
||||||
.UseSnakeCaseNamingConvention();
|
.UseSnakeCaseNamingConvention();
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
||||||
|
|
|
@ -3,7 +3,10 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BirthdayBot.Data;
|
namespace BirthdayBot.Data;
|
||||||
|
|
||||||
|
[Obsolete(ObsoleteReason, error: false)]
|
||||||
internal static class Database {
|
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; }
|
public static string DBConnectionString { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -8,6 +8,7 @@ namespace BirthdayBot.Data;
|
||||||
/// Represents guild-specific configuration as exists in the database.
|
/// Represents guild-specific configuration as exists in the database.
|
||||||
/// Updating any property requires a call to <see cref="UpdateAsync"/> for changes to take effect.
|
/// Updating any property requires a call to <see cref="UpdateAsync"/> for changes to take effect.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete(Database.ObsoleteReason, error: false)]
|
||||||
class GuildConfiguration {
|
class GuildConfiguration {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets this configuration's corresponding guild ID.
|
/// Gets this configuration's corresponding guild ID.
|
||||||
|
@ -73,7 +74,6 @@ class GuildConfiguration {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if the specified user is blocked by current guild policy (block list or moderated mode).
|
/// Checks if the specified user is blocked by current guild policy (block list or moderated mode).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("Block lists should be reimplemented in a more resource-efficient manner later.", false)]
|
|
||||||
public async Task<bool> IsUserBlockedAsync(ulong userId) {
|
public async Task<bool> IsUserBlockedAsync(ulong userId) {
|
||||||
if (IsModerated) return true;
|
if (IsModerated) return true;
|
||||||
return await IsUserInBlocklistAsync(userId).ConfigureAwait(false);
|
return await IsUserInBlocklistAsync(userId).ConfigureAwait(false);
|
||||||
|
@ -82,7 +82,6 @@ class GuildConfiguration {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if the given user exists in the block list.
|
/// Checks if the given user exists in the block list.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("Block lists should be reimplemented in a more resource-efficient manner later.", false)]
|
|
||||||
public async Task<bool> IsUserInBlocklistAsync(ulong userId) {
|
public async Task<bool> IsUserInBlocklistAsync(ulong userId) {
|
||||||
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
||||||
using var c = db.CreateCommand();
|
using var c = db.CreateCommand();
|
||||||
|
@ -99,7 +98,6 @@ class GuildConfiguration {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the specified user to the block list corresponding to this guild.
|
/// Adds the specified user to the block list corresponding to this guild.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("Block lists will be reimplemented in a more practical manner later.", false)]
|
|
||||||
public async Task BlockUserAsync(ulong userId) {
|
public async Task BlockUserAsync(ulong userId) {
|
||||||
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
||||||
using var c = db.CreateCommand();
|
using var c = db.CreateCommand();
|
||||||
|
@ -117,7 +115,6 @@ class GuildConfiguration {
|
||||||
/// Removes the specified user from the block list corresponding to this guild.
|
/// Removes the specified user from the block list corresponding to this guild.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>True if a user has been removed, false if the requested user was not in this list.</returns>
|
/// <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) {
|
public async Task<bool> UnblockUserAsync(ulong userId) {
|
||||||
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
||||||
using var c = db.CreateCommand();
|
using var c = db.CreateCommand();
|
||||||
|
@ -134,7 +131,6 @@ class GuildConfiguration {
|
||||||
/// Checks if the given user can be considered a bot moderator.
|
/// 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.
|
/// Checks for either the Manage Guild permission or if the user is within a predetermined role.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("Usage should be phased out when text commands are removed. Use PreconditionAttribute from now on.", error: false)]
|
|
||||||
public bool IsBotModerator(SocketGuildUser user)
|
public bool IsBotModerator(SocketGuildUser user)
|
||||||
=> user.GuildPermissions.ManageGuild || (ModeratorRole.HasValue && user.Roles.Any(r => r.Id == ModeratorRole.Value));
|
=> 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 BackingTable = "settings";
|
||||||
public const string BackingTableBans = "banned_users";
|
public const string BackingTableBans = "banned_users";
|
||||||
|
|
||||||
|
[Obsolete("DELETE THIS", error: true)]
|
||||||
internal static async Task DatabaseSetupAsync(NpgsqlConnection db) {
|
internal static async Task DatabaseSetupAsync(NpgsqlConnection db) {
|
||||||
using (var c = db.CreateCommand()) {
|
using (var c = db.CreateCommand()) {
|
||||||
c.CommandText = $"create table if not exists {BackingTable} ("
|
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
|
/// If true, this method shall not create a new entry and will return null if the guild does
|
||||||
/// not exist in the database.
|
/// not exist in the database.
|
||||||
/// </param>
|
/// </param>
|
||||||
[Obsolete("Begin using extension method to retrieve necessary data instead.", false)]
|
|
||||||
public static async Task<GuildConfiguration?> LoadAsync(ulong guildId, bool nullIfUnknown) {
|
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?
|
// TODO nullable static analysis problem: how to indicate non-null return when nullIfUnknown parameter is true?
|
||||||
using (var db = await Database.OpenConnectionAsync().ConfigureAwait(false)) {
|
using (var db = await Database.OpenConnectionAsync().ConfigureAwait(false)) {
|
||||||
|
|
|
@ -7,6 +7,7 @@ namespace BirthdayBot.Data;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents configuration for a guild user as may exist in the database.
|
/// Represents configuration for a guild user as may exist in the database.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete(Database.ObsoleteReason, error: false)]
|
||||||
class GuildUserConfiguration {
|
class GuildUserConfiguration {
|
||||||
public ulong GuildId { get; }
|
public ulong GuildId { get; }
|
||||||
public ulong UserId { get; }
|
public ulong UserId { get; }
|
||||||
|
@ -106,7 +107,6 @@ class GuildUserConfiguration {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attempts to retrieve a user's configuration. Returns a new, updateable instance if none is found.
|
/// Attempts to retrieve a user's configuration. Returns a new, updateable instance if none is found.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("Migrate to using extension methods to retrieve necessary data instead.", false)]
|
|
||||||
public static async Task<GuildUserConfiguration> LoadAsync(ulong guildId, ulong userId) {
|
public static async Task<GuildUserConfiguration> LoadAsync(ulong guildId, ulong userId) {
|
||||||
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
||||||
using var c = db.CreateCommand();
|
using var c = db.CreateCommand();
|
||||||
|
@ -123,7 +123,6 @@ class GuildUserConfiguration {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets all known user configuration records associated with the specified guild.
|
/// Gets all known user configuration records associated with the specified guild.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("Migrate to using extension methods to retrieve necessary data instead.", false)]
|
|
||||||
public static async Task<IEnumerable<GuildUserConfiguration>> LoadAllAsync(ulong guildId) {
|
public static async Task<IEnumerable<GuildUserConfiguration>> LoadAllAsync(ulong guildId) {
|
||||||
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
||||||
using var c = db.CreateCommand();
|
using var c = db.CreateCommand();
|
||||||
|
|
Loading…
Reference in a new issue