Browse Source

Add PriorityAttribute and sortby priority in Search

tags/1.0-rc
FiniteReality 8 years ago
parent
commit
accb3e27b8
3 changed files with 23 additions and 1 deletions
  1. +18
    -0
      src/Discord.Net.Commands/Attributes/PriorityAttribute.cs
  2. +4
    -0
      src/Discord.Net.Commands/Command.cs
  3. +1
    -1
      src/Discord.Net.Commands/CommandService.cs

+ 18
- 0
src/Discord.Net.Commands/Attributes/PriorityAttribute.cs View 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;
}
}
}

+ 4
- 0
src/Discord.Net.Commands/Command.cs View File

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


+ 1
- 1
src/Discord.Net.Commands/CommandService.cs View File

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


Loading…
Cancel
Save