Search in sources :

Example 1 with AudioHandler

use of com.jagrosh.jmusicbot.audio.AudioHandler in project MusicBot by jagrosh.

the class Bot method shutdown.

public void shutdown() {
    if (shuttingDown)
        return;
    shuttingDown = true;
    threadpool.shutdownNow();
    if (jda.getStatus() != JDA.Status.SHUTTING_DOWN) {
        jda.getGuilds().stream().forEach(g -> {
            g.getAudioManager().closeAudioConnection();
            AudioHandler ah = (AudioHandler) g.getAudioManager().getSendingHandler();
            if (ah != null) {
                ah.stopAndClear();
                ah.getPlayer().destroy();
                nowplaying.updateTopic(g.getIdLong(), ah, true);
            }
        });
        jda.shutdown();
    }
    if (gui != null)
        gui.dispose();
    System.exit(0);
}
Also used : AudioHandler(com.jagrosh.jmusicbot.audio.AudioHandler)

Example 2 with AudioHandler

use of com.jagrosh.jmusicbot.audio.AudioHandler in project MusicBot by jagrosh.

the class VolumeCmd method doCommand.

@Override
public void doCommand(CommandEvent event) {
    AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
    Settings settings = event.getClient().getSettingsFor(event.getGuild());
    int volume = handler.getPlayer().getVolume();
    if (event.getArgs().isEmpty()) {
        event.reply(FormatUtil.volumeIcon(volume) + " Current volume is `" + volume + "`");
    } else {
        int nvolume;
        try {
            nvolume = Integer.parseInt(event.getArgs());
        } catch (NumberFormatException e) {
            nvolume = -1;
        }
        if (nvolume < 0 || nvolume > 150)
            event.reply(event.getClient().getError() + " Volume must be a valid integer between 0 and 150!");
        else {
            handler.getPlayer().setVolume(nvolume);
            settings.setVolume(nvolume);
            event.reply(FormatUtil.volumeIcon(nvolume) + " Volume changed from `" + volume + "` to `" + nvolume + "`");
        }
    }
}
Also used : AudioHandler(com.jagrosh.jmusicbot.audio.AudioHandler) Settings(com.jagrosh.jmusicbot.settings.Settings)

Example 3 with AudioHandler

use of com.jagrosh.jmusicbot.audio.AudioHandler in project MusicBot by jagrosh.

the class LyricsCmd method doCommand.

