Browse Source

Merge b8f8799035 into db90eab953

pull/1130/merge
JustNrik GitHub 7 years ago
parent
commit
247be30c7e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 2 deletions
  1. +15
    -0
      src/Discord.Net.Commands/CommandService.cs
  2. +18
    -2
      src/Discord.Net.Commands/Extensions/MessageExtensions.cs
  3. +9
    -0
      src/Discord.Net.Commands/SuffixType.cs

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

@@ -348,6 +348,21 @@ namespace Discord.Commands

public Task<IResult> ExecuteAsync(ICommandContext context, int argPos, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
=> ExecuteAsync(context, context.Message.Content.Substring(argPos), services, multiMatchHandling);
public Task<IResult> ExecuteAsync(ICommandContext context, char suffix, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
=> ExecuteAsync(context, context.Message.Content.Trim(suffix), services, multiMatchHandling);
public Task<IResult> ExecuteAsync(ICommandContext context, string suffix, IServiceProvider services, SuffixType type, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
{
var text = context.Message.Content;
switch (type)
{
case SuffixType.String:
return ExecuteAsync(context, text.Remove(text.LastIndexOf(suffix) - 1), services, multiMatchHandling);
case SuffixType.Mention:
return ExecuteAsync(context, text.Remove(text.LastIndexOf(context.Client.CurrentUser.Mention) - 1), services, multiMatchHandling);
}
return ExecuteAsync(context, text, services, multiMatchHandling);
}
public async Task<IResult> ExecuteAsync(ICommandContext context, string input, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
{
services = services ?? EmptyServiceProvider.Instance;


+ 18
- 2
src/Discord.Net.Commands/Extensions/MessageExtensions.cs View File

@@ -33,8 +33,7 @@ namespace Discord.Commands
if (endPos == -1) return false;
if (text.Length < endPos + 2 || text[endPos + 1] != ' ') return false; //Must end in "> "

ulong userId;
if (!MentionUtils.TryParseUser(text.Substring(0, endPos + 1), out userId)) return false;
if (!MentionUtils.TryParseUser(text.Substring(0, endPos + 1), out ulong userId)) return false;
if (userId == user.Id)
{
argPos = endPos + 2;
@@ -42,5 +41,22 @@ namespace Discord.Commands
}
return false;
}
public static bool HasCharSuffix(this IUserMessage msg, char c)
=> msg.Content.Length > 0 && msg.Content[msg.Content.Length - 1] == c;
public static bool HasStringSuffix(this IUserMessage msg, string str, StringComparison comparisonType = StringComparison.Ordinal)
=> msg.Content.EndsWith(str, comparisonType);
public static bool HasMentionSuffix(this IUserMessage msg, IUser user)
{
var text = msg.Content;
if (text.Length <= 3 || text[text.Length - 1] != '>') return false;

int iniPos = text.LastIndexOf('<');
if (iniPos == -1) return false;
if (!MentionUtils.TryParseUser(text.Substring(iniPos, text.Length - iniPos), out ulong userId)) return false;
if (user.Id == userId) return true;

return false;
}
}
}

+ 9
- 0
src/Discord.Net.Commands/SuffixType.cs View File

@@ -0,0 +1,9 @@

namespace Discord.Commands
{
public enum SuffixType
{
String,
Mention
}
}

Loading…
Cancel
Save