Browse Source

add perms-v2 properties to command info classes

pull/2222/head
Cenngo 3 years ago
parent
commit
96e34a64ce
5 changed files with 75 additions and 3 deletions
  1. +8
    -0
      src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs
  2. +8
    -0
      src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs
  3. +13
    -0
      src/Discord.Net.Interactions/Info/IApplicationCommandInfo.cs
  4. +28
    -0
      src/Discord.Net.Interactions/Info/ModuleInfo.cs
  5. +18
    -3
      src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs

+ 8
- 0
src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs View File

@@ -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();
}



+ 8
- 0
src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs View File

@@ -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();



+ 13
- 0
src/Discord.Net.Interactions/Info/IApplicationCommandInfo.cs View File

@@ -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; }
}
}

+ 28
- 0
src/Discord.Net.Interactions/Info/ModuleInfo.cs View File

@@ -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;
}
}
}

+ 18
- 3
src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs View File

@@ -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)


Loading…
Cancel
Save