implement wildcard lenght quantifiers, TreatAsRegex property and solve catastrpohic backtracking (#2528)

This commit is contained in:
Cenk Ergen
2022-12-14 17:06:57 +03:00
committed by GitHub
parent 25cfb8822f
commit 3b107c2d01
9 changed files with 89 additions and 7 deletions

View File

@@ -35,6 +35,9 @@ namespace Discord.Interactions.Builders
/// <inheritdoc/>
public bool IgnoreGroupNames { get; set; }
/// <inheritdoc/>
public bool TreatNameAsRegex { get; set; }
/// <inheritdoc/>
public RunMode RunMode { get; set; }
@@ -117,6 +120,19 @@ namespace Discord.Interactions.Builders
return Instance;
}
/// <summary>
/// Sets <see cref="TreatNameAsRegex"/>.
/// </summary>
/// <param name="value">New value of the <see cref="TreatNameAsRegex"/>.</param>
/// <returns>
/// The builder instance.
/// </returns>
public TBuilder WithNameAsRegex (bool value)
{
TreatNameAsRegex = value;
return Instance;
}
/// <summary>
/// Adds parameter builders to <see cref="Parameters"/>.
/// </summary>
@@ -163,6 +179,10 @@ namespace Discord.Interactions.Builders
ICommandBuilder ICommandBuilder.SetRunMode (RunMode runMode) =>
SetRunMode(runMode);
/// <inheritdoc/>
ICommandBuilder ICommandBuilder.WithNameAsRegex(bool value) =>
WithNameAsRegex(value);
/// <inheritdoc/>
ICommandBuilder ICommandBuilder.AddParameters (params IParameterBuilder[] parameters) =>
AddParameters(parameters as TParamBuilder);

View File

@@ -34,6 +34,11 @@ namespace Discord.Interactions.Builders
/// </summary>
bool IgnoreGroupNames { get; set; }
/// <summary>
/// Gets or sets whether the <see cref="Name"/> should be directly used as a Regex pattern.
/// </summary>
bool TreatNameAsRegex { get; set; }
/// <summary>
/// Gets or sets the run mode this command gets executed with.
/// </summary>
@@ -90,6 +95,15 @@ namespace Discord.Interactions.Builders
/// </returns>
ICommandBuilder SetRunMode (RunMode runMode);
/// <summary>
/// Sets <see cref="TreatNameAsRegex"/>.
/// </summary>
/// <param name="value">New value of the <see cref="TreatNameAsRegex"/>.</param>
/// <returns>
/// The builder instance.
/// </returns>
ICommandBuilder WithNameAsRegex(bool value);
/// <summary>
/// Adds parameter builders to <see cref="Parameters"/>.
/// </summary>

View File

@@ -274,6 +274,7 @@ namespace Discord.Interactions.Builders
builder.Name = interaction.CustomId;
builder.RunMode = interaction.RunMode;
builder.IgnoreGroupNames = interaction.IgnoreGroupNames;
builder.TreatNameAsRegex = interaction.TreatAsRegex;
}
break;
case PreconditionAttribute precondition:
@@ -287,7 +288,7 @@ namespace Discord.Interactions.Builders
var parameters = methodInfo.GetParameters();
var wildCardCount = Regex.Matches(Regex.Escape(builder.Name), Regex.Escape(commandService._wildCardExp)).Count;
var wildCardCount = RegexUtils.GetWildCardCount(builder.Name, commandService._wildCardExp);
foreach (var parameter in parameters)
builder.AddParameter(x => BuildComponentParameter(x, parameter, parameter.Position >= wildCardCount));
@@ -355,6 +356,7 @@ namespace Discord.Interactions.Builders
builder.Name = modal.CustomId;
builder.RunMode = modal.RunMode;
builder.IgnoreGroupNames = modal.IgnoreGroupNames;
builder.TreatNameAsRegex = modal.TreatAsRegex;
}
break;
case PreconditionAttribute precondition: