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();
}
}
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);
}
}
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.");
}
}
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();
}
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());
}
}
}
Aggregations