feature: V2 Permissions (#2222)
* Initial V2 permissions * add perms-v2 attributes and properties, add deprecation messages * add perms-v2 properties to command info classes * add perms-v2 fields to Rest/SocketApplicationCommand entities and IApplicationCommand * fix json name of DmPermission field Co-authored-by: Cenngo <cenk.ergen1@gmail.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace Discord.Interactions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the <see cref="IApplicationCommandInfo.DefaultMemberPermissions"/> of an application command or module.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class DefaultMemberPermissionsAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the default permission required to use this command.
|
||||
/// </summary>
|
||||
public GuildPermission Permissions { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="IApplicationCommandInfo.DefaultMemberPermissions"/> of an application command or module.
|
||||
/// </summary>
|
||||
/// <param name="permissions">The default permission required to use this command.</param>
|
||||
public DefaultMemberPermissionsAttribute(GuildPermission permissions)
|
||||
{
|
||||
Permissions = permissions;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ namespace Discord.Interactions
|
||||
/// Set the "Default Permission" property of an Application Command.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
[Obsolete($"Soon to be deprecated, use Permissions-v2 attributes like {nameof(EnabledInDmAttribute)} and {nameof(DefaultMemberPermissionsAttribute)}")]
|
||||
public class DefaultPermissionAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace Discord.Interactions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the <see cref="IApplicationCommandInfo.IsEnabledInDm"/> property of an application command or module.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class EnabledInDmAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets whether or not this command can be used in DMs.
|
||||
/// </summary>
|
||||
public bool IsEnabled { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="IApplicationCommandInfo.IsEnabledInDm"/> property of an application command or module.
|
||||
/// </summary>
|
||||
/// <param name="isEnabled">Whether or not this command can be used in DMs.</param>
|
||||
public EnabledInDmAttribute(bool isEnabled)
|
||||
{
|
||||
IsEnabled = isEnabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,19 @@ namespace Discord.Interactions.Builders
|
||||
/// <summary>
|
||||
/// Gets the default permission of this command.
|
||||
/// </summary>
|
||||
[Obsolete($"To be deprecated soon, use {nameof(IsEnabledInDm)} and {nameof(DefaultMemberPermissions)} instead.")]
|
||||
public bool DefaultPermission { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this command can be used in DMs.
|
||||
/// </summary>
|
||||
public bool IsEnabledInDm { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default permissions needed for executing this command.
|
||||
/// </summary>
|
||||
public GuildPermission? DefaultMemberPermissions { get; set; } = null;
|
||||
|
||||
internal ContextCommandBuilder (ModuleBuilder module) : base(module) { }
|
||||
|
||||
/// <summary>
|
||||
@@ -49,6 +60,7 @@ namespace Discord.Interactions.Builders
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
[Obsolete($"To be deprecated soon, use {nameof(SetEnabledInDm)} and {nameof(WithDefaultMemberPermissions)} instead.")]
|
||||
public ContextCommandBuilder SetDefaultPermission (bool defaultPermision)
|
||||
{
|
||||
DefaultPermission = defaultPermision;
|
||||
@@ -70,6 +82,32 @@ namespace Discord.Interactions.Builders
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets <see cref="IsEnabledInDm"/>.
|
||||
/// </summary>
|
||||
/// <param name="isEnabled">New value of the <see cref="IsEnabledInDm"/>.</param>
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
public ContextCommandBuilder SetEnabledInDm(bool isEnabled)
|
||||
{
|
||||
IsEnabledInDm = isEnabled;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets <see cref="DefaultMemberPermissions"/>.
|
||||
/// </summary>
|
||||
/// <param name="permissions">New value of the <see cref="DefaultMemberPermissions"/>.</param>
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
public ContextCommandBuilder WithDefaultMemberPermissions(GuildPermission permissions)
|
||||
{
|
||||
DefaultMemberPermissions = permissions;
|
||||
return this;
|
||||
}
|
||||
|
||||
internal override ContextCommandInfo Build (ModuleInfo module, InteractionService commandService) =>
|
||||
ContextCommandInfo.Create(this, module, commandService);
|
||||
}
|
||||
|
||||
@@ -17,8 +17,19 @@ namespace Discord.Interactions.Builders
|
||||
/// <summary>
|
||||
/// Gets and sets the default permission of this command.
|
||||
/// </summary>
|
||||
[Obsolete($"To be deprecated soon, use {nameof(IsEnabledInDm)} and {nameof(DefaultMemberPermissions)} instead.")]
|
||||
public bool DefaultPermission { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this command can be used in DMs.
|
||||
/// </summary>
|
||||
public bool IsEnabledInDm { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default permissions needed for executing this command.
|
||||
/// </summary>
|
||||
public GuildPermission? DefaultMemberPermissions { get; set; } = null;
|
||||
|
||||
internal SlashCommandBuilder (ModuleBuilder module) : base(module) { }
|
||||
|
||||
/// <summary>
|
||||
@@ -49,6 +60,7 @@ namespace Discord.Interactions.Builders
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
[Obsolete($"To be deprecated soon, use {nameof(SetEnabledInDm)} and {nameof(WithDefaultMemberPermissions)} instead.")]
|
||||
public SlashCommandBuilder WithDefaultPermission (bool permission)
|
||||
{
|
||||
DefaultPermission = permission;
|
||||
@@ -70,6 +82,32 @@ namespace Discord.Interactions.Builders
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets <see cref="IsEnabledInDm"/>.
|
||||
/// </summary>
|
||||
/// <param name="isEnabled">New value of the <see cref="IsEnabledInDm"/>.</param>
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
public SlashCommandBuilder SetEnabledInDm(bool isEnabled)
|
||||
{
|
||||
IsEnabledInDm = isEnabled;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets <see cref="DefaultMemberPermissions"/>.
|
||||
/// </summary>
|
||||
/// <param name="permissions">New value of the <see cref="DefaultMemberPermissions"/>.</param>
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
public SlashCommandBuilder WithDefaultMemberPermissions(GuildPermission permissions)
|
||||
{
|
||||
DefaultMemberPermissions = permissions;
|
||||
return this;
|
||||
}
|
||||
|
||||
internal override SlashCommandInfo Build (ModuleInfo module, InteractionService commandService) =>
|
||||
new SlashCommandInfo(this, module, commandService);
|
||||
}
|
||||
|
||||
@@ -51,8 +51,19 @@ namespace Discord.Interactions.Builders
|
||||
/// <summary>
|
||||
/// Gets and sets the default permission of this module.
|
||||
/// </summary>
|
||||
[Obsolete($"To be deprecated soon, use {nameof(IsEnabledInDm)} and {nameof(DefaultMemberPermissions)} instead.")]
|
||||
public bool DefaultPermission { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this command can be used in DMs.
|
||||
/// </summary>
|
||||
public bool IsEnabledInDm { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default permissions needed for executing this command.
|
||||
/// </summary>
|
||||
public GuildPermission? DefaultMemberPermissions { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// Gets and sets whether this has a <see cref="DontAutoRegisterAttribute"/>.
|
||||
/// </summary>
|
||||
@@ -159,12 +170,39 @@ namespace Discord.Interactions.Builders
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
[Obsolete($"To be deprecated soon, use {nameof(SetEnabledInDm)} and {nameof(WithDefaultMemberPermissions)} instead.")]
|
||||
public ModuleBuilder WithDefaultPermission (bool permission)
|
||||
{
|
||||
DefaultPermission = permission;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets <see cref="IsEnabledInDm"/>.
|
||||
/// </summary>
|
||||
/// <param name="isEnabled">New value of the <see cref="IsEnabledInDm"/>.</param>
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
public ModuleBuilder SetEnabledInDm(bool isEnabled)
|
||||
{
|
||||
IsEnabledInDm = isEnabled;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets <see cref="DefaultMemberPermissions"/>.
|
||||
/// </summary>
|
||||
/// <param name="permissions">New value of the <see cref="DefaultMemberPermissions"/>.</param>
|
||||
/// <returns>
|
||||
/// The builder instance.
|
||||
/// </returns>
|
||||
public ModuleBuilder WithDefaultMemberPermissions(GuildPermission permissions)
|
||||
{
|
||||
DefaultMemberPermissions = permissions;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds attributes to <see cref="Attributes"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -85,6 +85,16 @@ namespace Discord.Interactions.Builders
|
||||
builder.DefaultPermission = defPermission.IsDefaultPermission;
|
||||
}
|
||||
break;
|
||||
case EnabledInDmAttribute enabledInDm:
|
||||
{
|
||||
builder.IsEnabledInDm = enabledInDm.IsEnabled;
|
||||
}
|
||||
break;
|
||||
case DefaultMemberPermissionsAttribute memberPermission:
|
||||
{
|
||||
builder.DefaultMemberPermissions = memberPermission.Permissions;
|
||||
}
|
||||
break;
|
||||
case PreconditionAttribute precondition:
|
||||
builder.AddPreconditions(precondition);
|
||||
break;
|
||||
@@ -169,6 +179,16 @@ namespace Discord.Interactions.Builders
|
||||
builder.DefaultPermission = defaultPermission.IsDefaultPermission;
|
||||
}
|
||||
break;
|
||||
case EnabledInDmAttribute enabledInDm:
|
||||
{
|
||||
builder.IsEnabledInDm = enabledInDm.IsEnabled;
|
||||
}
|
||||
break;
|
||||
case DefaultMemberPermissionsAttribute memberPermission:
|
||||
{
|
||||
builder.DefaultMemberPermissions = memberPermission.Permissions;
|
||||
}
|
||||
break;
|
||||
case PreconditionAttribute precondition:
|
||||
builder.WithPreconditions(precondition);
|
||||
break;
|
||||
@@ -211,6 +231,16 @@ namespace Discord.Interactions.Builders
|
||||
builder.DefaultPermission = defaultPermission.IsDefaultPermission;
|
||||
}
|
||||
break;
|
||||
case EnabledInDmAttribute enabledInDm:
|
||||
{
|
||||
builder.IsEnabledInDm = enabledInDm.IsEnabled;
|
||||
}
|
||||
break;
|
||||
case DefaultMemberPermissionsAttribute memberPermission:
|
||||
{
|
||||
builder.DefaultMemberPermissions = memberPermission.Permissions;
|
||||
}
|
||||
break;
|
||||
case PreconditionAttribute precondition:
|
||||
builder.WithPreconditions(precondition);
|
||||
break;
|
||||
|
||||
@@ -17,6 +17,12 @@ namespace Discord.Interactions
|
||||
/// <inheritdoc/>
|
||||
public bool DefaultPermission { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool IsEnabledInDm { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public GuildPermission? DefaultMemberPermissions { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IReadOnlyCollection<CommandParameterInfo> Parameters { get; }
|
||||
|
||||
@@ -31,6 +37,8 @@ namespace Discord.Interactions
|
||||
{
|
||||
CommandType = builder.CommandType;
|
||||
DefaultPermission = builder.DefaultPermission;
|
||||
IsEnabledInDm = builder.IsEnabledInDm;
|
||||
DefaultMemberPermissions = builder.DefaultMemberPermissions;
|
||||
Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,12 @@ namespace Discord.Interactions
|
||||
/// <inheritdoc/>
|
||||
public bool DefaultPermission { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool IsEnabledInDm { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public GuildPermission? DefaultMemberPermissions { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IReadOnlyCollection<SlashCommandParameterInfo> Parameters { get; }
|
||||
|
||||
@@ -41,6 +47,8 @@ namespace Discord.Interactions
|
||||
{
|
||||
Description = builder.Description;
|
||||
DefaultPermission = builder.DefaultPermission;
|
||||
IsEnabledInDm = builder.IsEnabledInDm;
|
||||
DefaultMemberPermissions = builder.DefaultMemberPermissions;
|
||||
Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray();
|
||||
FlattenedParameters = FlattenParameters(Parameters).ToImmutableArray();
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
|
||||
namespace Discord.Interactions
|
||||
{
|
||||
/// <summary>
|
||||
@@ -18,6 +20,17 @@ namespace Discord.Interactions
|
||||
/// <summary>
|
||||
/// Gets the DefaultPermission of this command.
|
||||
/// </summary>
|
||||
[Obsolete($"To be deprecated soon, use {nameof(IsEnabledInDm)} and {nameof(DefaultMemberPermissions)} instead.")]
|
||||
bool DefaultPermission { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this command can be used in DMs.
|
||||
/// </summary>
|
||||
public bool IsEnabledInDm { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default permissions needed for executing this command.
|
||||
/// </summary>
|
||||
public GuildPermission? DefaultMemberPermissions { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,19 @@ namespace Discord.Interactions
|
||||
/// <summary>
|
||||
/// Gets the default Permission of this module.
|
||||
/// </summary>
|
||||
[Obsolete($"To be deprecated soon, use {nameof(IsEnabledInDm)} and {nameof(DefaultMemberPermissions)} instead.")]
|
||||
public bool DefaultPermission { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this command can be used in DMs.
|
||||
/// </summary>
|
||||
public bool IsEnabledInDm { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default permissions needed for executing this command.
|
||||
/// </summary>
|
||||
public GuildPermission? DefaultMemberPermissions { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of Sub Modules of this module.
|
||||
/// </summary>
|
||||
@@ -110,6 +121,8 @@ namespace Discord.Interactions
|
||||
Description = builder.Description;
|
||||
Parent = parent;
|
||||
DefaultPermission = builder.DefaultPermission;
|
||||
IsEnabledInDm = builder.IsEnabledInDm;
|
||||
DefaultMemberPermissions = BuildDefaultMemberPermissions(builder);
|
||||
SlashCommands = BuildSlashCommands(builder).ToImmutableArray();
|
||||
ContextCommands = BuildContextCommands(builder).ToImmutableArray();
|
||||
ComponentCommands = BuildComponentCommands(builder).ToImmutableArray();
|
||||
@@ -226,5 +239,20 @@ namespace Discord.Interactions
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static GuildPermission? BuildDefaultMemberPermissions(ModuleBuilder builder)
|
||||
{
|
||||
var permissions = builder.DefaultMemberPermissions;
|
||||
|
||||
var parent = builder.Parent;
|
||||
|
||||
while (parent != null)
|
||||
{
|
||||
permissions = (permissions ?? 0) | (parent.DefaultMemberPermissions ?? 0);
|
||||
parent = parent.Parent;
|
||||
}
|
||||
|
||||
return permissions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,8 @@ namespace Discord.Interactions
|
||||
{
|
||||
Name = commandInfo.Name,
|
||||
Description = commandInfo.Description,
|
||||
IsDefaultPermission = commandInfo.DefaultPermission,
|
||||
IsDMEnabled = commandInfo.IsEnabledInDm,
|
||||
DefaultMemberPermissions = (commandInfo.DefaultMemberPermissions ?? 0) | (commandInfo.Module.DefaultMemberPermissions ?? 0)
|
||||
}.Build();
|
||||
|
||||
if (commandInfo.Parameters.Count > SlashCommandBuilder.MaxOptionsCount)
|
||||
@@ -64,8 +65,20 @@ namespace Discord.Interactions
|
||||
public static ApplicationCommandProperties ToApplicationCommandProps(this ContextCommandInfo commandInfo)
|
||||
=> commandInfo.CommandType switch
|
||||
{
|
||||
ApplicationCommandType.Message => new MessageCommandBuilder { Name = commandInfo.Name, IsDefaultPermission = commandInfo.DefaultPermission}.Build(),
|
||||
ApplicationCommandType.User => new UserCommandBuilder { Name = commandInfo.Name, IsDefaultPermission=commandInfo.DefaultPermission}.Build(),
|
||||
ApplicationCommandType.Message => new MessageCommandBuilder
|
||||
{
|
||||
Name = commandInfo.Name,
|
||||
IsDefaultPermission = commandInfo.DefaultPermission,
|
||||
DefaultMemberPermissions = (commandInfo.DefaultMemberPermissions ?? 0) | (commandInfo.Module.DefaultMemberPermissions ?? 0),
|
||||
IsDMEnabled = commandInfo.IsEnabledInDm
|
||||
}.Build(),
|
||||
ApplicationCommandType.User => new UserCommandBuilder
|
||||
{
|
||||
Name = commandInfo.Name,
|
||||
IsDefaultPermission = commandInfo.DefaultPermission,
|
||||
DefaultMemberPermissions = (commandInfo.DefaultMemberPermissions ?? 0) | (commandInfo.Module.DefaultMemberPermissions ?? 0),
|
||||
IsDMEnabled = commandInfo.IsEnabledInDm
|
||||
}.Build(),
|
||||
_ => throw new InvalidOperationException($"{commandInfo.CommandType} isn't a supported command type.")
|
||||
};
|
||||
#endregion
|
||||
@@ -113,6 +126,8 @@ namespace Discord.Interactions
|
||||
Name = moduleInfo.SlashGroupName,
|
||||
Description = moduleInfo.Description,
|
||||
IsDefaultPermission = moduleInfo.DefaultPermission,
|
||||
IsDMEnabled = moduleInfo.IsEnabledInDm,
|
||||
DefaultMemberPermissions = moduleInfo.DefaultMemberPermissions
|
||||
}.Build();
|
||||
|
||||
if (options.Count > SlashCommandBuilder.MaxOptionsCount)
|
||||
|
||||
Reference in New Issue
Block a user