Search in sources :

Example 21 with InvalidCommandException

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

the class PlayCommand method doRun.

@Override
public void doRun() throws Exception {
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    VoiceChannel channel = context.getVoiceChannel();
    AudioManager audioManager = Aiode.get().getAudioManager();
    MessageChannel messageChannel = getContext().getChannel();
    AudioPlayback playbackForGuild = audioManager.getPlaybackForGuild(guild);
    playbackForGuild.setCommunicationChannel(messageChannel);
    if (getCommandInput().isBlank()) {
        if (playbackForGuild.isPaused() || !audioManager.getQueue(guild).isEmpty()) {
            audioManager.startOrResumePlayback(guild, channel);
        } else {
            throw new InvalidCommandException("Queue is empty. Specify a song you want to play.");
        }
    } else {
        super.doRun();
    }
}
Also used : AudioManager(net.robinfriedli.aiode.audio.AudioManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) CommandContext(net.robinfriedli.aiode.command.CommandContext) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Guild(net.dv8tion.jda.api.entities.Guild)

Example 22 with InvalidCommandException

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

the class ReverseCommand method doRun.

@Override
public void doRun() {
    AudioPlayback playback = Aiode.get().getAudioManager().getPlaybackForGuild(getContext().getGuild());
    AudioTrack playingTrack = playback.getAudioPlayer().getPlayingTrack();
    if (playingTrack == null) {
        throw new InvalidCommandException("No track is being played at the moment");
    }
    long toReverseMs;
    try {
        if (argumentSet("minutes")) {
            toReverseMs = Integer.parseInt(getCommandInput()) * 60000;
        } else {
            toReverseMs = Integer.parseInt(getCommandInput()) * 1000;
        }
    } catch (NumberFormatException e) {
        throw new InvalidCommandException("'" + getCommandInput() + "' is not convertible to type integer. " + "Please enter a valid number.");
    }
    if (toReverseMs <= 0) {
        throw new InvalidCommandException("Expected 1 or greater");
    }
    long newPosition = playback.getCurrentPositionMs() - toReverseMs;
    if (newPosition < 0) {
        throw new InvalidCommandException("New position less than 0");
    }
    playback.setPosition(newPosition);
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack)

Example 23 with InvalidCommandException

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

the class RewindCommand method doRun.

@Override
public void doRun() {
    AudioManager audioManager = Aiode.get().getAudioManager();
    Guild guild = getContext().getGuild();
    AudioPlayback playback = audioManager.getPlaybackForGuild(guild);
    AudioQueue queue = playback.getAudioQueue();
    VoiceChannel channel = getContext().getVoiceChannel();
    if (!queue.hasPrevious()) {
        throw new InvalidCommandException("No previous item in queue");
    }
    int queueSize = queue.getTracks().size();
    if (getCommandInput().isBlank()) {
        queue.reverse();
    } else {
        int offset;
        try {
            offset = Integer.parseInt(getCommandInput());
        } catch (NumberFormatException e) {
            throw new InvalidCommandException(getCommandInput() + " is not an integer");
        }
        if (offset < 1) {
            throw new InvalidCommandException("Expected a number grater than 0");
        }
        boolean deficient = queue.getPosition() - offset < 0;
        int newIndex;
        if (!playback.isRepeatAll() && deficient) {
            newIndex = 0;
        } else if (deficient) {
            // if the current index is 20 with 50 tracks in the queue and the user wants to rewind 24, the result should be 46
            int provisional = queue.getPosition() - offset;
            int page = provisional / queueSize * (-1) + 1;
            newIndex = page * queueSize + provisional;
        } else {
            newIndex = queue.getPosition() - offset;
        }
        queue.setPosition(newIndex);
    }
    audioManager.startPlayback(guild, channel);
}
Also used : AudioManager(net.robinfriedli.aiode.audio.AudioManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Guild(net.dv8tion.jda.api.entities.Guild) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue)

Example 24 with InvalidCommandException

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

the class SkipCommand method doRun.

@Override
public void doRun() {
    AudioManager audioManager = Aiode.get().getAudioManager();
    Guild guild = getContext().getGuild();
    AudioPlayback playback = audioManager.getPlaybackForGuild(guild);
    AudioQueue queue = playback.getAudioQueue();
    VoiceChannel channel = getContext().getVoiceChannel();
    if (!queue.hasNext()) {
        throw new InvalidCommandException("No next item in queue");
    }
    if (getCommandInput().isBlank()) {
        queue.iterate();
    } else {
        int offset;
        try {
            offset = Integer.parseInt(getCommandInput());
        } catch (NumberFormatException e) {
            throw new InvalidCommandException(getCommandInput() + " is not an integer");
        }
        if (offset < 1) {
            throw new InvalidCommandException("Expected a number greater than 0");
        }
        int newIndex;
        int queueSize = queue.getTracks().size();
        boolean overflow = queue.getPosition() + offset >= queueSize;
        if (!playback.isRepeatAll() && overflow) {
            newIndex = queueSize - 1;
        } else if (overflow) {
            // if the current index is 30 with 50 tracks in the queue and the user wants to skip 24, the result should be 4
            // if the user wants to skip 20, the result be 0
            int provisional = queue.getPosition() + offset;
            int page = provisional / queueSize;
            newIndex = provisional - (page * queueSize);
        } else {
            newIndex = queue.getPosition() + offset;
        }
        queue.setPosition(newIndex);
    }
    audioManager.startPlayback(guild, channel);
}
Also used : AudioManager(net.robinfriedli.aiode.audio.AudioManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Guild(net.dv8tion.jda.api.entities.Guild) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue)

Example 25 with InvalidCommandException

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

the class VolumeCommand method doRun.

@Override
public void doRun() {
    AudioPlayback playback = Aiode.get().getAudioManager().getPlaybackForGuild(getContext().getGuild());
    int volume;
    try {
        volume = Integer.parseInt(getCommandInput());
    } catch (NumberFormatException e) {
        throw new InvalidCommandException("'" + getCommandInput() + "' is not an integer");
    }
    if (!(volume > 0 && volume <= 200)) {
        throw new InvalidCommandException("Expected a value between 1 and 200");
    }
    playback.setVolume(volume);
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException)

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