Skip to content

Commit d8a6456

Browse files
committed
feat: add /anonmacro command
1 parent be6a8fd commit d8a6456

1 file changed

Lines changed: 51 additions & 13 deletions

File tree

Marco/Commands/MacroCommand.cs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,39 @@ public MacroCommand(MacroService macroService, MacroCooldownService cooldownServ
2323
_cooldownService = cooldownService;
2424
}
2525

26-
[SlashCommand("macro", "Executes a macro.")]
26+
[SlashCommand("anonmacro", "Executes an macro anonymously, hiding the invocation.", false)]
2727
[SlashRequireGuild]
28-
public async Task MacroAsync(InteractionContext context,
29-
[Option("macro", "The name of the macro.", true)] [Autocomplete(typeof(MacroAutocompleteProvider))]
30-
string macroName)
28+
public async Task AnonymousMacroAsync(InteractionContext context,
29+
[Option("macro", "The name of the macro.", true)] [Autocomplete(typeof(MacroAutocompleteProvider))] string macroName)
3130
{
32-
if (!_macroService.TryGetMacro(context.Guild, macroName, out Macro? macro))
31+
Macro? macro = await VerifyMacroAsync(context, macroName);
32+
if (macro is null)
3333
{
34-
await context.CreateResponseAsync($"The macro `{macroName}` doesn't exist.", true).ConfigureAwait(false);
3534
return;
3635
}
3736

38-
DiscordChannel channel = context.Channel;
37+
var builder = new DiscordMessageBuilder();
38+
string response = macro.Response;
3939

40-
if (macro.ChannelId.HasValue && macro.ChannelId.Value != channel.Id)
40+
if (response.StartsWith("@silent "))
4141
{
42-
await context.CreateResponseAsync($"The macro `{macroName}` cannot be executed here.", true).ConfigureAwait(false);
43-
return;
42+
response = response[8..];
43+
builder.SuppressNotifications();
4444
}
45-
if (_cooldownService.IsOnCooldown(channel, macro))
45+
46+
builder.WithContent(response);
47+
await context.CreateResponseAsync("The macro has been sent.", true).ConfigureAwait(false);
48+
await context.Channel.SendMessageAsync(builder).ConfigureAwait(false);
49+
}
50+
51+
[SlashCommand("macro", "Executes a macro.")]
52+
[SlashRequireGuild]
53+
public async Task MacroAsync(InteractionContext context,
54+
[Option("macro", "The name of the macro.", true)] [Autocomplete(typeof(MacroAutocompleteProvider))] string macroName)
55+
{
56+
Macro? macro = await VerifyMacroAsync(context, macroName);
57+
if (macro is null)
4658
{
47-
await context.CreateResponseAsync($"The macro `{macroName}` is on cooldown because it was very recently executed.", true).ConfigureAwait(false);
4859
return;
4960
}
5061

@@ -59,6 +70,33 @@ public async Task MacroAsync(InteractionContext context,
5970

6071
builder.WithContent(response);
6172
await context.CreateResponseAsync(builder).ConfigureAwait(false);
62-
_cooldownService.UpdateCooldown(channel, macro);
73+
_cooldownService.UpdateCooldown(context.Channel, macro);
74+
}
75+
76+
private async Task<Macro?> VerifyMacroAsync(InteractionContext context, string macroName)
77+
{
78+
if (!_macroService.TryGetMacro(context.Guild, macroName, out Macro? macro))
79+
{
80+
await context.CreateResponseAsync($"The macro `{macroName}` doesn't exist.", true).ConfigureAwait(false);
81+
return null;
82+
}
83+
84+
DiscordChannel channel = context.Channel;
85+
86+
if (macro.ChannelId.HasValue && macro.ChannelId.Value != channel.Id)
87+
{
88+
await context.CreateResponseAsync($"The macro `{macroName}` cannot be executed here.", true).ConfigureAwait(false);
89+
return null;
90+
}
91+
92+
if (_cooldownService.IsOnCooldown(channel, macro))
93+
{
94+
await context
95+
.CreateResponseAsync($"The macro `{macroName}` is on cooldown because it was very recently executed.", true)
96+
.ConfigureAwait(false);
97+
return null;
98+
}
99+
100+
return macro;
63101
}
64102
}

0 commit comments

Comments
 (0)