@Override
public void doCommand(CommandEvent event) {
    String title;
    if (event.getArgs().isEmpty()) {
        AudioHandler sendingHandler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
        if (sendingHandler.isMusicPlaying(event.getJDA()))
            title = sendingHandler.getPlayer().getPlayingTrack().getInfo().title;
        else {
            event.replyError("There must be music playing to use that!");
            return;
        }
    } else
        title = event.getArgs();
    event.getChannel().sendTyping().queue();
    client.getLyrics(title).thenAccept(lyrics -> {
        if (lyrics == null) {
            event.replyError("Lyrics for `" + title + "` could not be found!" + (event.getArgs().isEmpty() ? " Try entering the song name manually (`lyrics [song name]`)" : ""));
            return;
        }
        EmbedBuilder eb = new EmbedBuilder().setAuthor(lyrics.getAuthor()).setColor(event.getSelfMember().getColor()).setTitle(lyrics.getTitle(), lyrics.getURL());
        if (lyrics.getContent().length() > 15000) {
            event.replyWarning("Lyrics for `" + title + "` found but likely not correct: " + lyrics.getURL());
        } else if (lyrics.getContent().length() > 2000) {
            String content = lyrics.getContent().trim();
            while (content.length() > 2000) {
                int index = content.lastIndexOf("\n\n", 2000);
                if (index == -1)
                    index = content.lastIndexOf("\n", 2000);
                if (index == -1)
                    index = content.lastIndexOf(" ", 2000);
                if (index == -1)
                    index = 2000;
                event.reply(eb.setDescription(content.substring(0, index).trim()).build());
                content = content.substring(index).trim();
                eb.setAuthor(null).setTitle(null, null);
            }
            event.reply(eb.setDescription(content).build());
        } else
            event.reply(eb.setDescription(lyrics.getContent()).build());
    });
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AudioHandler(com.jagrosh.jmusicbot.audio.AudioHandler)

Example 4 with AudioHandler

use of com.jagrosh.jmusicbot.audio.AudioHandler in project MusicBot by jagrosh.

the class PlayCmd method doCommand.

@Override
public void doCommand(CommandEvent event) {
    if (event.getArgs().isEmpty() && event.getMessage().getAttachments().isEmpty()) {
        AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
        if (handler.getPlayer().getPlayingTrack() != null && handler.getPlayer().isPaused()) {
            if (DJCommand.checkDJPermission(event)) {
                handler.getPlayer().setPaused(false);
                event.replySuccess("Resumed **" + handler.getPlayer().getPlayingTrack().getInfo().title + "**.");
            } else
                event.replyError("Only DJs can unpause the player!");
            return;
        }
        StringBuilder builder = new StringBuilder(event.getClient().getWarning() + " Play Commands:\n");
        builder.append("\n`").append(event.getClient().getPrefix()).append(name).append(" <song title>` - plays the first result from Youtube");
        builder.append("\n`").append(event.getClient().getPrefix()).append(name).append(" <URL>` - plays the provided song, playlist, or stream");
        for (Command cmd : children) builder.append("\n`").append(event.getClient().getPrefix()).append(name).append(" ").append(cmd.getName()).append(" ").append(cmd.getArguments()).append("` - ").append(cmd.getHelp());
        event.reply(builder.toString());
        return;
    }
    String args = event.getArgs().startsWith("<") && event.getArgs().endsWith(">") ? event.getArgs().substring(1, event.getArgs().length() - 1) : event.getArgs().isEmpty() ? event.getMessage().getAttachments().get(0).getUrl() : event.getArgs();
    event.reply(loadingEmoji + " Loading... `[" + args + "]`", m -> bot.getPlayerManager().loadItemOrdered(event.getGuild(), args, new ResultHandler(m, event, false)));
}
Also used : Command(com.jagrosh.jdautilities.command.Command) DJCommand(com.jagrosh.jmusicbot.commands.DJCommand) MusicCommand(com.jagrosh.jmusicbot.commands.MusicCommand) AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioHandler(com.jagrosh.jmusicbot.audio.AudioHandler)

Example 5 with AudioHandler

use of com.jagrosh.jmusicbot.audio.AudioHandler in project MusicBot by jagrosh.

the class MusicCommand method execute.

@Override
protected void execute(CommandEvent event) {
    Settings settings = event.getClient().getSettingsFor(event.getGuild());
    TextChannel tchannel = settings.getTextChannel(event.getGuild());
    if (tchannel != null && !event.getTextChannel().equals(tchannel)) {
        try {
            event.getMessage().delete().queue();
        } catch (PermissionException ignore) {
        }
        event.replyInDm(event.getClient().getError() + " You can only use that command in " + tchannel.getAsMention() + "!");
        return;
    }
    // no point constantly checking for this later
    bot.getPlayerManager().setUpHandler(event.getGuild());
    if (bePlaying && !((AudioHandler) event.getGuild().getAudioManager().getSendingHandler()).isMusicPlaying(event.getJDA())) {
        event.reply(event.getClient().getError() + " There must be music playing to use that!");
        return;
    }
    if (beListening) {
        VoiceChannel current = event.getGuild().getSelfMember().getVoiceState().getChannel();
        if (current == null)
            current = settings.getVoiceChannel(event.getGuild());
        GuildVoiceState userState = event.getMember().getVoiceState();
        if (!userState.inVoiceChannel() || userState.isDeafened() || (current != null && !userState.getChannel().equals(current))) {
            event.replyError("You must be listening in " + (current == null ? "a voice channel" : current.getAsMention()) + " to use that!");
            return;
        }
        VoiceChannel afkChannel = userState.getGuild().getAfkChannel();
        if (afkChannel != null && afkChannel.equals(userState.getChannel())) {
            event.replyError("You cannot use that command in an AFK channel!");
            return;
        }
        if (!event.getGuild().getSelfMember().getVoiceState().inVoiceChannel()) {
            try {
                event.getGuild().getAudioManager().openAudioConnection(userState.getChannel());
            } catch (PermissionException ex) {
                event.reply(event.getClient().getError() + " I am unable to connect to " + userState.getChannel().getAsMention() + "!");
                return;
            }
        }
    }
    doCommand(event);
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) TextChannel(net.dv8tion.jda.api.entities.TextChannel) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) AudioHandler(com.jagrosh.jmusicbot.audio.AudioHandler) Settings(com.jagrosh.jmusicbot.settings.Settings)

Aggregations

AudioHandler (com.jagrosh.jmusicbot.audio.AudioHandler)32 Settings (com.jagrosh.jmusicbot.settings.Settings)8 QueuedTrack (com.jagrosh.jmusicbot.audio.QueuedTrack)6 CommandEvent (com.jagrosh.jdautilities.command.CommandEvent)4 Bot (com.jagrosh.jmusicbot.Bot)4 RequestMetadata (com.jagrosh.jmusicbot.audio.RequestMetadata)4 DJCommand (com.jagrosh.jmusicbot.commands.DJCommand)4 MusicCommand (com.jagrosh.jmusicbot.commands.MusicCommand)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 Permission (net.dv8tion.jda.api.Permission)4 Message (net.dv8tion.jda.api.entities.Message)4 User (net.dv8tion.jda.api.entities.User)4 PermissionException (net.dv8tion.jda.api.exceptions.PermissionException)4 Command (com.jagrosh.jdautilities.command.Command)2 FinderUtil (com.jagrosh.jdautilities.commons.utils.FinderUtil)2 OrderedMenu (com.jagrosh.jdautilities.menu.OrderedMenu)2 Paginator (com.jagrosh.jdautilities.menu.Paginator)2 JMusicBot (com.jagrosh.jmusicbot.JMusicBot)2 RepeatMode (com.jagrosh.jmusicbot.settings.RepeatMode)2