Search in sources :

Example 26 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class AudioManager method playTrack.

public void playTrack(Guild guild, @Nullable VoiceChannel channel, boolean resumePaused) {
    AudioPlayback playback = getPlaybackForGuild(guild);
    if (!resumePaused && playback.isPaused()) {
        playback.getAudioPlayer().stopTrack();
        playback.unpause();
    }
    if (channel != null) {
        setChannel(playback, channel);
    } else if (playback.getVoiceChannel() == null) {
        throw new InvalidCommandException("Not in a voice channel");
    }
    if (ExecutionContext.Current.isSet()) {
        playback.setCommunicationChannel(ExecutionContext.Current.require().getChannel());
    }
    if (playback.isPaused() && resumePaused) {
        playback.unpause();
    } else {
        QueueIterator queueIterator = new QueueIterator(playback, this);
        playback.setCurrentQueueIterator(queueIterator);
        queueIterator.playNext();
    }
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException)

Example 27 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class PlayableFactory method createPlayableForSpotifyUrlType.

private List<Playable> createPlayableForSpotifyUrlType(StringList pathFragments, String type, CheckedFunction<String, List<Playable>> loadFunc, SpotifyApi spotifyApi) {
    String id = pathFragments.tryGet(pathFragments.indexOf(type) + 1);
    if (Strings.isNullOrEmpty(id)) {
        throw new InvalidCommandException(String.format("No %s id provided", type));
    }
    try {
        String accessToken = spotifyApi.clientCredentials().build().execute().getAccessToken();
        spotifyApi.setAccessToken(accessToken);
        return loadFunc.doApply(id);
    } catch (NotFoundException e) {
        throw new NoResultsFoundException(String.format("No Spotify track found for id '%s'", id));
    } catch (Exception e) {
        throw new RuntimeException("Exception during Spotify request", e);
    } finally {
        spotifyApi.setAccessToken(null);
    }
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) NotFoundException(se.michaelthelin.spotify.exceptions.detailed.NotFoundException) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) NotFoundException(se.michaelthelin.spotify.exceptions.detailed.NotFoundException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) IOException(java.io.IOException)

Example 28 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class PlayableFactory method createPlayablesFromSpotifyUrl.

private List<Playable> createPlayablesFromSpotifyUrl(URI uri, SpotifyApi spotifyApi, boolean redirectSpotify) {
    StringList pathFragments = StringList.createWithRegex(uri.getPath(), "/");
    SpotifyService spotifyService = new SpotifyService(spotifyApi);
    if (pathFragments.contains("playlist")) {
        return createPlayableForSpotifyUrlType(pathFragments, "playlist", playlistId -> {
            List<SpotifyTrack> playlistTracks = spotifyService.getPlaylistTracks(playlistId);
            return createPlayables(redirectSpotify, playlistTracks);
        }, spotifyApi);
    } else if (pathFragments.contains("track")) {
        return createPlayableForSpotifyUrlType(pathFragments, "track", trackId -> {
            Track track = spotifyApi.getTrack(trackId).build().execute();
            return Lists.newArrayList(createPlayable(redirectSpotify, track));
        }, spotifyApi);
    } else if (pathFragments.contains("episode")) {
        return createPlayableForSpotifyUrlType(pathFragments, "episode", episodeId -> {
            Episode episode = spotifyApi.getEpisode(episodeId).build().execute();
            return Lists.newArrayList(createPlayable(redirectSpotify, episode));
        }, spotifyApi);
    } else if (pathFragments.contains("album")) {
        return createPlayableForSpotifyUrlType(pathFragments, "album", albumId -> {
            List<Track> albumTracks = spotifyService.getAlbumTracks(albumId);
            return createPlayables(redirectSpotify, albumTracks);
        }, spotifyApi);
    } else if (pathFragments.contains("show")) {
        return createPlayableForSpotifyUrlType(pathFragments, "show", showId -> {
            List<Episode> showEpisodes = spotifyService.getShowEpisodes(showId);
            return createPlayables(redirectSpotify, showEpisodes);
        }, spotifyApi);
    } else {
        throw new InvalidCommandException("Detected Spotify URL but no track, playlist or album id provided.");
    }
}
Also used : StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyInvoker(net.robinfriedli.aiode.function.SpotifyInvoker) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) PlayableTrackWrapper(net.robinfriedli.aiode.audio.spotify.PlayableTrackWrapper) Strings(com.google.common.base.Strings) NotFoundException(se.michaelthelin.spotify.exceptions.detailed.NotFoundException) Lists(com.google.common.collect.Lists) HollowYouTubeVideo(net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo) Map(java.util.Map) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) Track(se.michaelthelin.spotify.model_objects.specification.Track) URI(java.net.URI) SpotifyTrackRedirectionRunnable(net.robinfriedli.aiode.audio.exec.SpotifyTrackRedirectionRunnable) Nullable(javax.annotation.Nullable) Collection(java.util.Collection) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SpotifyTrackKind(net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind) StandardCharsets(java.nio.charset.StandardCharsets) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) YouTubePlaylistPopulationRunnable(net.robinfriedli.aiode.audio.exec.YouTubePlaylistPopulationRunnable) List(java.util.List) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) TrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.TrackLoadingExecutor) NameValuePair(org.apache.http.NameValuePair) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) Collections(java.util.Collections) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) CheckedFunction(net.robinfriedli.aiode.function.CheckedFunction) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StringList(net.robinfriedli.stringlist.StringList) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Track(se.michaelthelin.spotify.model_objects.specification.Track) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack)

