From 7ce8d7b53145b35da0a6f89e2b1b8f8455603883 Mon Sep 17 00:00:00 2001 From: bivashy Date: Fri, 13 Oct 2023 02:15:29 +0600 Subject: [PATCH] Apply @CommandCooldown to all database-related commands --- .../mastercapexd/auth/messenger/commands/AccountCommand.java | 2 ++ .../auth/messenger/commands/AccountsListCommand.java | 2 ++ .../auth/messenger/commands/ChangePasswordCommand.java | 2 ++ .../auth/messenger/commands/ConfirmationToggleCommand.java | 4 ++++ .../mastercapexd/auth/messenger/commands/GoogleCommand.java | 2 ++ .../auth/messenger/commands/GoogleUnlinkCommand.java | 2 ++ .../mastercapexd/auth/messenger/commands/RestoreCommand.java | 2 ++ .../auth/server/commands/ChangePasswordCommand.java | 2 +- .../me/mastercapexd/auth/server/commands/GoogleCommand.java | 2 ++ .../auth/server/commands/GoogleUnlinkCommand.java | 2 ++ .../mastercapexd/auth/shared/commands/DiscordLinkCommand.java | 2 ++ .../me/mastercapexd/auth/shared/commands/LinkCodeCommand.java | 2 ++ .../auth/shared/commands/TelegramLinkCommand.java | 2 ++ .../me/mastercapexd/auth/shared/commands/VKLinkCommand.java | 2 ++ .../auth/shared/commands/annotation/CommandCooldown.java | 3 +++ 15 files changed, 32 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountCommand.java b/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountCommand.java index 5df1eca9..d0d48cc4 100644 --- a/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountCommand.java @@ -6,6 +6,7 @@ import me.mastercapexd.auth.link.LinkCommandActorWrapper; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.orphan.OrphanCommand; @@ -14,6 +15,7 @@ public class AccountCommand implements OrphanCommand { public static final String CONFIGURATION_KEY = "account-control"; @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void accountMenu(LinkCommandActorWrapper actorWrapper, LinkType linkType, Account account) { Keyboard accountKeyboard = linkType.getSettings().getKeyboards().createKeyboard("account", "%account_name%", account.getName()); actorWrapper.send(linkType.newMessageBuilder(linkType.getLinkMessages().getMessage("account-control", linkType.newMessageContext(account))) diff --git a/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountsListCommand.java b/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountsListCommand.java index 3134a63d..40defb22 100644 --- a/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountsListCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountsListCommand.java @@ -16,6 +16,7 @@ import me.mastercapexd.auth.discord.command.annotation.RenameTo; import me.mastercapexd.auth.link.LinkCommandActorWrapper; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.Default; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; @@ -32,6 +33,7 @@ public class AccountsListCommand implements OrphanCommand { private LinkType linkType; @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void onAccountsMenu(LinkCommandActorWrapper actorWrapper, LinkType linkType, @Flag("page") @Default("1") Integer page, @RenameTo(value = "size", type = "NUMBER") @Flag("pagesize") @Default("5") Integer accountsPerPage, @Flag("type") @Default("my") AccountListType type) { diff --git a/core/src/main/java/me/mastercapexd/auth/messenger/commands/ChangePasswordCommand.java b/core/src/main/java/me/mastercapexd/auth/messenger/commands/ChangePasswordCommand.java index 8ef278cd..827e97ef 100644 --- a/core/src/main/java/me/mastercapexd/auth/messenger/commands/ChangePasswordCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/messenger/commands/ChangePasswordCommand.java @@ -11,6 +11,7 @@ import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.messenger.commands.annotation.ConfigurationArgumentError; import me.mastercapexd.auth.server.commands.parameters.NewPassword; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.orphan.OrphanCommand; @@ -25,6 +26,7 @@ public class ChangePasswordCommand implements OrphanCommand { @ConfigurationArgumentError("changepass-not-enough-arguments") @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void onPasswordChange(LinkCommandActorWrapper actorWrapper, LinkType linkType, Account account, NewPassword newPassword) { plugin.getEventBus().publish(AccountTryChangePasswordEvent.class, account, false, true).thenAccept(tryChangePasswordEventPostResult -> { if (tryChangePasswordEventPostResult.getEvent().isCancelled()) diff --git a/core/src/main/java/me/mastercapexd/auth/messenger/commands/ConfirmationToggleCommand.java b/core/src/main/java/me/mastercapexd/auth/messenger/commands/ConfirmationToggleCommand.java index 5ab09ef3..03f63578 100644 --- a/core/src/main/java/me/mastercapexd/auth/messenger/commands/ConfirmationToggleCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/messenger/commands/ConfirmationToggleCommand.java @@ -11,12 +11,14 @@ import me.mastercapexd.auth.link.LinkCommandActorWrapper; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.messenger.commands.annotation.ConfigurationArgumentError; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.orphan.OrphanCommand; @CommandKey(ConfirmationToggleCommand.CONFIGURATION_KEY) public class ConfirmationToggleCommand implements OrphanCommand { + public static final String CONFIGURATION_KEY = "confirmation-toggle"; @Dependency private AccountDatabase accountDatabase; @@ -25,6 +27,7 @@ public class ConfirmationToggleCommand implements OrphanCommand { @ConfigurationArgumentError("confirmation-no-player") @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void onConfirmationToggle(LinkCommandActorWrapper actorWrapper, LinkType linkType, Account account) { if (!linkType.getSettings().getConfirmationSettings().canToggleConfirmation()) { actorWrapper.reply(linkType.getLinkMessages().getMessage("confirmation-toggle-disabled", linkType.newMessageContext(account))); @@ -40,4 +43,5 @@ public void onConfirmationToggle(LinkCommandActorWrapper actorWrapper, LinkType accountDatabase.saveOrUpdateAccount(account); }); } + } diff --git a/core/src/main/java/me/mastercapexd/auth/messenger/commands/GoogleCommand.java b/core/src/main/java/me/mastercapexd/auth/messenger/commands/GoogleCommand.java index 2f72dc71..9439ea94 100644 --- a/core/src/main/java/me/mastercapexd/auth/messenger/commands/GoogleCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/messenger/commands/GoogleCommand.java @@ -24,6 +24,7 @@ import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.messenger.commands.annotation.ConfigurationArgumentError; import me.mastercapexd.auth.server.commands.annotations.GoogleUse; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import me.mastercapexd.auth.util.GoogleAuthenticatorQRGenerator; import me.mastercapexd.auth.util.RandomCodeFactory; import revxrsal.commands.annotation.DefaultFor; @@ -43,6 +44,7 @@ public class GoogleCommand implements OrphanCommand { @GoogleUse @ConfigurationArgumentError("google-not-enough-arguments") @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void linkGoogle(LinkCommandActorWrapper actorWrapper, LinkType linkType, Account account) { String rawKey = plugin.getGoogleAuthenticator().createCredentials().getKey(); String nickname = account.getName(); diff --git a/core/src/main/java/me/mastercapexd/auth/messenger/commands/GoogleUnlinkCommand.java b/core/src/main/java/me/mastercapexd/auth/messenger/commands/GoogleUnlinkCommand.java index bef3fdb8..65c8ba83 100644 --- a/core/src/main/java/me/mastercapexd/auth/messenger/commands/GoogleUnlinkCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/messenger/commands/GoogleUnlinkCommand.java @@ -13,6 +13,7 @@ import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.messenger.commands.annotation.ConfigurationArgumentError; import me.mastercapexd.auth.server.commands.annotations.GoogleUse; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.orphan.OrphanCommand; @@ -30,6 +31,7 @@ public class GoogleUnlinkCommand implements OrphanCommand { @GoogleUse @ConfigurationArgumentError("google-unlink-not-enough-arguments") @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void unlink(LinkCommandActorWrapper actorWrapper, LinkType linkType, Account account) { LinkUser linkUser = account.findFirstLinkUserOrNew(GoogleLinkType.LINK_USER_FILTER, GoogleLinkType.getInstance()); diff --git a/core/src/main/java/me/mastercapexd/auth/messenger/commands/RestoreCommand.java b/core/src/main/java/me/mastercapexd/auth/messenger/commands/RestoreCommand.java index a758370e..73eca4e1 100644 --- a/core/src/main/java/me/mastercapexd/auth/messenger/commands/RestoreCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/messenger/commands/RestoreCommand.java @@ -9,6 +9,7 @@ import me.mastercapexd.auth.link.LinkCommandActorWrapper; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.messenger.commands.annotation.ConfigurationArgumentError; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.orphan.OrphanCommand; @@ -23,6 +24,7 @@ public class RestoreCommand implements OrphanCommand { @ConfigurationArgumentError("restore-not-enough-arguments") @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void onRestore(LinkCommandActorWrapper actorWrapper, LinkType linkType, Account account) { String generatedPassword = linkType.getSettings().getRestoreSettings().generateCode(); account.setPasswordHash(account.getCryptoProvider().hash(HashInput.of(generatedPassword))); diff --git a/core/src/main/java/me/mastercapexd/auth/server/commands/ChangePasswordCommand.java b/core/src/main/java/me/mastercapexd/auth/server/commands/ChangePasswordCommand.java index 2c156fdf..70edc373 100644 --- a/core/src/main/java/me/mastercapexd/auth/server/commands/ChangePasswordCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/server/commands/ChangePasswordCommand.java @@ -25,7 +25,7 @@ public class ChangePasswordCommand { private AccountDatabase accountStorage; @DefaultFor({"passchange", "changepass", "changepassword"}) - @CommandCooldown(5000) + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void changePlayerPassword(ServerPlayer sender, DoublePassword password) { String id = config.getActiveIdentifierType().getId(sender); accountStorage.getAccount(id).thenAcceptAsync(account -> { diff --git a/core/src/main/java/me/mastercapexd/auth/server/commands/GoogleCommand.java b/core/src/main/java/me/mastercapexd/auth/server/commands/GoogleCommand.java index db5918e3..28a7fc92 100644 --- a/core/src/main/java/me/mastercapexd/auth/server/commands/GoogleCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/server/commands/GoogleCommand.java @@ -15,6 +15,7 @@ import me.mastercapexd.auth.link.google.GoogleLinkType; import me.mastercapexd.auth.server.commands.annotations.GoogleUse; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.Command; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; @@ -31,6 +32,7 @@ public class GoogleCommand { @GoogleUse @DefaultFor("google") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void linkGoogle(ServerPlayer player) { String id = config.getActiveIdentifierType().getId(player); accountStorage.getAccount(id).thenAccept(account -> { diff --git a/core/src/main/java/me/mastercapexd/auth/server/commands/GoogleUnlinkCommand.java b/core/src/main/java/me/mastercapexd/auth/server/commands/GoogleUnlinkCommand.java index 12baecbc..cdcafa59 100644 --- a/core/src/main/java/me/mastercapexd/auth/server/commands/GoogleUnlinkCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/server/commands/GoogleUnlinkCommand.java @@ -13,6 +13,7 @@ import io.github.revxrsal.eventbus.EventBus; import me.mastercapexd.auth.link.google.GoogleLinkType; import me.mastercapexd.auth.server.commands.annotations.GoogleUse; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.Command; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; @@ -29,6 +30,7 @@ public class GoogleUnlinkCommand { @GoogleUse @DefaultFor({"googleunlink", "google unlink", "gunlink"}) + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void unlink(MessageableCommandActor actor, ServerPlayer player) { String id = config.getActiveIdentifierType().getId(player); accountStorage.getAccount(id).thenAccept(account -> { diff --git a/core/src/main/java/me/mastercapexd/auth/shared/commands/DiscordLinkCommand.java b/core/src/main/java/me/mastercapexd/auth/shared/commands/DiscordLinkCommand.java index 1d1dae16..575d947c 100644 --- a/core/src/main/java/me/mastercapexd/auth/shared/commands/DiscordLinkCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/shared/commands/DiscordLinkCommand.java @@ -13,6 +13,7 @@ import me.mastercapexd.auth.link.user.confirmation.BaseLinkConfirmationUser; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.server.commands.annotations.DiscordUse; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.annotation.Optional; @@ -33,6 +34,7 @@ public DiscordLinkCommand(Messages messages) { @DiscordUse @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void discordLink(MessageableCommandActor commandActor, PlayerIdSupplier idSupplier, @Optional LinkUserIdentificator linkUserIdentificator) { String accountId = idSupplier.getPlayerId(); diff --git a/core/src/main/java/me/mastercapexd/auth/shared/commands/LinkCodeCommand.java b/core/src/main/java/me/mastercapexd/auth/shared/commands/LinkCodeCommand.java index 23fd733e..89085140 100644 --- a/core/src/main/java/me/mastercapexd/auth/shared/commands/LinkCodeCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/shared/commands/LinkCodeCommand.java @@ -17,6 +17,7 @@ import me.mastercapexd.auth.link.LinkCommandActorWrapper; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.messenger.commands.annotation.ConfigurationArgumentError; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import me.mastercapexd.auth.shared.commands.parameter.MessengerLinkContext; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; @@ -37,6 +38,7 @@ public class LinkCodeCommand implements OrphanCommand { @ConfigurationArgumentError("confirmation-not-enough-arguments") @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void onLink(MessageableCommandActor actor, MessengerLinkContext linkContext, @Optional LinkUserIdentificator possibleIdentificator) { LinkConfirmationType linkConfirmationType = getLinkConfirmationType(actor); Messages messages = linkConfirmationType.getConfirmationMessages(linkContext.getConfirmationUser()); diff --git a/core/src/main/java/me/mastercapexd/auth/shared/commands/TelegramLinkCommand.java b/core/src/main/java/me/mastercapexd/auth/shared/commands/TelegramLinkCommand.java index 4bb5e422..14f0af0b 100644 --- a/core/src/main/java/me/mastercapexd/auth/shared/commands/TelegramLinkCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/shared/commands/TelegramLinkCommand.java @@ -13,6 +13,7 @@ import me.mastercapexd.auth.link.user.confirmation.BaseLinkConfirmationUser; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.server.commands.annotations.TelegramUse; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.annotation.Optional; @@ -34,6 +35,7 @@ public TelegramLinkCommand(Messages messages) { @TelegramUse @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void telegramLink(MessageableCommandActor commandActor, PlayerIdSupplier idSupplier, @Optional LinkUserIdentificator linkUserIdentificator) { String accountId = idSupplier.getPlayerId(); diff --git a/core/src/main/java/me/mastercapexd/auth/shared/commands/VKLinkCommand.java b/core/src/main/java/me/mastercapexd/auth/shared/commands/VKLinkCommand.java index 325fd608..809e13b2 100644 --- a/core/src/main/java/me/mastercapexd/auth/shared/commands/VKLinkCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/shared/commands/VKLinkCommand.java @@ -17,6 +17,7 @@ import me.mastercapexd.auth.link.vk.VKLinkType; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.server.commands.annotations.VkUse; +import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.annotation.Optional; @@ -38,6 +39,7 @@ public VKLinkCommand(Messages messages) { @VkUse @DefaultFor("~") + @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void vkLink(MessageableCommandActor commandActor, PlayerIdSupplier idSupplier, @Optional LinkUserIdentificator linkUserIdentificator) { String accountId = idSupplier.getPlayerId(); diff --git a/core/src/main/java/me/mastercapexd/auth/shared/commands/annotation/CommandCooldown.java b/core/src/main/java/me/mastercapexd/auth/shared/commands/annotation/CommandCooldown.java index 88c9a140..53468af9 100644 --- a/core/src/main/java/me/mastercapexd/auth/shared/commands/annotation/CommandCooldown.java +++ b/core/src/main/java/me/mastercapexd/auth/shared/commands/annotation/CommandCooldown.java @@ -10,6 +10,9 @@ @Target(ElementType.METHOD) public @interface CommandCooldown { + int DEFAULT_VALUE = 3000; + + long value(); TimeUnit unit() default TimeUnit.MILLISECONDS;