Skip to content

Commit 63f353c

Browse files
committed
unify removed command handling
1 parent d5f2d90 commit 63f353c

File tree

115 files changed

+753
-1978
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+753
-1978
lines changed

src/TaylorBot.Net/Core/src/TaylorBot.Net.Commands.Infrastructure/DisabledCommandPostgresRepository.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ public async ValueTask<string> InsertOrGetCommandDisabledMessageAsync(CommandMet
1212

1313
return await connection.QuerySingleAsync<string>(
1414
"""
15-
INSERT INTO commands.commands (name, aliases, module_name) VALUES (@CommandName, @Aliases, @ModuleName)
15+
INSERT INTO commands.commands (name, aliases, module_name) VALUES (@CommandName, @Aliases, '')
1616
ON CONFLICT (name) DO UPDATE SET
17-
aliases = excluded.aliases,
18-
module_name = excluded.module_name
17+
aliases = excluded.aliases
1918
RETURNING disabled_message;
2019
""",
2120
new
2221
{
2322
CommandName = command.Name,
2423
Aliases = command.Aliases ?? [],
25-
ModuleName = command.ModuleName ?? string.Empty,
2624
}
2725
);
2826
}

src/TaylorBot.Net/Core/src/TaylorBot.Net.Commands/CommandRunner.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using TaylorBot.Net.Core.Snowflake;
99
using TaylorBot.Net.Core.User;
1010
using TaylorBot.Net.EntityTracker.Domain.TextChannel;
11+
using static TaylorBot.Net.Commands.RunContext;
1112

1213
namespace TaylorBot.Net.Commands;
1314

@@ -27,14 +28,20 @@ public record RunContext(
2728
CommandGuild? Guild,
2829
GuildTextChannel? GuildTextChannel,
2930
IDiscordClient Client,
30-
RunContext.SlashCommandInfo? SlashCommand,
31-
RunContext.OnGoingState OnGoing,
31+
SlashCommandInfo? SlashCommand,
32+
OnGoingState OnGoing,
3233
CommandActivity Activity,
33-
bool WasAcknowledged = true
34+
bool WasAcknowledged = true,
35+
PrefixCommandInfo? PrefixCommand = null
3436
)
3537
{
3638
public record SlashCommandInfo(string Id, string Name);
3739

40+
public record PrefixCommandInfo(IReadOnlyList<string>? ReplacementSlashCommands, bool IsRemoved = false, string? RemovedMessage = null)
41+
{
42+
public PrefixCommandInfo(string? ReplacementSlashCommand = null, bool IsRemoved = false, string? RemovedMessage = null) : this(ReplacementSlashCommand != null ? [ReplacementSlashCommand] : null, IsRemoved, RemovedMessage) { }
43+
}
44+
3845
public class OnGoingState { public string? OnGoingCommandAddedToPool { get; set; } }
3946
}
4047

