Skip to content

Commit

Permalink
Allow application command aliases by using multiple attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaZ2 committed Dec 10, 2023
1 parent 51b776e commit feefc67
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NetCord.Services.ApplicationCommands;

[AttributeUsage(AttributeTargets.Method)]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public abstract class ApplicationCommandAttribute : Attribute
{
private protected ApplicationCommandAttribute(string name)
Expand Down
24 changes: 14 additions & 10 deletions NetCord.Services/ApplicationCommands/ApplicationCommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,35 @@ private void AddModuleCore([DynamicallyAccessedMembers(DynamicallyAccessedMember
{
var configuration = _configuration;

var slashCommandAttribute = type.GetCustomAttribute<SlashCommandAttribute>();
if (slashCommandAttribute is not null)
bool slashCommandGroup = false;

foreach (var slashCommandAttribute in type.GetCustomAttributes<SlashCommandAttribute>())
{
SlashCommandGroupInfo<TContext> slashCommandGroupInfo = new(type, slashCommandAttribute, configuration);
OnAutocompleteAdd(slashCommandGroupInfo);
AddCommandInfo(slashCommandGroupInfo);

slashCommandGroup = true;
}
else

if (slashCommandGroup)
return;

foreach (var method in type.GetMethods())
{
foreach (var method in type.GetMethods())
foreach (var applicationCommandAttribute in method.GetCustomAttributes<ApplicationCommandAttribute>())
{
slashCommandAttribute = method.GetCustomAttribute<SlashCommandAttribute>();
if (slashCommandAttribute is not null)
if (applicationCommandAttribute is SlashCommandAttribute slashCommandAttribute)
{
SlashCommandInfo<TContext> slashCommandInfo = new(method, type, slashCommandAttribute, configuration);
OnAutocompleteAdd(slashCommandInfo);
AddCommandInfo(slashCommandInfo);
}

var userCommandAttribute = method.GetCustomAttribute<UserCommandAttribute>();
if (userCommandAttribute is not null)
if (applicationCommandAttribute is UserCommandAttribute userCommandAttribute)
AddCommandInfo(new UserCommandInfo<TContext>(method, type, userCommandAttribute, configuration));

var messageCommandAttribute = method.GetCustomAttribute<MessageCommandAttribute>();
if (messageCommandAttribute is not null)
if (applicationCommandAttribute is MessageCommandAttribute messageCommandAttribute)
AddCommandInfo(new MessageCommandInfo<TContext>(method, type, messageCommandAttribute, configuration));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NetCord.Services.ApplicationCommands;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class SlashCommandAttribute : ApplicationCommandAttribute
{
public SlashCommandAttribute(string name, string description) : base(name)
Expand Down
6 changes: 2 additions & 4 deletions NetCord.Services/ApplicationCommands/SlashCommandGroupInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ internal SlashCommandGroupInfo([DynamicallyAccessedMembers(DynamicallyAccessedMe

foreach (var method in type.GetMethods())
{
var subSlashCommandAttribute = method.GetCustomAttribute<SubSlashCommandAttribute>();
if (subSlashCommandAttribute is not null)
foreach (var subSlashCommandAttribute in method.GetCustomAttributes<SubSlashCommandAttribute>())
subCommands.Add(subSlashCommandAttribute.Name!, new SubSlashCommandInfo<TContext>(method, type, subSlashCommandAttribute, configuration));
}

Expand All @@ -44,8 +43,7 @@ internal SlashCommandGroupInfo([DynamicallyAccessedMembers(DynamicallyAccessedMe
if (!nested.IsAssignableTo(baseType))
continue;

var subSlashCommandAttribute = nested.GetCustomAttribute<SubSlashCommandAttribute>();
if (subSlashCommandAttribute is not null)
foreach (var subSlashCommandAttribute in nested.GetCustomAttributes<SubSlashCommandAttribute>())
subCommands.Add(subSlashCommandAttribute.Name!, new SubSlashCommandGroupInfo<TContext>(nested, subSlashCommandAttribute, configuration));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NetCord.Services.ApplicationCommands;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class SubSlashCommandAttribute : Attribute
{
public SubSlashCommandAttribute(string name, string description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ internal SubSlashCommandGroupInfo([DynamicallyAccessedMembers(DynamicallyAccesse

foreach (var method in type.GetMethods())
{
var subSlashCommandAttribute = method.GetCustomAttribute<SubSlashCommandAttribute>();
if (subSlashCommandAttribute is not null)
foreach (var subSlashCommandAttribute in method.GetCustomAttributes<SubSlashCommandAttribute>())
subCommands.Add(subSlashCommandAttribute.Name!, new SubSlashCommandInfo<TContext>(method, type, subSlashCommandAttribute, configuration));
}

Expand Down
5 changes: 5 additions & 0 deletions Tests/NetCord.Test/ApplicationCommands/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

namespace NetCord.Test.SlashCommands;

[SlashCommand("pn", "PN")]
[SlashCommand("permission-nested", "Permission")]
public class NestedCommand : ApplicationCommandModule<SlashCommandContext>
{
[SubSlashCommand("a", "A")]
[SubSlashCommand("add", "Permission add")]
public Task AddAsync(int i, Permissions permission)
{
Expand All @@ -23,9 +25,11 @@ public static InteractionCallback Remove(int i, Permissions permission)
return InteractionCallback.Message(permission.ToString());
}

[SubSlashCommand("l", "L")]
[SubSlashCommand("list", "Permission list")]
public class ListCommand : ApplicationCommandModule<SlashCommandContext>
{
[SubSlashCommand("u", "U")]
[SubSlashCommand("user", "Permission list user")]
public static InteractionCallback User(int i, Permissions permission)
{
Expand Down Expand Up @@ -114,6 +118,7 @@ public async Task PlayAsync()
//voiceClient.VoiceReceive += args => outputStream.WriteAsync(args.Frame);
}

[SlashCommand("c", "C")]
[SlashCommand("channel", "Channel")]
public Task ChannelAsync(TextChannel channel)
{
Expand Down
1 change: 1 addition & 0 deletions Tests/NetCord.Test/ApplicationCommands/MessageCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace NetCord.Test.ApplicationCommands;

public class MessageCommands : ApplicationCommandModule<MessageCommandContext>
{
[MessageCommand("Clear", DMPermission = false, DefaultGuildUserPermissions = Permissions.ManageMessages)]
[MessageCommand("Clear to this", DMPermission = false, DefaultGuildUserPermissions = Permissions.ManageMessages)]
public async Task ClearToAsync()
{
Expand Down
1 change: 1 addition & 0 deletions Tests/NetCord.Test/ApplicationCommands/UserCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace NetCord.Test.ApplicationCommands;

public class UserCommands : ApplicationCommandModule<UserCommandContext>
{
[UserCommand("Get ID")]
[UserCommand("Get id")]
public Task GetId()
=> RespondAsync(InteractionCallback.Message(Context.Target.Id.ToString()!));
Expand Down

0 comments on commit feefc67

Please sign in to comment.