lavalink.client.io.jda.JdaLink Java Examples

The following examples show how to use lavalink.client.io.jda.JdaLink. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LavalinkTest.java    From Lavalink-Client with MIT License 6 votes vote down vote up
private static void ensureConnected(JdaLavalink lavalink, VoiceChannel voiceChannel) {
    JdaLink link = lavalink.getLink(voiceChannel.getGuild());
    link.connect(voiceChannel);
    long started = System.currentTimeMillis();
    while (link.getState() != Link.State.CONNECTED
            && System.currentTimeMillis() - started < 10000 //wait 10 sec max
            && !Thread.currentThread().isInterrupted()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        link.connect(voiceChannel);
    }

    assertEquals(Link.State.CONNECTED, link.getState(), "Failed to connect to voice channel in a reasonable amount of time");
}
 
Example #2
Source File: AudioCmdUtils.java    From MantaroBot with GNU General Public License v3.0 6 votes vote down vote up
public static CompletionStage<Void> openAudioConnection(GuildMessageReceivedEvent event, JdaLink link, VoiceChannel userChannel, I18nContext lang) {
    if (userChannel.getUserLimit() <= userChannel.getMembers().size() && userChannel.getUserLimit() > 0 && !event.getGuild().getSelfMember().hasPermission(Permission.MANAGE_CHANNEL)) {
        event.getChannel().sendMessageFormat(lang.get("commands.music_general.connect.full_channel"), EmoteReference.ERROR).queue();
        return completedFuture(null);
    }

    try {
        //This used to be a CompletableFuture that went through a listener which is now useless bc im 99% sure you can't listen to the connection status on LL.
        joinVoiceChannel(link, userChannel);
        event.getChannel().sendMessageFormat(lang.get("commands.music_general.connect.success"), EmoteReference.CORRECT, userChannel.getName()).queue();
        return completedFuture(null);
    } catch (NullPointerException e) {
        e.printStackTrace();
        event.getChannel().sendMessageFormat(lang.get("commands.music_general.connect.non_existent_channel"), EmoteReference.ERROR).queue();

        //Reset custom channel.
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        dbGuild.getData().setMusicChannel(null);
        dbGuild.saveAsync();
        CompletableFuture<Void> future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;
    }
}
 
Example #3
Source File: MusicCmds.java    From MantaroBot with GNU General Public License v3.0 6 votes vote down vote up
private boolean isInConditionTo(Context ctx, JdaLink player) {
    try {
        if (!ctx.getMember().getVoiceState().inVoiceChannel() || !ctx.getMember().getVoiceState().getChannel().getId().equalsIgnoreCase(player.getChannel())) {
            if (isDJ(ctx.getMember())) {
                return true;
            }

            sendNotConnectedToMyChannel(ctx.getChannel(), ctx.getLanguageContext());
            return false;
        }

        return true;
    } catch (NullPointerException e) {
        if (ctx.getSelfMember().getVoiceState().inVoiceChannel())
            log.error("Possible bug? No player even though bot is connected to a channel!", e);

        ctx.sendLocalized("commands.music_general.no_player", EmoteReference.ERROR);
        return false; //No player to stop/change?
    }
}
 
Example #4
Source File: GuildMusicManager.java    From MantaroBot with GNU General Public License v3.0 5 votes vote down vote up
public GuildMusicManager(String guildId) {
    this.guildId = guildId;

    JdaLink lavaLink = MantaroBot.getInstance().getLavaLink().getLink(guildId);
    trackScheduler = new TrackScheduler(lavaLink, guildId);

    lavaLink.getPlayer().addListener(trackScheduler);
}
 
Example #5
Source File: GuildMusicManager.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
public JdaLink getLavaLink() {
    return MantaroBot.getInstance().getLavaLink().getLink(guildId);
}
 