@@ -102,7 +109,7 @@ public RunContext BuildContext(ParsedInteraction interaction, CommandActivity ac
102109

103110
public record Command(CommandMetadata Metadata, Func<ValueTask<ICommandResult>> RunAsync, IList<ICommandPrecondition>? Preconditions = null);
104111

105-
public record CommandMetadata(string Name, string? ModuleName = null, IReadOnlyList<string>? Aliases = null, bool IsSlashCommand = true);
112+
public record CommandMetadata(string Name, IReadOnlyList<string>? Aliases = null, bool IsSlashCommand = true);
106113

107114
public interface ICommandRunner
108115
{
@@ -117,10 +124,11 @@ public class CommandRunner(
117124
UserNotIgnoredPrecondition userNotIgnored,
118125
MemberTrackedPrecondition memberTracked,
119126
TextChannelTrackedPrecondition textChannelTracked,
120-
UserNoOngoingCommandPrecondition userNoOngoingCommand
127+
UserNoOngoingCommandPrecondition userNoOngoingCommand,
128+
NotRemovedPrecondition notRemoved
121129
) : ICommandRunner
122130
{
123-
private readonly List<ICommandPrecondition> slashCommandsPreconditions = [notDisabled, notGuildDisabled, notGuildChannelDisabled, userNotIgnored, memberTracked, textChannelTracked, userNoOngoingCommand];
131+
private readonly List<ICommandPrecondition> slashCommandsPreconditions = [notDisabled, notGuildDisabled, notGuildChannelDisabled, userNotIgnored, memberTracked, textChannelTracked, userNoOngoingCommand, notRemoved];
124132

125133
public async ValueTask<ICommandResult> RunSlashCommandAsync(Command command, RunContext context)
126134
{

src/TaylorBot.Net/Core/src/TaylorBot.Net.Commands/DiscordNet/CommandExecutedHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ await context.Channel.SendMessageAsync(
9292
await context.Channel.SendMessageAsync(
9393
messageReference: new(context.Message.Id),
9494
allowedMentions: new AllowedMentions { MentionRepliedUser = false },
95-
embed: EmbedFactory.CreateError($"{context.User.Mention} {failed.UserReason.Reason}"));
95+
embed: EmbedFactory.CreateError(failed.UserReason.Reason));
9696
}
9797
break;
9898

@@ -131,7 +131,7 @@ await context.Channel.SendMessageAsync(
131131
// Preconditions have not been executed, we must make sure the user is not ignored.
132132
var runResult = await userNotIgnoredPrecondition.CanRunAsync(
133133
new Command(DiscordNetContextMapper.MapToCommandMetadata(commandContext), () => new()),
134-
DiscordNetContextMapper.MapToRunContext(commandContext)
134+
DiscordNetContextMapper.MapToRunContext(commandContext, new())
135135
);
136136

137137
if (runResult is not PreconditionFailed failed || !failed.UserReason.HideInPrefixCommands)

src/TaylorBot.Net/Core/src/TaylorBot.Net.Commands/DiscordNet/DiscordNetContextMapper.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using Discord;
22
using TaylorBot.Net.Core.Channel;
3+
using static TaylorBot.Net.Commands.RunContext;
34

45
namespace TaylorBot.Net.Commands.DiscordNet;
56

67
public static class DiscordNetContextMapper
78
{
8-
public static RunContext MapToRunContext(ITaylorBotCommandContext context)
9+
public static RunContext MapToRunContext(ITaylorBotCommandContext context, PrefixCommandInfo? prefixInfo)
910
{
1011
var channelType = context.Channel.GetChannelType();
1112
if (!channelType.HasValue)
@@ -25,9 +26,10 @@ public static RunContext MapToRunContext(ITaylorBotCommandContext context)
2526
guild,
2627
GuildTextChannel: guild != null ? new(channel.Id, guild.Id, channel.Type) : null,
2728
context.Client,
28-
new(string.Empty, string.Empty),
29-
new(),
30-
context.Activity.Value
29+
SlashCommand: null,
30+
OnGoing: new(),
31+
context.Activity.Value,
32+
PrefixCommand: prefixInfo ?? new()
3133
);
3234
context.RunContext = runContext;
3335
return runContext;
@@ -37,11 +39,11 @@ public static CommandMetadata MapToCommandMetadata(ITaylorBotCommandContext cont
3739
{
3840
if (context.IsTestEnv)
3941
{
40-
return new CommandMetadata(null!, IsSlashCommand: false);
42+
return new(null!, IsSlashCommand: false);
4143
}
4244

4345
var commandInfo = context.CommandInfos.OrderByDescending(c => c.Priority).First();
4446

45-
return new(commandInfo.Aliases[0], commandInfo.Module.Name, commandInfo.Aliases, IsSlashCommand: false);
47+
return new(commandInfo.Aliases[0], commandInfo.Aliases, IsSlashCommand: false);
4648
}
4749
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using static TaylorBot.Net.Commands.RunContext;
2+
3+
namespace TaylorBot.Net.Commands.DiscordNet;
4+
5+
public class PrefixedCommandRunner(ICommandRunner commandRunner)
6+
{
7+
public async Task<TaylorBotResult> RunAsync(
8+
ITaylorBotCommandContext context,
9+
PrefixCommandInfo commandInfo)
10+
{
11+
var command = new Command(
12+
DiscordNetContextMapper.MapToCommandMetadata(context),
13+
() => throw new InvalidOperationException());
14+
var runContext = DiscordNetContextMapper.MapToRunContext(context, commandInfo);
15+
16+
var result = await commandRunner.RunSlashCommandAsync(command, runContext);
17+
18+
return new TaylorBotResult(result, runContext);
19+
}
20+
}

src/TaylorBot.Net/Core/src/TaylorBot.Net.Commands/Events/ShardReadyHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ShardReadyHandler(SingletonTaskRunner commandMentionCacheRunner, IA
99
public Task ShardReadyAsync(DiscordSocketClient shardClient)
1010
{
1111
// Cache command ids for mentions
12-
_ = commandMentionCacheRunner.StartTaskIfNotStarted(
12+
_ = commandMentionCacheRunner.RunTaskIfNotRan(
1313
commandRepository.CacheCommandsAsync,
1414
nameof(IApplicationCommandsRepository.CacheCommandsAsync)
1515
);

src/TaylorBot.Net/Core/src/TaylorBot.Net.Commands/Extensions/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static IServiceCollection AddCommandApplication(this IServiceCollection s
6161
.AddTransient<IReactionAddedHandler>(c => c.GetRequiredService<PageMessageReactionsHandler>())
6262
.AddTransient<IReactionRemovedHandler>(c => c.GetRequiredService<PageMessageReactionsHandler>())
6363
.AddTransient<CommandExecutedHandler>()
64+
.AddTransient<PrefixedCommandRunner>()
6465
.AddTransient<CommandServiceLogger>()
6566
.AddTransient<IRateLimiter, CommandRateLimiter>()
6667
.AddTransient<IUserTracker, UserTracker>()
@@ -86,6 +87,7 @@ public static IServiceCollection AddCommandApplication(this IServiceCollection s
8687
.AddTransient<MemberTrackedPrecondition>()
8788
.AddTransient<TextChannelTrackedPrecondition>()
8889
.AddTransient<UserNoOngoingCommandPrecondition>()
90+
.AddTransient<NotRemovedPrecondition>()
8991
.AddTransient<UserHasPermissionOrOwnerPrecondition.Factory>()
9092
.AddTransient<TaylorBotHasPermissionPrecondition.Factory>()
9193
.AddTransient<TaylorBotOwnerPrecondition>()

src/TaylorBot.Net/Core/src/TaylorBot.Net.Commands/Preconditions/NotGuildChannelDisabledPrecondition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async ValueTask<ICommandResult> CanRunAsync(Command command, RunContext c
2828
$"""
2929
You can't use {mention.Command(command, context)} because it is disabled in {context.Channel.Mention} 🚫
3030
{(canRun is PreconditionPassed
31-
? $"You can re-enable it by typing </command channel-enable:909694280703016991> {command.Metadata.Name} ✅"
31+
? $"You can re-enable it by typing {mention.SlashCommand("command channel-enable")} {command.Metadata.Name} ✅"
3232
: "Ask a moderator to re-enable it 🙏")}
3333
"""
3434
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace TaylorBot.Net.Commands.Preconditions;
2+
3+
public class NotRemovedPrecondition(CommandMentioner mention) : ICommandPrecondition
4+
{
5+
public ValueTask<ICommandResult> CanRunAsync(Command command, RunContext context)
6+
{
7+
if (context.PrefixCommand?.IsRemoved == true)
8+
{
9+
var userReason = context.PrefixCommand.ReplacementSlashCommands != null ?
10+
context.PrefixCommand.ReplacementSlashCommands.Count > 1 ?
11+
$"""
12+
This command has been moved to:
13+
{string.Join('\n', context.PrefixCommand.ReplacementSlashCommands.Select(c => $"👉 {mention.SlashCommand(c)} 👈"))}
14+
Please use them instead! 😊
15+
""" :
16+
$"""
17+
This command has been moved to 👉 {mention.SlashCommand(context.PrefixCommand.ReplacementSlashCommands[0])} 👈
18+
Please use it instead! 😊
19+
""" :
20+
$"""
21+
This command has been removed, sorry! 😕
22+
{context.PrefixCommand.RemovedMessage}
23+
""";
24+
25+
return new(new PreconditionFailed(
26+
PrivateReason: $"{command.Metadata.Name} is removed",
27+
UserReason: new(userReason)));
28+
}
29+
return new(new PreconditionPassed());
30+
}
31+
}

src/TaylorBot.Net/Core/src/TaylorBot.Net.Commands/Preconditions/PlusPrecondition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ private async Task<bool> IsPlusGuildAsync(RunContext context)
7474
return context.Guild != null && await plusRepository.IsActivePlusGuildAsync(context.Guild);
7575
}
7676

77-
private static string PlusInfo()
77+
private string PlusInfo()
7878
{
7979
return
8080
$"""
8181
TaylorBot is free and {"supported by the community on Patreon".DiscordMdLink("https://www.patreon.com/taylorbot")} 💖
82-
Some features are exclusive to **TaylorBot Plus**, learn more with </plus show:1246970937321066608> 💎
82+
Some features are exclusive to **TaylorBot Plus**, learn more with {mention.SlashCommand("plus show")} 💎
8383
""";
8484
}
8585
}

0 commit comments

Comments
 (0)