Example 29 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class AbstractCommand method processCommand.

/**
 * @param commandString the string following the command identifier
 * @deprecated replaced by the {@link CommandParser}
 */
@Deprecated
private void processCommand(String commandString) {
    StringList words = StringList.separateString(commandString, " ");
    int commandBodyIndex = 0;
    for (String word : words) {
        if (word.startsWith("$")) {
            String argString = word.replaceFirst("\\$", "");
            // check if the argument has an assigned value
            int equalsIndex = argString.indexOf("=");
            if (equalsIndex > -1) {
                if (equalsIndex == 0 || equalsIndex == word.length() - 1) {
                    throw new InvalidCommandException("Malformed argument. Equals sign cannot be first or last character.");
                }
                String argument = argString.substring(0, equalsIndex);
                String value = argString.substring(equalsIndex + 1);
                argumentController.setArgument(argument.toLowerCase(), value);
            } else {
                argumentController.setArgument(argString.toLowerCase());
            }
        } else if (!word.isBlank()) {
            break;
        }
        commandBodyIndex += word.length();
    }
    commandInput = words.toString().substring(commandBodyIndex).trim();
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StringList(net.robinfriedli.stringlist.StringList)

Example 30 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class CommandManager method runCommand.

/**
 * Run command in a new thread, to be enqueued in the provided {@link ThreadExecutionQueue}.
 *
 * @param command        the command to run
 * @param executionQueue the target execution queue
 */
public void runCommand(Command command, ThreadExecutionQueue executionQueue) {
    CommandContext context = command.getContext();
    CommandExecutionTask commandExecutionTask = new CommandExecutionTask(command, executionQueue, this);
    commandExecutionTask.setName("command-execution-" + context);
    try {
        boolean queued = !executionQueue.add(commandExecutionTask, false);
        if (queued) {
            MessageService messageService = Aiode.get().getMessageService();
            messageService.sendError("Executing too many commands concurrently. This command will be executed after one has finished. " + "You may use the abort command to cancel queued commands and interrupt running commands.", context.getChannel());
            logger.warn(String.format("Guild %s has reached the max concurrent commands limit.", context.getGuild()));
        }
    } catch (RateLimitException e) {
        if (!e.isTimeout()) {
            throw new InvalidCommandException(e.getMessage());
        }
    }
}
Also used : CommandExecutionTask(net.robinfriedli.aiode.concurrent.CommandExecutionTask) RateLimitException(net.robinfriedli.aiode.exceptions.RateLimitException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) MessageService(net.robinfriedli.aiode.discord.MessageService)

Aggregations

InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)47 Guild (net.dv8tion.jda.api.entities.Guild)12 Session (org.hibernate.Session)11 CommandContext (net.robinfriedli.aiode.command.CommandContext)10 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)9 StringList (net.robinfriedli.stringlist.StringList)9 List (java.util.List)8 Playlist (net.robinfriedli.aiode.entities.Playlist)8 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)7 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)6 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)5 Collection (java.util.Collection)5 AudioPlayback (net.robinfriedli.aiode.audio.AudioPlayback)5 PlaylistItem (net.robinfriedli.aiode.entities.PlaylistItem)5 IOException (java.io.IOException)4 User (net.dv8tion.jda.api.entities.User)4 AudioManager (net.robinfriedli.aiode.audio.AudioManager)4 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)4 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)4 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)4