Fixed elipsis alias bug, closes #295

This commit is contained in:
Kwoth
2022-07-02 15:03:24 +02:00
parent 5c933b676d
commit 2a98aceae6

View File

@@ -27,7 +27,8 @@ public class CommandMapService : IInputTransformer, INService
AliasMaps = new(configs.ToDictionary(x => x.GuildId,
x => new ConcurrentDictionary<string, string>(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<string> TransformInput(
IGuild guild,
IMessageChannel channel,
@@ -56,34 +58,73 @@ public class CommandMapService : IInputTransformer, INService
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;