mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-21 21:54:36 +00:00
Update preconditions to use EF queries
This commit is contained in:
parent
0a81f208ea
commit
67decd6fb4
2 changed files with 20 additions and 15 deletions
|
@ -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 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 const string ReplyBlocked = ":x: You have been blocked from using bot commands in this server.";
|
||||||
|
|
||||||
public override async Task<PreconditionResult> CheckRequirementsAsync(
|
public override Task<PreconditionResult> CheckRequirementsAsync(
|
||||||
IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) {
|
IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) {
|
||||||
// Not in guild context, unaffected by blocking
|
// 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
|
// Manage Guild permission overrides any blocks
|
||||||
var user = (SocketGuildUser)context.User;
|
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
|
// 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
|
// Moderated mode check
|
||||||
if (gconf.IsModerated) return PreconditionResult.FromError(FailModerated);
|
if (settings.Moderated) return Task.FromResult(PreconditionResult.FromError(FailModerated));
|
||||||
|
|
||||||
// Block list check
|
// 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,17 +13,19 @@ class RequireBotModeratorAttribute : PreconditionAttribute {
|
||||||
|
|
||||||
public override string ErrorMessage => Error;
|
public override string ErrorMessage => Error;
|
||||||
|
|
||||||
public override async Task<PreconditionResult> CheckRequirementsAsync(
|
public override Task<PreconditionResult> CheckRequirementsAsync(
|
||||||
IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) {
|
IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) {
|
||||||
// A bot moderator can only exist in a guild context, so we must do this check.
|
// 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...
|
// 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();
|
if (user.GuildPermissions.ManageGuild) return Task.FromResult(PreconditionResult.FromSuccess());
|
||||||
var gconf = await ((SocketGuild)context.Guild).GetConfigAsync().ConfigureAwait(false);
|
using var db = new BotDatabaseContext();
|
||||||
if (gconf.ModeratorRole.HasValue && user.Roles.Any(r => r.Id == gconf.ModeratorRole.Value))
|
var settings = ((SocketGuild)context.Guild).GetConfigOrNew(db);
|
||||||
return PreconditionResult.FromSuccess();
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue