2022-03-12 05:52:46 +00:00
|
|
|
|
using BirthdayBot.Data;
|
|
|
|
|
using Discord.Interactions;
|
|
|
|
|
|
|
|
|
|
namespace BirthdayBot.ApplicationCommands;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Only users not on the blocklist or affected by moderator mode may use the command.<br/>
|
|
|
|
|
/// This is used in the <see cref="BotModuleBase"/> base class. Manually using it anywhere else is unnecessary.
|
|
|
|
|
/// </summary>
|
|
|
|
|
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.";
|
|
|
|
|
|
2022-03-20 04:09:42 +00:00
|
|
|
|
public override Task<PreconditionResult> CheckRequirementsAsync(
|
2022-03-12 05:52:46 +00:00
|
|
|
|
IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) {
|
|
|
|
|
// Not in guild context, unaffected by blocking
|
2022-03-20 04:09:42 +00:00
|
|
|
|
if (context.Guild is not SocketGuild guild) return Task.FromResult(PreconditionResult.FromSuccess());
|
2022-03-12 05:52:46 +00:00
|
|
|
|
|
|
|
|
|
// Manage Guild permission overrides any blocks
|
|
|
|
|
var user = (SocketGuildUser)context.User;
|
2022-03-20 04:09:42 +00:00
|
|
|
|
if (user.GuildPermissions.ManageGuild) return Task.FromResult(PreconditionResult.FromSuccess());
|
2022-03-12 05:52:46 +00:00
|
|
|
|
|
2022-03-20 04:09:42 +00:00
|
|
|
|
using var db = new BotDatabaseContext();
|
|
|
|
|
var settings = guild.GetConfigOrNew(db);
|
2022-03-12 05:52:46 +00:00
|
|
|
|
|
|
|
|
|
// Bot moderators override any blocks
|
2022-03-20 04:09:42 +00:00
|
|
|
|
if (settings.ModeratorRole.HasValue && user.Roles.Any(r => r.Id == (ulong)settings.ModeratorRole.Value))
|
|
|
|
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
2022-03-12 05:52:46 +00:00
|
|
|
|
|
|
|
|
|
// Moderated mode check
|
2022-03-20 04:09:42 +00:00
|
|
|
|
if (settings.Moderated) return Task.FromResult(PreconditionResult.FromError(FailModerated));
|
2022-03-12 05:52:46 +00:00
|
|
|
|
|
|
|
|
|
// Block list check
|
2022-03-20 04:09:42 +00:00
|
|
|
|
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));
|
2022-03-12 05:52:46 +00:00
|
|
|
|
|
2022-03-20 04:09:42 +00:00
|
|
|
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
2022-03-12 05:52:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|