From 960bfa07b7bd1df2ddbb0914c18538dae691b0b5 Mon Sep 17 00:00:00 2001 From: Bram Date: Thu, 5 Aug 2021 15:38:08 +0200 Subject: [PATCH] Added default perms to slash commands --- .../Attributes/SlashCommandAttribute.cs | 9 ++++++++- .../Attributes/SlashCommandGroupAttribute.cs | 9 ++++++++- .../Info/ISlashCommandInfo.cs | 5 +++++ .../Info/SlashCommandInfo.cs | 11 +++++++++-- .../Builders/SlashCommandBuildService.cs | 7 ++++--- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Color-Chan.Discord.Commands/Attributes/SlashCommandAttribute.cs b/src/Color-Chan.Discord.Commands/Attributes/SlashCommandAttribute.cs index 781d7a63..b0a58f5d 100644 --- a/src/Color-Chan.Discord.Commands/Attributes/SlashCommandAttribute.cs +++ b/src/Color-Chan.Discord.Commands/Attributes/SlashCommandAttribute.cs @@ -16,6 +16,7 @@ public class SlashCommandAttribute : Attribute /// /// The name of the command. /// The description of what the command does. + /// Whether the command is enabled by default when the app is added to a guild. Default: true. /// /// Thrown when or doesn't /// match the command name requirements. @@ -24,7 +25,7 @@ public class SlashCommandAttribute : Attribute /// Thrown when or is /// null. /// - public SlashCommandAttribute(string name, string description) + public SlashCommandAttribute(string name, string description, bool defaultPermission = true) { if (name.Length is < 1 or > 32) throw new ArgumentException("Command names must be between 1 and 32 characters."); @@ -36,6 +37,7 @@ public SlashCommandAttribute(string name, string description) Name = name ?? throw new ArgumentNullException(nameof(name), "Command name can not be null"); Description = description ?? throw new ArgumentNullException(nameof(description), "Command description can not be null"); + DefaultPermission = defaultPermission; } /// @@ -47,5 +49,10 @@ public SlashCommandAttribute(string name, string description) /// The description of what the command does. /// public string Description { get; } + + /// + /// Whether the command is enabled by default when the app is added to a guild. + /// + public bool DefaultPermission { get; } } } \ No newline at end of file diff --git a/src/Color-Chan.Discord.Commands/Attributes/SlashCommandGroupAttribute.cs b/src/Color-Chan.Discord.Commands/Attributes/SlashCommandGroupAttribute.cs index 42ae399c..b2582e91 100644 --- a/src/Color-Chan.Discord.Commands/Attributes/SlashCommandGroupAttribute.cs +++ b/src/Color-Chan.Discord.Commands/Attributes/SlashCommandGroupAttribute.cs @@ -16,6 +16,7 @@ public class SlashCommandGroupAttribute : Attribute /// /// The name of the command group. /// The description of what the command group does. + /// Whether the command is enabled by default when the app is added to a guild. Default: true. /// /// Thrown when or doesn't /// match the command group name requirements. @@ -24,7 +25,7 @@ public class SlashCommandGroupAttribute : Attribute /// Thrown when or is /// null. /// - public SlashCommandGroupAttribute(string name, string description) + public SlashCommandGroupAttribute(string name, string description, bool defaultPermission = true) { if (name.Length is < 1 or > 32) throw new ArgumentException("Command group names must be between 1 and 32 characters."); @@ -36,6 +37,7 @@ public SlashCommandGroupAttribute(string name, string description) Name = name ?? throw new ArgumentNullException(nameof(name), "Command group name can not be null"); Description = description ?? throw new ArgumentNullException(nameof(description), "Command group description can not be null"); + DefaultPermission = defaultPermission; } /// @@ -47,5 +49,10 @@ public SlashCommandGroupAttribute(string name, string description) /// The description of what the command group is. /// public string Description { get; } + + /// + /// Whether the command is enabled by default when the app is added to a guild. + /// + public bool DefaultPermission { get; } } } \ No newline at end of file diff --git a/src/Color-Chan.Discord.Commands/Info/ISlashCommandInfo.cs b/src/Color-Chan.Discord.Commands/Info/ISlashCommandInfo.cs index a0db0d7b..14fd6973 100644 --- a/src/Color-Chan.Discord.Commands/Info/ISlashCommandInfo.cs +++ b/src/Color-Chan.Discord.Commands/Info/ISlashCommandInfo.cs @@ -19,6 +19,11 @@ public interface ISlashCommandInfo /// public string Description { get; set; } + /// + /// Whether the command is enabled by default when the app is added to a guild. + /// + public bool DefaultPermission { get; set; } + /// /// The containing the method of the command. /// diff --git a/src/Color-Chan.Discord.Commands/Info/SlashCommandInfo.cs b/src/Color-Chan.Discord.Commands/Info/SlashCommandInfo.cs index 9ce9c61f..13b3052b 100644 --- a/src/Color-Chan.Discord.Commands/Info/SlashCommandInfo.cs +++ b/src/Color-Chan.Discord.Commands/Info/SlashCommandInfo.cs @@ -12,12 +12,14 @@ public class SlashCommandInfo : ISlashCommandInfo /// /// The name of the command. /// The description of the command. + /// Whether the command is enabled by default when the app is added to a guild. /// The containing the method of the command. /// The command module containing the . - public SlashCommandInfo(string name, string description, MethodInfo command, TypeInfo module) + public SlashCommandInfo(string name, string description, bool defaultPermission, MethodInfo command, TypeInfo module) { CommandName = name; Description = description; + DefaultPermission = defaultPermission; CommandMethod = command; ParentModule = module; } @@ -27,11 +29,13 @@ public SlashCommandInfo(string name, string description, MethodInfo command, Typ /// /// The name of the command. /// The description of the command. + /// Whether the command is enabled by default when the app is added to a guild. /// The command module containing the . - public SlashCommandInfo(string name, string description, TypeInfo module) + public SlashCommandInfo(string name, string description, bool defaultPermission, TypeInfo module) { CommandName = name; Description = description; + DefaultPermission = defaultPermission; ParentModule = module; } @@ -41,6 +45,9 @@ public SlashCommandInfo(string name, string description, TypeInfo module) /// public string Description { get; set; } + /// + public bool DefaultPermission { get; set; } + /// public MethodInfo? CommandMethod { get; set; } diff --git a/src/Color-Chan.Discord.Commands/Services/Implementations/Builders/SlashCommandBuildService.cs b/src/Color-Chan.Discord.Commands/Services/Implementations/Builders/SlashCommandBuildService.cs index 34214ca3..8bd4883e 100644 --- a/src/Color-Chan.Discord.Commands/Services/Implementations/Builders/SlashCommandBuildService.cs +++ b/src/Color-Chan.Discord.Commands/Services/Implementations/Builders/SlashCommandBuildService.cs @@ -113,7 +113,8 @@ public IEnumerable BuildSlashCommandsParams(IEn { Name = commandInfo.CommandName, Description = commandInfo.Description, - Options = options + Options = options, + DefaultPermission = commandInfo.DefaultPermission }); } @@ -136,7 +137,7 @@ public IEnumerable BuildSlashCommandsParams(IEn var commandAttribute = validMethod.GetCustomAttribute(); if (commandAttribute is not null) { - var commandInfo = new SlashCommandInfo(commandAttribute.Name, commandAttribute.Description, validMethod, parentModule) + var commandInfo = new SlashCommandInfo(commandAttribute.Name, commandAttribute.Description, commandAttribute.DefaultPermission, validMethod, parentModule) { Guilds = _guildBuildService.GetCommandGuilds(validMethod), CommandOptions = _optionBuildService.GetCommandOptions(validMethod).ToList(), @@ -153,7 +154,7 @@ public IEnumerable BuildSlashCommandsParams(IEn private KeyValuePair BuildCommandGroupInfoKeyValuePair(SlashCommandGroupAttribute groupAttribute, TypeInfo parentModule) { // Build the command group. - var commandGroup = new SlashCommandInfo(groupAttribute.Name, groupAttribute.Description, parentModule) + var commandGroup = new SlashCommandInfo(groupAttribute.Name, groupAttribute.Description, groupAttribute.DefaultPermission, parentModule) { CommandOptions = new List(), Guilds = _guildBuildService.GetCommandGuilds(parentModule),