diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 6fd5d38ad..9a8cd97f2 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -348,6 +348,21 @@ namespace Discord.Commands public Task ExecuteAsync(ICommandContext context, int argPos, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) => ExecuteAsync(context, context.Message.Content.Substring(argPos), services, multiMatchHandling); + public Task ExecuteAsync(ICommandContext context, char suffix, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) + => ExecuteAsync(context, context.Message.Content.Trim(suffix), services, multiMatchHandling); + + public Task 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 ExecuteAsync(ICommandContext context, string input, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) { services = services ?? EmptyServiceProvider.Instance; diff --git a/src/Discord.Net.Commands/Extensions/MessageExtensions.cs b/src/Discord.Net.Commands/Extensions/MessageExtensions.cs index a27c5f322..fb9f95eb8 100644 --- a/src/Discord.Net.Commands/Extensions/MessageExtensions.cs +++ b/src/Discord.Net.Commands/Extensions/MessageExtensions.cs @@ -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; + } } } diff --git a/src/Discord.Net.Commands/SuffixType.cs b/src/Discord.Net.Commands/SuffixType.cs new file mode 100644 index 000000000..30a255ab0 --- /dev/null +++ b/src/Discord.Net.Commands/SuffixType.cs @@ -0,0 +1,9 @@ + +namespace Discord.Commands +{ + public enum SuffixType + { + String, + Mention + } +}