diff --git a/ApplicationCommands/Preconditions/EnforceBlocking.cs b/ApplicationCommands/Preconditions/EnforceBlocking.cs index bda3335..2041395 100644 --- a/ApplicationCommands/Preconditions/EnforceBlocking.cs +++ b/ApplicationCommands/Preconditions/EnforceBlocking.cs @@ -13,26 +13,29 @@ class EnforceBlockingAttribute : PreconditionAttribute { 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 async Task CheckRequirementsAsync( + 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 PreconditionResult.FromSuccess(); + 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 PreconditionResult.FromSuccess(); + if (user.GuildPermissions.ManageGuild) return Task.FromResult(PreconditionResult.FromSuccess()); - var gconf = await guild.GetConfigAsync().ConfigureAwait(false); + using var db = new BotDatabaseContext(); + var settings = guild.GetConfigOrNew(db); // Bot moderators override any blocks - if (gconf.ModeratorRole.HasValue && user.Roles.Any(r => r.Id == gconf.ModeratorRole.Value)) return PreconditionResult.FromSuccess(); + if (settings.ModeratorRole.HasValue && user.Roles.Any(r => r.Id == (ulong)settings.ModeratorRole.Value)) + return Task.FromResult(PreconditionResult.FromSuccess()); // Moderated mode check - if (gconf.IsModerated) return PreconditionResult.FromError(FailModerated); + if (settings.Moderated) return Task.FromResult(PreconditionResult.FromError(FailModerated)); // Block list check - if (await gconf.IsUserInBlocklistAsync(user.Id)) return PreconditionResult.FromError(FailBlocked); + var blockquery = db.BlocklistEntries.Where(entry => entry.GuildId == (long)guild.Id && entry.UserId == (long)user.Id); + if (blockquery.Any()) return Task.FromResult(PreconditionResult.FromError(FailBlocked)); - return PreconditionResult.FromSuccess(); + return Task.FromResult(PreconditionResult.FromSuccess()); } } \ No newline at end of file diff --git a/ApplicationCommands/Preconditions/RequireBotModerator.cs b/ApplicationCommands/Preconditions/RequireBotModerator.cs index a32e6cb..c63e0dd 100644 --- a/ApplicationCommands/Preconditions/RequireBotModerator.cs +++ b/ApplicationCommands/Preconditions/RequireBotModerator.cs @@ -13,17 +13,19 @@ class RequireBotModeratorAttribute : PreconditionAttribute { public override string ErrorMessage => Error; - public override async Task CheckRequirementsAsync( + public override Task CheckRequirementsAsync( IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) { // A bot moderator can only exist in a guild context, so we must do this check. // This check causes this precondition to become a functional equivalent to RequireGuildContextAttribute... - if (context.User is not SocketGuildUser user) return PreconditionResult.FromError(RequireGuildContextAttribute.Error); + if (context.User is not SocketGuildUser user) + return Task.FromResult(PreconditionResult.FromError(RequireGuildContextAttribute.Error)); - if (user.GuildPermissions.ManageGuild) return PreconditionResult.FromSuccess(); - var gconf = await ((SocketGuild)context.Guild).GetConfigAsync().ConfigureAwait(false); - if (gconf.ModeratorRole.HasValue && user.Roles.Any(r => r.Id == gconf.ModeratorRole.Value)) - return PreconditionResult.FromSuccess(); + if (user.GuildPermissions.ManageGuild) return Task.FromResult(PreconditionResult.FromSuccess()); + using var db = new BotDatabaseContext(); + var settings = ((SocketGuild)context.Guild).GetConfigOrNew(db); + if (settings.ModeratorRole.HasValue && user.Roles.Any(r => r.Id == (ulong)settings.ModeratorRole.Value)) + return Task.FromResult(PreconditionResult.FromSuccess()); - return PreconditionResult.FromError(Error); + return Task.FromResult(PreconditionResult.FromError(Error)); } } \ No newline at end of file