示例#1
0
        /// <inheritdoc/>
        public ICommandInstance InitializeCommand(ICommandInstanceDescriptor descriptor, CommandsOptions options)
        {
            // validate this is a correct command attribute type
            if (!(descriptor.Attribute is RegexCommandAttribute regexCommand))
            {
                throw new ArgumentException($"{this.GetType().Name} can only be used with {typeof(RegexCommandAttribute).Name} commands", nameof(descriptor.Attribute));
            }

            // if pattern starts with ^, replace it with \G
            // this will ensure it'll match start of the string when starting from index after prefix
            string pattern = regexCommand.Pattern;

            if (pattern.Length > 0 && pattern[0] == '^')
            {
                pattern = $@"\G{pattern.Substring(1)}";
            }

            // init instance
            return(new RegexCommandInstance(
                       pattern: pattern,
                       regexOptions: regexCommand.Options,
                       method: descriptor.Method,
                       requirements: descriptor.GetRequirements(),
                       prefixOverride: descriptor.GetPrefixOverride(),
                       prefixRequirementOverride: descriptor.GetPrefixRequirementOverride(),
                       caseSensitivityOverride: descriptor.GetCaseSensitivityOverride()));
        }
示例#2
0
        /// <inheritdoc/>
        public ICommandInstance InitializeCommand(ICommandInstanceDescriptor descriptor, CommandsOptions options)
        {
            // validate this is a correct command attribute type
            if (!(descriptor.Attribute is CommandAttribute command))
            {
                throw new ArgumentException($"{this.GetType().Name} can only be used with {typeof(CommandAttribute).Name} commands", nameof(descriptor.Attribute));
            }

            // init instance
            return(new StandardCommandInstance(
                       text: command.Text,
                       method: descriptor.Method,
                       requirements: descriptor.GetRequirements(),
                       prefixOverride: descriptor.GetPrefixOverride(),
                       prefixRequirementOverride: descriptor.GetPrefixRequirementOverride(),
                       caseSensitivityOverride: descriptor.GetCaseSensitivityOverride()));
        }