Skip to content

Commit

Permalink
Apply @CommandCooldown to all database-related commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy committed Oct 12, 2023
1 parent 7e225e3 commit 7ce8d7b
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)));
Expand All @@ -40,4 +43,5 @@ public void onConfirmationToggle(LinkCommandActorWrapper actorWrapper, LinkType
accountDatabase.saveOrUpdateAccount(account);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
@Target(ElementType.METHOD)
public @interface CommandCooldown {

int DEFAULT_VALUE = 3000;


long value();

TimeUnit unit() default TimeUnit.MILLISECONDS;
Expand Down

0 comments on commit 7ce8d7b

Please sign in to comment.