Search in sources :

Example 1 with AuthorizationCodeUriRequest

use of se.michaelthelin.spotify.requests.authorization.authorization_code.AuthorizationCodeUriRequest in project aiode by robinfriedli.

the class LoginCommand method doRun.

@Override
public void doRun() {
    User user = getContext().getUser();
    AuthorizationCodeUriRequest uriRequest = getContext().getSpotifyApi().authorizationCodeUri().show_dialog(true).state(user.getId()).scope("playlist-read-private playlist-read-collaborative user-library-read playlist-modify-private playlist-modify-public").build();
    LoginManager loginManager = Aiode.get().getLoginManager();
    CompletableFuture<Login> pendingLogin = new CompletableFuture<>();
    loginManager.expectLogin(user, pendingLogin);
    String loginUri = uriRequest.execute().toString();
    EmbedBuilder loginLinkBuilder = new EmbedBuilder().setTitle("Spotify login").setDescription(String.format("Click [here](%s) to be redirected to Spotify", loginUri)).setColor(0x1DB954);
    CompletableFuture<Message> futurePrivateMessage = getMessageService().send(loginLinkBuilder.build(), user);
    CompletableFuture<Message> futureNoticeMessage = new CompletableFuture<>();
    try {
        futurePrivateMessage.get();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setDescription("I have sent you a login link");
        sendMessage(embedBuilder).thenAccept(futureNoticeMessage::complete);
    } catch (CancellationException | ExecutionException e) {
        loginManager.removePendingLogin(user);
        throw new UserException("I was unable to send you a message. Please adjust your privacy settings to allow direct messages from guild members.");
    } catch (InterruptedException ignored) {
    }
    CompletableFuture<Login> futureLogin = pendingLogin.orTimeout(10, TimeUnit.MINUTES);
    CompletableFutures.handleWhenComplete(futureLogin, (login, throwable) -> {
        futureNoticeMessage.thenAccept(message -> message.delete().queue());
        futurePrivateMessage.thenAccept(message -> message.delete().queue());
        if (login != null) {
            getMessageService().sendSuccess("You have successfully connected your Spotify account and may now search and play tracks from your library", user);
            sendSuccess("User " + getContext().getUser().getName() + " logged in to Spotify");
        }
        if (throwable != null) {
            loginManager.removePendingLogin(user);
            if (throwable instanceof TimeoutException) {
                getMessageService().sendError("Login attempt timed out", user);
            } else {
                getMessageService().sendException("There has been an unexpected error while completing your login, please try again.", getContext().getChannel());
                LoggerFactory.getLogger(getClass()).error("unexpected exception while completing login", throwable);
            }
            setFailed(true);
        }
    }, e -> LoggerFactory.getLogger(getClass()).error("Unexpected error in whenComplete of pending login handler", e));
}
Also used : User(net.dv8tion.jda.api.entities.User) LoginManager(net.robinfriedli.aiode.login.LoginManager) Message(net.dv8tion.jda.api.entities.Message) AuthorizationCodeUriRequest(se.michaelthelin.spotify.requests.authorization.authorization_code.AuthorizationCodeUriRequest) Login(net.robinfriedli.aiode.login.Login) CompletableFuture(java.util.concurrent.CompletableFuture) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) CancellationException(java.util.concurrent.CancellationException) UserException(net.robinfriedli.aiode.exceptions.UserException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 Message (net.dv8tion.jda.api.entities.Message)1 User (net.dv8tion.jda.api.entities.User)1 UserException (net.robinfriedli.aiode.exceptions.UserException)1 Login (net.robinfriedli.aiode.login.Login)1 LoginManager (net.robinfriedli.aiode.login.LoginManager)1 AuthorizationCodeUriRequest (se.michaelthelin.spotify.requests.authorization.authorization_code.AuthorizationCodeUriRequest)1