2022-02-25 04:53:08 +00:00
|
|
|
|
using BirthdayBot.Data;
|
|
|
|
|
using Discord.Interactions;
|
|
|
|
|
|
|
|
|
|
namespace BirthdayBot.ApplicationCommands;
|
|
|
|
|
/// <summary>
|
2022-03-12 05:52:46 +00:00
|
|
|
|
/// Precondition requiring the executing user be recognized as a bot moderator.<br/>
|
|
|
|
|
/// A bot moderator has either the Manage Server permission or is a member of the designated bot moderator role.
|
2022-02-25 04:53:08 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
class RequireBotModeratorAttribute : PreconditionAttribute {
|
2022-03-12 05:52:46 +00:00
|
|
|
|
public const string Error = "User did not pass the mod check.";
|
2022-03-11 03:23:08 +00:00
|
|
|
|
public const string Reply = ":x: You must be a moderator to use this command.";
|
2022-02-25 04:53:08 +00:00
|
|
|
|
|
2022-03-12 05:52:46 +00:00
|
|
|
|
public override string ErrorMessage => Error;
|
2022-03-11 03:23:08 +00:00
|
|
|
|
|
2022-03-20 04:09:42 +00:00
|
|
|
|
public override Task<PreconditionResult> CheckRequirementsAsync(
|
2022-03-11 03:23:08 +00:00
|
|
|
|
IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) {
|
2022-03-12 05:52:46 +00:00
|
|
|
|
// 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...
|
2022-03-20 04:09:42 +00:00
|
|
|
|
if (context.User is not SocketGuildUser user)
|
|
|
|
|
return Task.FromResult(PreconditionResult.FromError(RequireGuildContextAttribute.Error));
|
2022-02-25 04:53:08 +00:00
|
|
|
|
|
2022-03-20 04:09:42 +00:00
|
|
|
|
if (user.GuildPermissions.ManageGuild) return Task.FromResult(PreconditionResult.FromSuccess());
|
|
|
|
|
using var db = new BotDatabaseContext();
|
2022-03-20 19:40:33 +00:00
|
|
|
|
var checkRole = (ulong?)db.GuildConfigurations
|
|
|
|
|
.Where(g => g.GuildId == (long)((SocketGuild)context.Guild).Id)
|
2022-10-10 21:27:54 +00:00
|
|
|
|
.Select(g => g.ModeratorRole).FirstOrDefault();
|
2022-03-20 19:40:33 +00:00
|
|
|
|
if (checkRole.HasValue && user.Roles.Any(r => r.Id == checkRole.Value))
|
2022-03-20 04:09:42 +00:00
|
|
|
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
2022-03-11 03:23:08 +00:00
|
|
|
|
|
2022-03-20 04:09:42 +00:00
|
|
|
|
return Task.FromResult(PreconditionResult.FromError(Error));
|
2022-02-25 04:53:08 +00:00
|
|
|
|
}
|
2022-03-12 05:52:46 +00:00
|
|
|
|
}
|