From 4976fc4b67a64898cd7e236b31c1769e3a7c5920 Mon Sep 17 00:00:00 2001 From: JustNrik <35231903+JustNrik@users.noreply.github.com> Date: Wed, 22 Aug 2018 13:19:49 -0400 Subject: [PATCH 1/6] Create SuffixType.cs --- src/Discord.Net.Commands/SuffixType.cs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/Discord.Net.Commands/SuffixType.cs 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 + } +} From 160552f7a3f2825b1f81d5d8463de20461db54d4 Mon Sep 17 00:00:00 2001 From: JustNrik <35231903+JustNrik@users.noreply.github.com> Date: Wed, 22 Aug 2018 13:22:12 -0400 Subject: [PATCH 2/6] Update CommandService.cs --- src/Discord.Net.Commands/CommandService.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 6fd5d38ad..64b6f8b1e 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.IndexOf(suffix) - 1), services, multiMatchHandling); + case SuffixType.Mention: + return ExecuteAsync(context, text.Remove(text.IndexOf(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; From a2b88b51bfe2bca883599dab1f7ef353f6214445 Mon Sep 17 00:00:00 2001 From: JustNrik <35231903+JustNrik@users.noreply.github.com> Date: Wed, 22 Aug 2018 13:23:38 -0400 Subject: [PATCH 3/6] Update MessageExtensions.cs --- .../Extensions/MessageExtensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Discord.Net.Commands/Extensions/MessageExtensions.cs b/src/Discord.Net.Commands/Extensions/MessageExtensions.cs index a27c5f322..49bcccbd8 100644 --- a/src/Discord.Net.Commands/Extensions/MessageExtensions.cs +++ b/src/Discord.Net.Commands/Extensions/MessageExtensions.cs @@ -42,5 +42,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.IndexOf('<'); + 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; + } } } From 62238d494ea56f8dcf42166abf114ea6c063be8d Mon Sep 17 00:00:00 2001 From: JustNrik <35231903+JustNrik@users.noreply.github.com> Date: Wed, 22 Aug 2018 14:13:06 -0400 Subject: [PATCH 4/6] Update MessageExtensions.cs --- src/Discord.Net.Commands/Extensions/MessageExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.Commands/Extensions/MessageExtensions.cs b/src/Discord.Net.Commands/Extensions/MessageExtensions.cs index 49bcccbd8..617078953 100644 --- a/src/Discord.Net.Commands/Extensions/MessageExtensions.cs +++ b/src/Discord.Net.Commands/Extensions/MessageExtensions.cs @@ -52,7 +52,7 @@ namespace Discord.Commands var text = msg.Content; if (text.Length <= 3 || text[text.Length - 1] != '>') return false; - int iniPos = text.IndexOf('<'); + 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; From 59f537f1a4e7fcf074c9ddb5779e2bb22657e047 Mon Sep 17 00:00:00 2001 From: JustNrik <35231903+JustNrik@users.noreply.github.com> Date: Wed, 22 Aug 2018 14:23:22 -0400 Subject: [PATCH 5/6] Update MessageExtensions.cs IndexOf => LastIndexOf --- src/Discord.Net.Commands/Extensions/MessageExtensions.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Discord.Net.Commands/Extensions/MessageExtensions.cs b/src/Discord.Net.Commands/Extensions/MessageExtensions.cs index 617078953..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; From b8f87990356b819a12ec5f4c1c8bf2d1bee4350c Mon Sep 17 00:00:00 2001 From: JustNrik <35231903+JustNrik@users.noreply.github.com> Date: Wed, 22 Aug 2018 14:25:09 -0400 Subject: [PATCH 6/6] Update CommandService.cs IndexOf => LastIndexOf --- src/Discord.Net.Commands/CommandService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 64b6f8b1e..9a8cd97f2 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -357,9 +357,9 @@ namespace Discord.Commands switch (type) { case SuffixType.String: - return ExecuteAsync(context, text.Remove(text.IndexOf(suffix) - 1), services, multiMatchHandling); + return ExecuteAsync(context, text.Remove(text.LastIndexOf(suffix) - 1), services, multiMatchHandling); case SuffixType.Mention: - return ExecuteAsync(context, text.Remove(text.IndexOf(context.Client.CurrentUser.Mention) - 1), services, multiMatchHandling); + return ExecuteAsync(context, text.Remove(text.LastIndexOf(context.Client.CurrentUser.Mention) - 1), services, multiMatchHandling); } return ExecuteAsync(context, text, services, multiMatchHandling); }