From 753724d15cc12528b1c66407d438cccb29a26343 Mon Sep 17 00:00:00 2001 From: Zach Goodson Date: Sat, 11 May 2024 15:24:38 -0500 Subject: [PATCH] Create RequireTeamAttribute.cs (#2903) * Create RequireTeamAttribute.cs * Fix Attribute Doc * Attempt to Fix NULL Cases. * Fix NULL check (oops). * Add RequireTeamAtttribute to preconditions.md * Fix Typo --- docs/guides/int_framework/preconditions.md | 1 + .../Preconditions/RequireTeamAttribute.cs | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/Discord.Net.Interactions/Attributes/Preconditions/RequireTeamAttribute.cs diff --git a/docs/guides/int_framework/preconditions.md b/docs/guides/int_framework/preconditions.md index 75e57279..bfa7168e 100644 --- a/docs/guides/int_framework/preconditions.md +++ b/docs/guides/int_framework/preconditions.md @@ -29,6 +29,7 @@ to use. * @Discord.Interactions.RequireUserPermissionAttribute * @Discord.Interactions.RequireNsfwAttribute * @Discord.Interactions.RequireRoleAttribute +* @Discord.Interactions.RequireTeamAttribute ## Using Preconditions diff --git a/src/Discord.Net.Interactions/Attributes/Preconditions/RequireTeamAttribute.cs b/src/Discord.Net.Interactions/Attributes/Preconditions/RequireTeamAttribute.cs new file mode 100644 index 00000000..18ff9e39 --- /dev/null +++ b/src/Discord.Net.Interactions/Attributes/Preconditions/RequireTeamAttribute.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Discord.Interactions +{ + /// + /// Requires the command to be invoked by a member of the team that owns the bot. + /// + /// + /// This precondition will restrict the access of the command or module to the a member of the team of the Discord application, narrowed to specific team roles if specified. + /// If the precondition fails to be met, an erroneous will be returned with the + /// message "Command can only be run by a member of the bot's team." + /// + /// This precondition will only work if the account has a of + /// ;otherwise, this precondition will always fail. + /// + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] + public class RequireTeamAttribute : PreconditionAttribute + { + /// + /// The team roles to require. Valid values: "*", "admin", "developer", or "read_only" + /// + public string[] TeamRoles { get; } = []; + + /// + /// Requires that the user invoking the command to have a specific team role. + /// + /// The team roles to require. Valid values: "*", "admin", "developer", or "read_only" + public RequireTeamAttribute(params string[] teamRoles) + { + TeamRoles = teamRoles ?? TeamRoles; + } + + /// + public override async Task CheckRequirementsAsync(IInteractionContext context, ICommandInfo command, IServiceProvider services) + { + switch (context.Client.TokenType) + { + case TokenType.Bot: + var application = await context.Client.GetApplicationInfoAsync().ConfigureAwait(false); + + var idFound = false; + + foreach (var member in application.Team.TeamMembers) + { + if (member.User.Id == context.User.Id) + { + if (TeamRoles.Length == 0 || TeamRoles.Any(role => member.Permissions.Contains(role))) + { + idFound = true; + } + + break; + } + } + + if (idFound == false) + return PreconditionResult.FromError(ErrorMessage ?? $"Command can only be run by a member of the bot's team {(TeamRoles.Length == 0 ? "." : "with the specified permissions.")}"); + return PreconditionResult.FromSuccess(); + default: + return PreconditionResult.FromError($"{nameof(RequireTeamAttribute)} is not supported by this {nameof(TokenType)}."); + } + } + } +}