-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
93cb71a
commit 753724d
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Discord.Net.Interactions/Attributes/Preconditions/RequireTeamAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Discord.Interactions | ||
{ | ||
/// <summary> | ||
/// Requires the command to be invoked by a member of the team that owns the bot. | ||
/// </summary> | ||
/// <remarks> | ||
/// 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 <see cref="PreconditionResult"/> will be returned with the | ||
/// message "Command can only be run by a member of the bot's team." | ||
/// <note> | ||
/// This precondition will only work if the account has a <see cref="TokenType"/> of <see cref="TokenType.Bot"/> | ||
/// ;otherwise, this precondition will always fail. | ||
/// </note> | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] | ||
public class RequireTeamAttribute : PreconditionAttribute | ||
{ | ||
/// <summary> | ||
/// The team roles to require. Valid values: "*", "admin", "developer", or "read_only" | ||
/// </summary> | ||
public string[] TeamRoles { get; } = []; | ||
|
||
/// <summary> | ||
/// Requires that the user invoking the command to have a specific team role. | ||
/// </summary> | ||
/// <param name="teamRoles">The team roles to require. Valid values: "*", "admin", "developer", or "read_only"</param> | ||
public RequireTeamAttribute(params string[] teamRoles) | ||
{ | ||
TeamRoles = teamRoles ?? TeamRoles; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override async Task<PreconditionResult> 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)}."); | ||
} | ||
} | ||
} | ||
} |