From e4f5c90aabc842935666e6c24f40edcd60593058 Mon Sep 17 00:00:00 2001 From: Noi Date: Mon, 16 Jan 2023 21:43:07 -0800 Subject: [PATCH] Remove EnforceBlockingAttribute and all related logic Users have been instructed to manually set command permissions from now on --- ApplicationCommands/BotModuleBase.cs | 1 - ApplicationCommands/ConfigModule.cs | 57 -------- .../Preconditions/EnforceBlocking.cs | 43 ------ Data/BlocklistEntry.cs | 17 --- Data/BotDatabaseContext.cs | 13 -- .../20230117053251_RemoveBlocking.Designer.cs | 131 ++++++++++++++++++ .../20230117053251_RemoveBlocking.cs | 39 ++++++ .../BotDatabaseContextModelSnapshot.cs | 30 ---- ShardInstance.cs | 2 - 9 files changed, 170 insertions(+), 163 deletions(-) delete mode 100644 ApplicationCommands/Preconditions/EnforceBlocking.cs delete mode 100644 Data/BlocklistEntry.cs create mode 100644 Data/Migrations/20230117053251_RemoveBlocking.Designer.cs create mode 100644 Data/Migrations/20230117053251_RemoveBlocking.cs diff --git a/ApplicationCommands/BotModuleBase.cs b/ApplicationCommands/BotModuleBase.cs index e3d63c9..c5a9c14 100644 --- a/ApplicationCommands/BotModuleBase.cs +++ b/ApplicationCommands/BotModuleBase.cs @@ -9,7 +9,6 @@ namespace BirthdayBot.ApplicationCommands; /// /// Base class for our interaction module classes. Contains common data for use in implementing classes. /// -[EnforceBlocking] public abstract class BotModuleBase : InteractionModuleBase { protected const string HelpPfxModOnly = "Bot moderators only: "; protected const string ErrGuildOnly = ":x: This command can only be run within a server."; diff --git a/ApplicationCommands/ConfigModule.cs b/ApplicationCommands/ConfigModule.cs index 556256a..dc0f66d 100644 --- a/ApplicationCommands/ConfigModule.cs +++ b/ApplicationCommands/ConfigModule.cs @@ -142,63 +142,6 @@ public class ConfigModule : BotModuleBase { } } - public const string ObsoleteAttrReason = "Made redundant by Discord's built-in command permissions. Will be removed eventually."; - const string ObsoleteNotice = ":x: This feature has been deprecated, and the setting of blocks has been disabled. " - + "All existing blocks that have been previously set up will cease to function in the near future.\n" - + "Please use Discord's equivalent built-in features to limit access to your users. " - + "For more information: https://discord.com/blog/slash-commands-permissions-discord-apps-bots."; - [Obsolete(ObsoleteAttrReason)] - [Group("block", HelpCmdBlocking)] - public class SubCmdsConfigBlocking : BotModuleBase { - [SlashCommand("add-block", HelpPfxModOnly + "Add a user to the block list.")] - public Task CmdAddBlock([Summary(description: "The user to block.")] SocketGuildUser user) => UpdateBlockAsync(user, true); - - [SlashCommand("remove-block", HelpPfxModOnly + "Remove a user from the block list.")] - public Task CmdDelBlock([Summary(description: "The user to unblock.")] SocketGuildUser user) => UpdateBlockAsync(user, false); - - private async Task UpdateBlockAsync(SocketGuildUser user, bool setting) { - // setting: true to add (set), false to remove (unset) - using var db = new BotDatabaseContext(); - var existing = db.BlocklistEntries - .Where(bl => bl.GuildId == user.Guild.Id && bl.UserId == user.Id).FirstOrDefault(); - - var already = (existing != null) == setting; - if (already) { - await RespondAsync($":white_check_mark: User is already {(setting ? "" : "not ")}blocked.").ConfigureAwait(false); - return; - } - - if (setting) { - await RespondAsync(ObsoleteNotice); - return; - } else db.Remove(existing!); - await db.SaveChangesAsync(); - - await RespondAsync($":white_check_mark: {Common.FormatName(user, false)} has been {(setting ? "" : "un")}blocked."); - } - - [SlashCommand("set-moderated", HelpPfxModOnly + "Set moderated mode on the server.")] - public async Task CmdSetModerated([Summary(name: "enable", description: "The moderated mode setting.")] bool setting) { - if (setting == true) { - await RespondAsync(ObsoleteNotice); - return; - } - - var current = false; - await DoDatabaseUpdate(Context, s => { - current = s.Moderated; - s.Moderated = setting; - }); - - var already = setting == current; - if (already) { - await RespondAsync($":white_check_mark: Moderated mode is already **{(setting ? "en" : "dis")}abled**."); - } else { - await RespondAsync($":white_check_mark: Moderated mode is now **{(setting ? "en" : "dis")}abled**.").ConfigureAwait(false); - } - } - } - [SlashCommand("check", HelpPfxModOnly + HelpCmdCheck)] public async Task CmdCheck() { static string DoTestFor(string label, Func test) => $"{label}: { (test() ? ":white_check_mark: Yes" : ":x: No") }"; diff --git a/ApplicationCommands/Preconditions/EnforceBlocking.cs b/ApplicationCommands/Preconditions/EnforceBlocking.cs deleted file mode 100644 index 49f4f0a..0000000 --- a/ApplicationCommands/Preconditions/EnforceBlocking.cs +++ /dev/null @@ -1,43 +0,0 @@ -using BirthdayBot.Data; -using Discord.Interactions; - -namespace BirthdayBot.ApplicationCommands; -/// -/// Only users not on the blocklist or affected by moderator mode may use the command.
-/// This is used in the base class. Manually using it anywhere else is unnecessary. -///
-[Obsolete(ConfigModule.ObsoleteAttrReason)] -class EnforceBlockingAttribute : PreconditionAttribute { - public const string FailModerated = "Guild has moderator mode enabled."; - public const string FailBlocked = "User is in the guild's block list."; - public const string ReplyModerated = ":x: This bot is in moderated mode, preventing you from using any bot commands in this server."; - public const string ReplyBlocked = ":x: You have been blocked from using bot commands in this server."; - - public override Task CheckRequirementsAsync( - IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) { - // Not in guild context, unaffected by blocking - if (context.Guild is not SocketGuild guild) return Task.FromResult(PreconditionResult.FromSuccess()); - - // Manage Guild permission overrides any blocks - var user = (SocketGuildUser)context.User; - if (user.GuildPermissions.ManageGuild) return Task.FromResult(PreconditionResult.FromSuccess()); - - using var db = new BotDatabaseContext(); - var settings = (from row in db.GuildConfigurations - where row.GuildId == guild.Id - select new { ModRole = row.ModeratorRole, ModMode = row.Moderated }).FirstOrDefault(); - if (settings != null) { - // Bot moderators override all blocking measures in place - if (user.Roles.Any(r => r.Id == settings.ModRole)) return Task.FromResult(PreconditionResult.FromSuccess()); - - // Check for moderated mode - if (settings.ModMode) return Task.FromResult(PreconditionResult.FromError(FailModerated)); - - // Check if user exists in blocklist - if (db.BlocklistEntries.Where(row => row.GuildId == guild.Id && row.UserId == user.Id).Any()) - return Task.FromResult(PreconditionResult.FromError(FailBlocked)); - } - - return Task.FromResult(PreconditionResult.FromSuccess()); - } -} \ No newline at end of file diff --git a/Data/BlocklistEntry.cs b/Data/BlocklistEntry.cs deleted file mode 100644 index 628c4f2..0000000 --- a/Data/BlocklistEntry.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace BirthdayBot.Data; - -[Obsolete(ApplicationCommands.ConfigModule.ObsoleteAttrReason)] -[Table("banned_users")] -public class BlocklistEntry { - [Key] - public ulong GuildId { get; set; } - [Key] - public ulong UserId { get; set; } - - [ForeignKey(nameof(GuildConfig.GuildId))] - [InverseProperty(nameof(GuildConfig.BlockedUsers))] - public GuildConfig Guild { get; set; } = null!; -} diff --git a/Data/BotDatabaseContext.cs b/Data/BotDatabaseContext.cs index 2e8fda1..ad986eb 100644 --- a/Data/BotDatabaseContext.cs +++ b/Data/BotDatabaseContext.cs @@ -17,8 +17,6 @@ public class BotDatabaseContext : DbContext { }.ToString(); } - [Obsolete(ApplicationCommands.ConfigModule.ObsoleteAttrReason)] - public DbSet BlocklistEntries { get; set; } = null!; public DbSet GuildConfigurations { get; set; } = null!; public DbSet UserEntries { get; set; } = null!; @@ -28,17 +26,6 @@ public class BotDatabaseContext : DbContext { .UseSnakeCaseNamingConvention(); protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity(entity => { - entity.HasKey(e => new { e.GuildId, e.UserId }) - .HasName("banned_users_pkey"); - - entity.HasOne(d => d.Guild) - .WithMany(p => p.BlockedUsers) - .HasForeignKey(d => d.GuildId) - .HasConstraintName("banned_users_guild_id_fkey") - .OnDelete(DeleteBehavior.Cascade); - }); - modelBuilder.Entity(entity => { entity.HasKey(e => e.GuildId) .HasName("settings_pkey"); diff --git a/Data/Migrations/20230117053251_RemoveBlocking.Designer.cs b/Data/Migrations/20230117053251_RemoveBlocking.Designer.cs new file mode 100644 index 0000000..aaaa558 --- /dev/null +++ b/Data/Migrations/20230117053251_RemoveBlocking.Designer.cs @@ -0,0 +1,131 @@ +// +using System; +using BirthdayBot.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace BirthdayBot.Data.Migrations +{ + [DbContext(typeof(BotDatabaseContext))] + [Migration("20230117053251_RemoveBlocking")] + partial class RemoveBlocking + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("BirthdayBot.Data.GuildConfig", b => + { + b.Property("GuildId") + .HasColumnType("numeric(20,0)") + .HasColumnName("guild_id"); + + b.Property("AnnounceMessage") + .HasColumnType("text") + .HasColumnName("announce_message"); + + b.Property("AnnounceMessagePl") + .HasColumnType("text") + .HasColumnName("announce_message_pl"); + + b.Property("AnnouncePing") + .HasColumnType("boolean") + .HasColumnName("announce_ping"); + + b.Property("AnnouncementChannel") + .HasColumnType("numeric(20,0)") + .HasColumnName("channel_announce_id"); + + b.Property("BirthdayRole") + .HasColumnType("numeric(20,0)") + .HasColumnName("role_id"); + + b.Property("GuildTimeZone") + .HasColumnType("text") + .HasColumnName("time_zone"); + + b.Property("LastSeen") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasColumnName("last_seen") + .HasDefaultValueSql("now()"); + + b.Property("Moderated") + .HasColumnType("boolean") + .HasColumnName("moderated"); + + b.Property("ModeratorRole") + .HasColumnType("numeric(20,0)") + .HasColumnName("moderator_role"); + + b.HasKey("GuildId") + .HasName("settings_pkey"); + + b.ToTable("settings", (string)null); + }); + + modelBuilder.Entity("BirthdayBot.Data.UserEntry", b => + { + b.Property("GuildId") + .HasColumnType("numeric(20,0)") + .HasColumnName("guild_id"); + + b.Property("UserId") + .HasColumnType("numeric(20,0)") + .HasColumnName("user_id"); + + b.Property("BirthDay") + .HasColumnType("integer") + .HasColumnName("birth_day"); + + b.Property("BirthMonth") + .HasColumnType("integer") + .HasColumnName("birth_month"); + + b.Property("LastSeen") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasColumnName("last_seen") + .HasDefaultValueSql("now()"); + + b.Property("TimeZone") + .HasColumnType("text") + .HasColumnName("time_zone"); + + b.HasKey("GuildId", "UserId") + .HasName("user_birthdays_pkey"); + + b.ToTable("user_birthdays", (string)null); + }); + + modelBuilder.Entity("BirthdayBot.Data.UserEntry", b => + { + b.HasOne("BirthdayBot.Data.GuildConfig", "Guild") + .WithMany("UserEntries") + .HasForeignKey("GuildId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("user_birthdays_guild_id_fkey"); + + b.Navigation("Guild"); + }); + + modelBuilder.Entity("BirthdayBot.Data.GuildConfig", b => + { + b.Navigation("UserEntries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Data/Migrations/20230117053251_RemoveBlocking.cs b/Data/Migrations/20230117053251_RemoveBlocking.cs new file mode 100644 index 0000000..158e933 --- /dev/null +++ b/Data/Migrations/20230117053251_RemoveBlocking.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BirthdayBot.Data.Migrations +{ + /// + public partial class RemoveBlocking : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "banned_users"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "banned_users", + columns: table => new + { + guildid = table.Column(name: "guild_id", type: "numeric(20,0)", nullable: false), + userid = table.Column(name: "user_id", type: "numeric(20,0)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("banned_users_pkey", x => new { x.guildid, x.userid }); + table.ForeignKey( + name: "banned_users_guild_id_fkey", + column: x => x.guildid, + principalTable: "settings", + principalColumn: "guild_id", + onDelete: ReferentialAction.Cascade); + }); + } + } +} diff --git a/Data/Migrations/BotDatabaseContextModelSnapshot.cs b/Data/Migrations/BotDatabaseContextModelSnapshot.cs index 98f9f26..c82301e 100644 --- a/Data/Migrations/BotDatabaseContextModelSnapshot.cs +++ b/Data/Migrations/BotDatabaseContextModelSnapshot.cs @@ -22,22 +22,6 @@ namespace BirthdayBot.Data.Migrations NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - modelBuilder.Entity("BirthdayBot.Data.BlocklistEntry", b => - { - b.Property("GuildId") - .HasColumnType("numeric(20,0)") - .HasColumnName("guild_id"); - - b.Property("UserId") - .HasColumnType("numeric(20,0)") - .HasColumnName("user_id"); - - b.HasKey("GuildId", "UserId") - .HasName("banned_users_pkey"); - - b.ToTable("banned_users", (string)null); - }); - modelBuilder.Entity("BirthdayBot.Data.GuildConfig", b => { b.Property("GuildId") @@ -122,18 +106,6 @@ namespace BirthdayBot.Data.Migrations b.ToTable("user_birthdays", (string)null); }); - modelBuilder.Entity("BirthdayBot.Data.BlocklistEntry", b => - { - b.HasOne("BirthdayBot.Data.GuildConfig", "Guild") - .WithMany("BlockedUsers") - .HasForeignKey("GuildId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired() - .HasConstraintName("banned_users_guild_id_fkey"); - - b.Navigation("Guild"); - }); - modelBuilder.Entity("BirthdayBot.Data.UserEntry", b => { b.HasOne("BirthdayBot.Data.GuildConfig", "Guild") @@ -148,8 +120,6 @@ namespace BirthdayBot.Data.Migrations modelBuilder.Entity("BirthdayBot.Data.GuildConfig", b => { - b.Navigation("BlockedUsers"); - b.Navigation("UserEntries"); }); #pragma warning restore 612, 618 diff --git a/ShardInstance.cs b/ShardInstance.cs index d1317fc..82ffa5e 100644 --- a/ShardInstance.cs +++ b/ShardInstance.cs @@ -150,8 +150,6 @@ public sealed class ShardInstance : IDisposable { if (result.Error == InteractionCommandError.UnmetPrecondition) { var errReply = result.ErrorReason switch { RequireBotModeratorAttribute.Error => RequireBotModeratorAttribute.Reply, - EnforceBlockingAttribute.FailBlocked => EnforceBlockingAttribute.ReplyBlocked, - EnforceBlockingAttribute.FailModerated => EnforceBlockingAttribute.ReplyModerated, RequireGuildContextAttribute.Error => RequireGuildContextAttribute.Reply, _ => result.ErrorReason };