mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-25 01:44:12 +00:00
33 lines
No EOL
1.7 KiB
C#
33 lines
No EOL
1.7 KiB
C#
using BirthdayBot.Data;
|
|
using Discord.Interactions;
|
|
|
|
namespace BirthdayBot.ApplicationCommands;
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
[Obsolete("Replace with appropriate DefaultMemberPermissionsAttribute")]
|
|
class RequireBotModeratorAttribute : PreconditionAttribute {
|
|
public const string Error = "User did not pass the mod check.";
|
|
public const string Reply = ":x: You must be a moderator to use this command.";
|
|
|
|
public override string ErrorMessage => Error;
|
|
|
|
public override Task<PreconditionResult> 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 Task.FromResult(PreconditionResult.FromError(RequireGuildContextAttribute.Error));
|
|
|
|
if (user.GuildPermissions.ManageGuild) return Task.FromResult(PreconditionResult.FromSuccess());
|
|
using var db = new BotDatabaseContext();
|
|
var checkRole = (ulong?)db.GuildConfigurations
|
|
.Where(g => g.GuildId == ((SocketGuild)context.Guild).Id)
|
|
.Select(g => g.ModeratorRole).FirstOrDefault();
|
|
if (checkRole.HasValue && user.Roles.Any(r => r.Id == checkRole.Value))
|
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
|
|
|
return Task.FromResult(PreconditionResult.FromError(Error));
|
|
}
|
|
} |