Added #askConfirmation(User, String, Runnable) in ConfirmableCommand

This can allow the command to give a bit more context about the confirmation request.
ConfirmableCommand#askConfirmation(User, Runnable) can still be used.
This commit is contained in:
Florian CUNY 2018-09-02 11:55:26 +02:00
parent 5ad363ca11
commit e5cc56b919

View File

@ -48,11 +48,12 @@ public abstract class ConfirmableCommand extends CompositeCommand {
} }
/** /**
* Tells user to confirm command by retyping * Tells user to confirm command by retyping it.
* @param user - user * @param user User to ask confirmation to.
* @param confirmed - runnable to be executed if confirmed * @param message Optional message to send to the user to give them a bit more context. It must already be translated.
* @param confirmed Runnable to be executed if successfully confirmed.
*/ */
public void askConfirmation(User user, Runnable confirmed) { public void askConfirmation(User user, String message, Runnable confirmed) {
// Check for pending confirmations // Check for pending confirmations
if (toBeConfirmed.containsKey(user)) { if (toBeConfirmed.containsKey(user)) {
if (toBeConfirmed.get(user).getTopLabel().equals(getTopLabel()) && toBeConfirmed.get(user).getLabel().equalsIgnoreCase(getLabel())) { if (toBeConfirmed.get(user).getTopLabel().equals(getTopLabel()) && toBeConfirmed.get(user).getLabel().equalsIgnoreCase(getLabel())) {
@ -65,6 +66,10 @@ public abstract class ConfirmableCommand extends CompositeCommand {
user.sendMessage("commands.confirmation.previous-request-cancelled"); user.sendMessage("commands.confirmation.previous-request-cancelled");
} }
} }
// Send user the context message if it is not empty
if (!message.trim().isEmpty()) {
user.sendRawMessage(message);
}
// Tell user that they need to confirm // Tell user that they need to confirm
user.sendMessage("commands.confirmation.confirm", "[seconds]", String.valueOf(getSettings().getConfirmationTime())); user.sendMessage("commands.confirmation.confirm", "[seconds]", String.valueOf(getSettings().getConfirmationTime()));
// Set up a cancellation task // Set up a cancellation task
@ -77,6 +82,15 @@ public abstract class ConfirmableCommand extends CompositeCommand {
toBeConfirmed.put(user, new Confirmer(getTopLabel(), getLabel(), confirmed, task)); toBeConfirmed.put(user, new Confirmer(getTopLabel(), getLabel(), confirmed, task));
} }
/**
* Tells user to confirm command by retyping it.
* @param user User to ask confirmation to.
* @param confirmed Runnable to be executed if successfully confirmed.
*/
public void askConfirmation(User user, Runnable confirmed) {
askConfirmation(user, "", confirmed);
}
private class Confirmer { private class Confirmer {
private final String topLabel; private final String topLabel;
private final String label; private final String label;