[Feature] Parameter precondition attribute for simplifying performing hierarchical operations within a guild (#2906)

* Support interaction framework and update bundled preconditions docs

* Support text commands and update bundled preconditions docs

* Fix example

* Move hierarchy util to `PermissionUtils`

* Refactoring
This commit is contained in:
zobweyt
2024-05-12 01:27:15 +03:00
committed by GitHub
parent 476ec068f1
commit 1a5cba875d
5 changed files with 130 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
using System;
namespace Discord
{
/// <summary>
/// Provides a series of helper methods for permissions.
/// </summary>
public static class PermissionUtils
{
/// <summary>
/// Determines the hierarchy of a target object based on its type.
/// </summary>
/// <param name="target">
/// The target object: <see cref="IRole"/>, <see cref="IGuildUser"/>, or <see cref="IUser"/>.
/// </param>
/// <returns>
/// An integer representing the hierarchy of the target.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when the parameter type is not supported by this precondition attribute.
/// </exception>
public static int GetHieararchy(object target) => target switch
{
// The order of cases here is important to determine the correct hierarchy value.
IRole role => role.Position,
IGuildUser guildUser => guildUser.Hierarchy,
IUser => int.MinValue,
_ => throw new ArgumentOutOfRangeException(nameof(target), "Cannot determine hierarchy for the provided target.")
};
}
}