Skip to content

Commit 4a47f4f

Browse files
committed
fix: don't allow registration of duplicate macros/aliases
1 parent 82b5ad6 commit 4a47f4f

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

Marco/Commands/AddMacroCommand.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public async Task AddMacro(
4646

4747
name = Regex.Replace(name.ToLowerInvariant(), "\\s", string.Empty, RegexOptions.Compiled);
4848

49+
if (_macroService.TryGetMacro(guild, name, out _))
50+
{
51+
embed.WithColor(DiscordColor.Red);
52+
embed.WithTitle("Cannot add macro");
53+
embed.WithDescription($"A macro with the name or alias `{name}` already exists.");
54+
await context.CreateResponseAsync(embed, true).ConfigureAwait(false);
55+
return;
56+
}
57+
4958
var modal = new DiscordModalBuilder(context.Client);
5059
modal.WithTitle($"Add macro '{name}'");
5160
DiscordModalTextInput aliasesInput =
@@ -57,11 +66,33 @@ public async Task AddMacro(
5766
if (response != DiscordModalResponse.Success)
5867
return;
5968

69+
var aliases = new List<string>(aliasesInput.Value?.Split() ?? ArraySegment<string>.Empty);
70+
var invalidAliases = new List<string>();
71+
72+
if (aliases.Count > 0)
73+
{
74+
for (int index = aliases.Count - 1; index >= 0; index--)
75+
{
76+
string current = aliases[index];
77+
if (_macroService.TryGetMacro(guild, current, out _))
78+
{
79+
aliases.RemoveAt(index);
80+
invalidAliases.Add(current);
81+
}
82+
}
83+
}
84+
6085
Macro macro = await _macroService
61-
.CreateMacroAsync(guild, null, name, responseInput.Value!, aliasesInput.Value?.Split()).ConfigureAwait(false);
86+
.CreateMacroAsync(guild, null, name, responseInput.Value!, aliases.ToArray()).ConfigureAwait(false);
6287
embed.WithColor(DiscordColor.Green);
6388
embed.WithTitle("Macro added");
6489
embed.WithDescription($"The macro `{macro.Name}` has been added.");
90+
if (invalidAliases.Count > 0)
91+
{
92+
embed.Description += "\n\n⚠️ The following aliases were not added because they already exist: " +
93+
string.Join(", ", invalidAliases.Select(a => $"`{a}`"));
94+
}
95+
6596
embed.AddField("Type", "Global", true);
6697
embed.AddFieldIf(macro.Aliases.Count > 0, "Alias".ToQuantity(macro.Aliases.Count), string.Join(' ', macro.Aliases), true);
6798

0 commit comments

Comments
 (0)