Example #6
Source File: AudioCmdUtils.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
public static CompletionStage<Boolean> connectToVoiceChannel(GuildMessageReceivedEvent event, I18nContext lang) {
    VoiceChannel userChannel = event.getMember().getVoiceState().getChannel();
    Guild guild = event.getGuild();
    TextChannel textChannel = event.getChannel();

    //I can't see you in any VC here?
    if (userChannel == null) {
        textChannel.sendMessageFormat(lang.get("commands.music_general.connect.user_no_vc"), EmoteReference.ERROR).queue();
        return completedFuture(false);
    }

    //Can't connect to this channel
    if (!guild.getSelfMember().hasPermission(userChannel, Permission.VOICE_CONNECT)) {
        textChannel.sendMessageFormat(lang.get("commands.music_general.connect.missing_permissions_connect"), EmoteReference.ERROR, lang.get("discord_permissions.voice_connect")).queue();
        return completedFuture(false);
    }

    //Can't speak on this channel
    if (!guild.getSelfMember().hasPermission(userChannel, Permission.VOICE_SPEAK)) {
        textChannel.sendMessageFormat(lang.get("commands.music_general.connect.missing_permission_speak"), EmoteReference.ERROR, lang.get("discord_permissions.voice_speak")).queue();
        return completedFuture(false);
    }

    //Set the custom guild music channel from the db value
    VoiceChannel guildMusicChannel = null;
    if (MantaroData.db().getGuild(guild).getData().getMusicChannel() != null)
        guildMusicChannel = guild.getVoiceChannelById(MantaroData.db().getGuild(guild).getData().getMusicChannel());

    //This is where we call LL.
    JdaLink link = MantaroBot.getInstance().getAudioManager().getMusicManager(guild).getLavaLink();
    if (guildMusicChannel != null) {
        //If the channel is not the set one, reject this connect.
        if (!userChannel.equals(guildMusicChannel)) {
            textChannel.sendMessageFormat(lang.get("commands.music_general.connect.channel_locked"), EmoteReference.ERROR, guildMusicChannel.getName()).queue();
            return completedFuture(false);
        }

        //If the link is not currently connected or connecting, accept connection and call openAudioConnection
        if (link.getState() != Link.State.CONNECTED && link.getState() != Link.State.CONNECTING) {
            log.debug("Connected to channel {}. Reason: Link is not CONNECTED or CONNECTING and we requested a connection from connectToVoiceChannel (custom music channel)", userChannel.getId());
            return openAudioConnection(event, link, userChannel, lang).thenApply(__ -> true);
        }

        //Nothing to connect to, but pass true so we can load the song (for example, it's already connected)
        return completedFuture(true);
    }

    //Assume last channel it's the one it was attempting to connect to? (on the one below this too)
    //If the link is CONNECTED and the lastChannel is not the one it's already connected to, reject connection
    if (link.getState() == Link.State.CONNECTED && link.getLastChannel() != null && !link.getLastChannel().equals(userChannel.getId())) {
        textChannel.sendMessageFormat(lang.get("commands.music_general.connect.already_connected"), EmoteReference.WARNING, guild.getVoiceChannelById(link.getLastChannel()).getName()).queue();
        return completedFuture(false);
    }

    //If the link is CONNECTING and the lastChannel is not the one it's already connected to, reject connection
    if (link.getState() == Link.State.CONNECTING && link.getLastChannel() != null && !link.getLastChannel().equals(userChannel.getId())) {
        textChannel.sendMessageFormat(lang.get("commands.music_general.connect.attempting_to_connect"), EmoteReference.ERROR, guild.getVoiceChannelById(link.getLastChannel()).getName()).queue();
        return completedFuture(false);
    }

    //If the link is not currently connected or connecting, accept connection and call openAudioConnection
    if (link.getState() != Link.State.CONNECTED && link.getState() != Link.State.CONNECTING) {
        log.debug("Connected to voice channel {}. Reason: Link is not CONNECTED or CONNECTING and we requested a connection from connectToVoiceChannel", userChannel.getId());
        return openAudioConnection(event, link, userChannel, lang).thenApply(__ -> true);
    }

    //Nothing to connect to, but pass true so we can load the song (for example, it's already connected)
    return completedFuture(true);
}
 
Example #7
Source File: AudioCmdUtils.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
private static void joinVoiceChannel(JdaLink manager, VoiceChannel channel) {
    manager.connect(channel);
}