From 689c77d476b23503a54ed1b7d399e9d3db2ebf2d Mon Sep 17 00:00:00 2001 From: quin lynch Date: Sat, 29 May 2021 07:07:55 -0300 Subject: [PATCH] Closes #8. Added DM support for buttons and slash commands. Added SlashCommandBuilder --- .../Interactions/ApplicationCommandOption.cs | 12 +- .../Interactions/SlashCommandBuilder.cs | 489 ++++++++++++++++++ .../Slash Commands/SocketSlashCommand.cs | 3 + .../Slash Commands/SocketSlashCommandData.cs | 3 + 4 files changed, 505 insertions(+), 2 deletions(-) create mode 100644 src/Discord.Net.Core/Entities/Interactions/SlashCommandBuilder.cs diff --git a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOption.cs b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOption.cs index f9e34362c..d33990da1 100644 --- a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOption.cs +++ b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandOption.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Discord @@ -22,8 +23,15 @@ namespace Discord get => _name; set { - if (value?.Length > 32) - throw new ArgumentException("Name length must be less than or equal to 32"); + if (value == null) + throw new ArgumentNullException($"{nameof(Name)} cannot be null!"); + + if (value.Length > 32) + throw new ArgumentException($"{nameof(Name)} length must be less than or equal to 32"); + + if (!Regex.IsMatch(value, @"^[\w-]{1,32}$")) + throw new ArgumentException($"{nameof(Name)} must match the regex ^[\\w-]{{1,32}}$"); + _name = value; } } diff --git a/src/Discord.Net.Core/Entities/Interactions/SlashCommandBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/SlashCommandBuilder.cs new file mode 100644 index 000000000..c3c1aaa7f --- /dev/null +++ b/src/Discord.Net.Core/Entities/Interactions/SlashCommandBuilder.cs @@ -0,0 +1,489 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Discord +{ + /// + /// A class used to build slash commands. + /// + public class SlashCommandBuilder + { + /// + /// Returns the maximun length a commands name allowed by Discord + /// + public const int MaxNameLength = 32; + /// + /// Returns the maximum length of a commands description allowed by Discord. + /// + public const int MaxDescriptionLength = 100; + /// + /// Returns the maximum count of command options allowed by Discord + /// + public const int MaxOptionsCount = 10; + + /// + /// The name of this slash command. + /// + public string Name + { + get + { + return _name; + } + set + { + Preconditions.NotNullOrEmpty(value, nameof(Name)); + Preconditions.AtLeast(value.Length, 3, nameof(Name)); + Preconditions.AtMost(value.Length, MaxNameLength, nameof(Name)); + + // Discord updated the docs, this regex prevents special characters like @!$%(... etc, + // https://discord.com/developers/docs/interactions/slash-commands#applicationcommand + if (!Regex.IsMatch(value, @"^[\w-]{3,32}$")) + throw new ArgumentException("Command name cannot contian any special characters or whitespaces!"); + + _name = value; + } + } + + /// + /// A 1-100 length description of this slash command + /// + public string Description + { + get + { + return _description; + } + set + { + Preconditions.AtLeast(value.Length, 1, nameof(Description)); + Preconditions.AtMost(value.Length, MaxDescriptionLength, nameof(Description)); + + _description = value; + } + } + + /// + /// Gets or sets the options for this command. + /// + public List Options + { + get => _options; + set + { + if (value != null) + if (value.Count > MaxOptionsCount) + throw new ArgumentException(message: $"Option count must be less than or equal to {MaxOptionsCount}.", paramName: nameof(Options)); + + _options = value; + } + } + private string _name { get; set; } + private string _description { get; set; } + private List _options { get; set; } + + public SlashCommandCreationProperties Build() + { + SlashCommandCreationProperties props = new SlashCommandCreationProperties() + { + Name = this.Name, + Description = this.Description, + }; + + if (this.Options != null && this.Options.Any()) + { + var options = new List(); + + this.Options.ForEach(x => options.Add(x.Build())); + + props.Options = options; + } + + return props; + + } + + public SlashCommandBuilder WithName(string Name) + { + this.Name = Name; + return this; + } + + /// + /// Sets the description of the current command. + /// + /// The description of this command. + /// The current builder. + public SlashCommandBuilder WithDescription(string Description) + { + this.Description = Description; + return this; + } + + /// + /// Adds an option to the current slash command. + /// + /// The name of the option to add. + /// The type of this option. + /// The description of this option. + /// If this option is required for this command. + /// If this option is the default option. + /// The options of the option to add. + /// The choices of this option. + /// The current builder. + public SlashCommandBuilder AddOption(string Name, ApplicationCommandOptionType Type, + string Description, bool Required = true, bool Default = false, List Options = null, params ApplicationCommandOptionChoiceProperties[] Choices) + { + // Make sure the name matches the requirements from discord + Preconditions.NotNullOrEmpty(Name, nameof(Name)); + Preconditions.AtLeast(Name.Length, 3, nameof(Name)); + Preconditions.AtMost(Name.Length, MaxNameLength, nameof(Name)); + + // Discord updated the docs, this regex prevents special characters like @!$%( and s p a c e s.. etc, + // https://discord.com/developers/docs/interactions/slash-commands#applicationcommand + if (!Regex.IsMatch(Name, @"^[\w-]{3,32}$")) + throw new ArgumentException("Command name cannot contian any special characters or whitespaces!", nameof(Name)); + + // same with description + Preconditions.NotNullOrEmpty(Description, nameof(Description)); + Preconditions.AtLeast(Description.Length, 3, nameof(Description)); + Preconditions.AtMost(Description.Length, MaxDescriptionLength, nameof(Description)); + + // make sure theres only one option with default set to true + if (Default) + { + if (this.Options != null) + if (this.Options.Any(x => x.Default)) + throw new ArgumentException("There can only be one command option with default set to true!", nameof(Default)); + } + + SlashCommandOptionBuilder option = new SlashCommandOptionBuilder(); + option.Name = Name; + option.Description = Description; + option.Required = Required; + option.Default = Default; + option.Options = Options; + option.Choices = Choices != null ? new List(Choices) : null; + + return AddOption(option); + } + + /// + /// Adds an option to the current slash command. + /// + /// The name of the option to add. + /// The type of this option. + /// The description of this option. + /// If this option is required for this command. + /// If this option is the default option. + /// The choices of this option. + /// The current builder. + public SlashCommandBuilder AddOption(string Name, ApplicationCommandOptionType Type, + string Description, bool Required = true, bool Default = false, params ApplicationCommandOptionChoiceProperties[] Choices) + => AddOption(Name, Type, Description, Required, Default, null, Choices); + + /// + /// Adds an option to the current slash command. + /// + /// The name of the option to add. + /// The type of this option. + /// The sescription of this option. + /// The current builder. + public SlashCommandBuilder AddOption(string Name, ApplicationCommandOptionType Type, string Description) + => AddOption(Name, Type, Description, Options: null, Choices: null); + + /// + /// Adds an option to this slash command. + /// + /// The option to add. + /// The current builder. + public SlashCommandBuilder AddOption(SlashCommandOptionBuilder Option) + { + if (this.Options == null) + this.Options = new List(); + + if (this.Options.Count >= MaxOptionsCount) + throw new ArgumentOutOfRangeException(nameof(Options), $"Cannot have more than {MaxOptionsCount} options!"); + + if (Option == null) + throw new ArgumentNullException(nameof(Option), "Option cannot be null"); + + this.Options.Add(Option); + return this; + } + /// + /// Adds a collection of options to the current slash command. + /// + /// The collection of options to add. + /// The current builder. + public SlashCommandBuilder AddOptions(params SlashCommandOptionBuilder[] Options) + { + if (Options == null) + throw new ArgumentNullException(nameof(Options), "Options cannot be null!"); + + if (Options.Length == 0) + throw new ArgumentException(nameof(Options), "Options cannot be empty!"); + + if (this.Options == null) + this.Options = new List(); + + if (this.Options.Count + Options.Length > MaxOptionsCount) + throw new ArgumentOutOfRangeException(nameof(Options), $"Cannot have more than {MaxOptionsCount} options!"); + + this.Options.AddRange(Options); + return this; + } + } + + /// + /// Represents a class used to build options for the . + /// + public class SlashCommandOptionBuilder + { + /// + /// The max length of a choice's name allowed by Discord. + /// + public const int ChoiceNameMaxLength = 100; + + /// + /// The maximum number of choices allowed by Discord. + /// + public const int MaxChoiceCount = 10; + + private string _name; + private string _description; + + /// + /// The name of this option. + /// + public string Name + { + get => _name; + set + { + if (value?.Length > SlashCommandBuilder.MaxNameLength) + throw new ArgumentException("Name length must be less than or equal to 32"); + if (value?.Length < 1) + throw new ArgumentException("Name length must at least 1 characters in length"); + + if (value != null) + if (!Regex.IsMatch(value, @"^[\w-]{3,32}$")) + throw new ArgumentException("Option name cannot contian any special characters or whitespaces!"); + + _name = value; + } + } + + /// + /// The description of this option. + /// + public string Description + { + get => _description; + set + { + if (value?.Length > SlashCommandBuilder.MaxDescriptionLength) + throw new ArgumentException("Description length must be less than or equal to 100"); + if (value?.Length < 1) + throw new ArgumentException("Name length must at least 1 character in length"); + + _description = value; + } + } + + /// + /// The type of this option. + /// + public ApplicationCommandOptionType Type { get; set; } + + /// + /// The first required option for the user to complete. only one option can be default. + /// + public bool Default { get; set; } + + /// + /// if this option is required for this command, otherwise . + /// + public bool Required { get; set; } + + /// + /// choices for string and int types for the user to pick from. + /// + public List Choices { get; set; } + + /// + /// If the option is a subcommand or subcommand group type, this nested options will be the parameters. + /// + public List Options { get; set; } + + /// + /// Builds the current option. + /// + /// The built version of this option. + public ApplicationCommandOptionProperties Build() + { + bool isSubType = this.Type == ApplicationCommandOptionType.SubCommand || this.Type == ApplicationCommandOptionType.SubCommandGroup; + + if (isSubType && (Options == null || !Options.Any())) + throw new ArgumentException(nameof(Options), "SubCommands/SubCommandGroups must have at least one option"); + + if (!isSubType && (Options != null && Options.Any())) + throw new ArgumentException(nameof(Options), $"Cannot have options on {Type} type"); + + return new ApplicationCommandOptionProperties() + { + Name = this.Name, + Description = this.Description, + Default = this.Default, + Required = this.Required, + Type = this.Type, + Options = new List(this.Options.Select(x => x.Build())), + Choices = this.Choices + }; + } + + /// + /// Adds a sub option to the current option. + /// + /// The sub option to add. + /// The current builder. + public SlashCommandOptionBuilder AddOption(SlashCommandOptionBuilder option) + { + if (this.Options == null) + this.Options = new List(); + + if (this.Options.Count >= SlashCommandBuilder.MaxOptionsCount) + throw new ArgumentOutOfRangeException(nameof(Choices), $"There can only be {SlashCommandBuilder.MaxOptionsCount} options per sub command group!"); + + if (option == null) + throw new ArgumentNullException(nameof(option), "Option cannot be null"); + + Options.Add(option); + return this; + } + + /// + /// Adds a choice to the current option. + /// + /// The name of the choice. + /// The value of the choice. + /// The current builder. + public SlashCommandOptionBuilder AddChoice(string Name, int Value) + { + if (Choices == null) + Choices = new List(); + + if (Choices.Count >= MaxChoiceCount) + throw new ArgumentOutOfRangeException(nameof(Choices), $"Cannot add more than {MaxChoiceCount} choices!"); + + if (Name == null) + throw new ArgumentNullException($"{nameof(Name)} cannot be null!"); + + Preconditions.AtLeast(Name.Length, 1, nameof(Name)); + Preconditions.AtMost(Name.Length, 100, nameof(Name)); + + Choices.Add(new ApplicationCommandOptionChoiceProperties() + { + Name = Name, + Value = Value + }); + + return this; + } + + /// + /// Adds a choice to the current option. + /// + /// The name of the choice. + /// The value of the choice. + /// The current builder. + public SlashCommandOptionBuilder AddChoice(string Name, string Value) + { + if (Choices == null) + Choices = new List(); + + if (Choices.Count >= MaxChoiceCount) + throw new ArgumentOutOfRangeException(nameof(Choices), $"Cannot add more than {MaxChoiceCount} choices!"); + + if (Name == null) + throw new ArgumentNullException($"{nameof(Name)} cannot be null!"); + + if (Value == null) + throw new ArgumentNullException($"{nameof(Value)} cannot be null!"); + + Preconditions.AtLeast(Name.Length, 1, nameof(Name)); + Preconditions.AtMost(Name.Length, 100, nameof(Name)); + + Preconditions.AtLeast(Value.Length, 1, nameof(Value)); + Preconditions.AtMost(Value.Length, 100, nameof(Value)); + + Choices.Add(new ApplicationCommandOptionChoiceProperties() + { + Name = Name, + Value = Value + }); + + return this; + } + + /// + /// Sets the current builders name. + /// + /// The name to set the current option builder. + /// The current builder. + public SlashCommandOptionBuilder WithName(string Name) + { + this.Name = Name; + + return this; + } + + /// + /// Sets the current builders description. + /// + /// The description to set. + /// The current builder. + public SlashCommandOptionBuilder WithDescription(string Description) + { + this.Description = Description; + return this; + } + + /// + /// Sets the current builders required field. + /// + /// The value to set. + /// The current builder. + public SlashCommandOptionBuilder WithRequired(bool value) + { + this.Required = value; + return this; + } + + /// + /// Sets the current builders default field. + /// + /// The value to set. + /// The current builder. + public SlashCommandOptionBuilder WithDefault(bool value) + { + this.Default = value; + return this; + } + + /// + /// Sets the current type of this builder. + /// + /// The type to set. + /// The current builder. + public SlashCommandOptionBuilder WithType(ApplicationCommandOptionType Type) + { + this.Type = Type; + return this; + } + } +} diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs index 72e8a0d6a..3561ac8c0 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs @@ -8,6 +8,9 @@ using Model = Discord.API.Gateway.InteractionCreated; namespace Discord.WebSocket { + /// + /// Represents a Websocket-based slash command received over the gateway. + /// public class SocketSlashCommand : SocketInteraction { /// diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandData.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandData.cs index 3ef6678e1..b3cb5ddd2 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandData.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandData.cs @@ -5,6 +5,9 @@ using Model = Discord.API.ApplicationCommandInteractionData; namespace Discord.WebSocket { + /// + /// Represents the data tied with the interaction. + /// public class SocketSlashCommandData : SocketEntity, IApplicationCommandInteractionData { ///