示例#1
0
        /// <summary>
        ///     Adds an option to the current slash command.
        /// </summary>
        /// <param name="Name">The name of the option to add.</param>
        /// <param name="Type">The type of this option.</param>
        /// <param name="Description">The description of this option.</param>
        /// <param name="Required">If this option is required for this command.</param>
        /// <param name="Default">If this option is the default option.</param>
        /// <param name="Options">The options of the option to add.</param>
        /// <param name="Choices">The choices of this option.</param>
        /// <returns>The current builder.</returns>
        public SlashCommandBuilder AddOption(string Name, ApplicationCommandOptionType Type,
                                             string Description, bool Required = true, bool Default = false, List <SlashCommandOptionBuilder> 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 <ApplicationCommandOptionChoiceProperties>(Choices) : null;

            return(AddOption(option));
        }
示例#2
0
        /// <summary>
        ///     Adds an option to this slash command.
        /// </summary>
        /// <param name="Parameter">The option to add.</param>
        /// <returns>The current builder.</returns>
        public SlashCommandBuilder AddOption(SlashCommandOptionBuilder Option)
        {
            if (this.Options == null)
            {
                this.Options = new List <SlashCommandOptionBuilder>();
            }

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