Merge pull request #285 from FiniteReality/issue/264
Add PriorityAttribute and sort commands by priority in CommandService
This commit is contained in:
18
src/Discord.Net.Commands/Attributes/PriorityAttribute.cs
Normal file
18
src/Discord.Net.Commands/Attributes/PriorityAttribute.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Discord.Commands
|
||||||
|
{
|
||||||
|
/// <summary> Sets priority of commands </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public class PriorityAttribute : Attribute
|
||||||
|
{
|
||||||
|
/// <summary> The priority which has been set for the command </summary>
|
||||||
|
public int Priority { get; }
|
||||||
|
|
||||||
|
/// <summary> Creates a new <see cref="PriorityAttribute"/> with the given priority. </summary>
|
||||||
|
public PriorityAttribute(int priority)
|
||||||
|
{
|
||||||
|
Priority = priority;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ namespace Discord.Commands
|
|||||||
public string Summary { get; }
|
public string Summary { get; }
|
||||||
public string Remarks { get; }
|
public string Remarks { get; }
|
||||||
public string Text { get; }
|
public string Text { get; }
|
||||||
|
public int Priority { get; }
|
||||||
public bool HasVarArgs { get; }
|
public bool HasVarArgs { get; }
|
||||||
public IReadOnlyList<string> Aliases { get; }
|
public IReadOnlyList<string> Aliases { get; }
|
||||||
public IReadOnlyList<CommandParameter> Parameters { get; }
|
public IReadOnlyList<CommandParameter> Parameters { get; }
|
||||||
@@ -70,6 +71,9 @@ namespace Discord.Commands
|
|||||||
if (remarksAttr != null)
|
if (remarksAttr != null)
|
||||||
Remarks = remarksAttr.Text;
|
Remarks = remarksAttr.Text;
|
||||||
|
|
||||||
|
var priorityAttr = source.GetCustomAttribute<PriorityAttribute>();
|
||||||
|
Priority = priorityAttr?.Priority ?? 0;
|
||||||
|
|
||||||
Parameters = BuildParameters(source);
|
Parameters = BuildParameters(source);
|
||||||
HasVarArgs = Parameters.Count > 0 ? Parameters[Parameters.Count - 1].IsMultiple : false;
|
HasVarArgs = Parameters.Count > 0 ? Parameters[Parameters.Count - 1].IsMultiple : false;
|
||||||
Preconditions = BuildPreconditions(source);
|
Preconditions = BuildPreconditions(source);
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ namespace Discord.Commands
|
|||||||
public SearchResult Search(IUserMessage message, string input)
|
public SearchResult Search(IUserMessage message, string input)
|
||||||
{
|
{
|
||||||
string lowerInput = input.ToLowerInvariant();
|
string lowerInput = input.ToLowerInvariant();
|
||||||
var matches = _map.GetCommands(input).ToImmutableArray();
|
var matches = _map.GetCommands(input).OrderByDescending(x => x.Priority).ToImmutableArray();
|
||||||
|
|
||||||
if (matches.Length > 0)
|
if (matches.Length > 0)
|
||||||
return SearchResult.FromSuccess(input, matches);
|
return SearchResult.FromSuccess(input, matches);
|
||||||
|
|||||||
Reference in New Issue
Block a user