From 2a98aceae60ae90b0d70e2b848474626c5d5aaa9 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 2 Jul 2022 15:03:24 +0200 Subject: [PATCH] Fixed elipsis alias bug, closes #295 --- .../Utility/CommandMap/CommandMapService.cs | 97 +++++++++++++------ 1 file changed, 69 insertions(+), 28 deletions(-) diff --git a/src/NadekoBot/Modules/Utility/CommandMap/CommandMapService.cs b/src/NadekoBot/Modules/Utility/CommandMap/CommandMapService.cs index 054195e8c..f527a080c 100644 --- a/src/NadekoBot/Modules/Utility/CommandMap/CommandMapService.cs +++ b/src/NadekoBot/Modules/Utility/CommandMap/CommandMapService.cs @@ -27,7 +27,8 @@ public class CommandMapService : IInputTransformer, INService AliasMaps = new(configs.ToDictionary(x => x.GuildId, x => new ConcurrentDictionary(x.CommandAliases.DistinctBy(ca => ca.Trigger) - .ToDictionary(ca => ca.Trigger, ca => ca.Mapping)))); + .ToDictionary(ca => ca.Trigger, ca => ca.Mapping), + StringComparer.OrdinalIgnoreCase))); _db = db; } @@ -45,6 +46,7 @@ public class CommandMapService : IInputTransformer, INService return count; } + // todo bank take all public async Task TransformInput( IGuild guild, IMessageChannel channel, @@ -53,37 +55,76 @@ public class CommandMapService : IInputTransformer, INService { if (guild is null || string.IsNullOrWhiteSpace(input)) return null; - + if (AliasMaps.TryGetValue(guild.Id, out var maps)) { - var keys = maps.Keys.OrderByDescending(x => x.Length); - - foreach (var k in keys) + string word; + var index = input.IndexOf(' ', StringComparison.InvariantCulture); + if (index == -1) { - string newInput; - if (input.StartsWith(k + " ", StringComparison.InvariantCultureIgnoreCase)) - newInput = maps[k] + input.Substring(k.Length, input.Length - k.Length); - else if (input.Equals(k, StringComparison.InvariantCultureIgnoreCase)) - newInput = maps[k]; - else - continue; - - try - { - var toDelete = await channel.SendConfirmAsync(_eb, $"{input} => {newInput}"); - _ = Task.Run(async () => - { - await Task.Delay(1500); - await toDelete.DeleteAsync(new() - { - RetryMode = RetryMode.AlwaysRetry - }); - }); - } - catch { } - - return newInput; + word = input; } + else + { + word = input[..index]; + } + + string newInput; + if (maps.TryGetValue(word, out var alias)) + { + if (index == -1) + newInput = alias; + else + newInput = alias + ' ' + input[index..]; + } + else + { + return null; + } + + try + { + var toDelete = await channel.SendConfirmAsync(_eb, $"{input} => {newInput}"); + _ = Task.Run(async () => + { + await Task.Delay(1500); + await toDelete.DeleteAsync(new() + { + RetryMode = RetryMode.AlwaysRetry + }); + }); + } + catch { } + + return newInput; + + // var keys = maps.Keys.OrderByDescending(x => x.Length); + // foreach (var k in keys) + // { + // string newInput; + // if (input.StartsWith(k + " ", StringComparison.InvariantCultureIgnoreCase)) + // newInput = maps[k] + input.Substring(k.Length, input.Length - k.Length); + // else if (input.Equals(k, StringComparison.InvariantCultureIgnoreCase)) + // newInput = maps[k]; + // else + // continue; + // + // try + // { + // var toDelete = await channel.SendConfirmAsync(_eb, $"{input} => {newInput}"); + // _ = Task.Run(async () => + // { + // await Task.Delay(1500); + // await toDelete.DeleteAsync(new() + // { + // RetryMode = RetryMode.AlwaysRetry + // }); + // }); + // } + // catch { } + // + // return newInput; + // } } return null;