Skip to content

Commit

Permalink
Merge branch 'v2.1.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed May 24, 2023
2 parents fb81cb2 + 4a47f4f commit 9ffb608
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Marco/AutocompleteProviders/MacroAutocompleteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext
continue;
}

if (hasOptionValue && !macro.Name.Contains(optionValue))
if (hasOptionValue && !macro.Name.Contains(optionValue) && !macro.Aliases.Contains(optionValue))
{
continue;
}
Expand Down
33 changes: 32 additions & 1 deletion Marco/Commands/AddMacroCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public async Task AddMacro(

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

if (_macroService.TryGetMacro(guild, name, out _))
{
embed.WithColor(DiscordColor.Red);
embed.WithTitle("Cannot add macro");
embed.WithDescription($"A macro with the name or alias `{name}` already exists.");
await context.CreateResponseAsync(embed, true).ConfigureAwait(false);
return;
}

var modal = new DiscordModalBuilder(context.Client);
modal.WithTitle($"Add macro '{name}'");
DiscordModalTextInput aliasesInput =
Expand All @@ -57,11 +66,33 @@ public async Task AddMacro(
if (response != DiscordModalResponse.Success)
return;

var aliases = new List<string>(aliasesInput.Value?.Split() ?? ArraySegment<string>.Empty);
var invalidAliases = new List<string>();

if (aliases.Count > 0)
{
for (int index = aliases.Count - 1; index >= 0; index--)
{
string current = aliases[index];
if (_macroService.TryGetMacro(guild, current, out _))
{
aliases.RemoveAt(index);
invalidAliases.Add(current);
}
}
}

Macro macro = await _macroService
.CreateMacroAsync(guild, null, name, responseInput.Value!, aliasesInput.Value?.Split()).ConfigureAwait(false);
.CreateMacroAsync(guild, null, name, responseInput.Value!, aliases.ToArray()).ConfigureAwait(false);
embed.WithColor(DiscordColor.Green);
embed.WithTitle("Macro added");
embed.WithDescription($"The macro `{macro.Name}` has been added.");
if (invalidAliases.Count > 0)
{
embed.Description += "\n\n⚠️ The following aliases were not added because they already exist: " +
string.Join(", ", invalidAliases.Select(a => $"`{a}`"));
}

embed.AddField("Type", "Global", true);
embed.AddFieldIf(macro.Aliases.Count > 0, "Alias".ToQuantity(macro.Aliases.Count), string.Join(' ', macro.Aliases), true);

Expand Down
2 changes: 1 addition & 1 deletion Marco/Commands/MacroCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task MacroAsync(InteractionContext context,
{
if (!_macroService.TryGetMacro(context.Guild, macroName, out Macro? macro))
{
await context.CreateResponseAsync($"The macro `{macroName}` doesn't exist", true).ConfigureAwait(false);
await context.CreateResponseAsync($"The macro `{macroName}` doesn't exist.", true).ConfigureAwait(false);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Marco/Marco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
</PropertyGroup>

<PropertyGroup Condition="'$(VersionSuffix)' != '' And '$(BuildNumber)' == ''">
Expand Down
6 changes: 4 additions & 2 deletions Marco/Services/MacroService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ public async Task<Macro> EditMacroAsync(DiscordGuild guild, string name, Action<
public IReadOnlyCollection<Macro> GetMacros(DiscordGuild guild)
{
ArgumentNullException.ThrowIfNull(guild);
return _macros.TryGetValue(guild, out Dictionary<string, Macro>? macros) ? macros.Values : ArraySegment<Macro>.Empty;
return _macros.TryGetValue(guild, out Dictionary<string, Macro>? macros)
? macros.Values.Distinct().ToArray()
: ArraySegment<Macro>.Empty;
}

/// <summary>
Expand All @@ -195,7 +197,7 @@ public IReadOnlyCollection<Macro> GetMacros(DiscordChannel channel)
ArgumentNullException.ThrowIfNull(channel);
if (channel.Guild is not { } guild) throw new ArgumentException("Channel must be in a guild", nameof(channel));
return _macros.TryGetValue(guild, out Dictionary<string, Macro>? macros)
? macros.Values.Where(m => m.ChannelId == channel.Id).ToArray()
? macros.Values.Where(m => m.ChannelId == channel.Id).Distinct().ToArray()
: ArraySegment<Macro>.Empty;
}

Expand Down

0 comments on commit 9ffb608

Please sign in to comment.