Search in sources :

Example 1 with ConnectionListener

use of net.dv8tion.jda.api.audio.hooks.ConnectionListener in project JDA by DV8FromTheWorld.

the class GuildSetupNode method updateAudioManagerReference.

private void updateAudioManagerReference(GuildImpl guild) {
    JDAImpl api = getController().getJDA();
    AbstractCacheView<AudioManager> managerView = api.getAudioManagersView();
    try (UnlockHook hook = managerView.writeLock()) {
        TLongObjectMap<AudioManager> audioManagerMap = managerView.getMap();
        AudioManagerImpl mng = (AudioManagerImpl) audioManagerMap.get(id);
        if (mng == null)
            return;
        ConnectionListener listener = mng.getConnectionListener();
        final AudioManagerImpl newMng = new AudioManagerImpl(guild);
        newMng.setSelfMuted(mng.isSelfMuted());
        newMng.setSelfDeafened(mng.isSelfDeafened());
        newMng.setQueueTimeout(mng.getConnectTimeout());
        newMng.setSendingHandler(mng.getSendingHandler());
        newMng.setReceivingHandler(mng.getReceivingHandler());
        newMng.setConnectionListener(listener);
        newMng.setAutoReconnect(mng.isAutoReconnect());
        if (mng.isConnected()) {
            final long channelId = mng.getConnectedChannel().getIdLong();
            final VoiceChannel channel = api.getVoiceChannelById(channelId);
            if (channel != null) {
                if (mng.isConnected())
                    mng.closeAudioConnection(ConnectionStatus.ERROR_CANNOT_RESUME);
            } else {
                // The voice channel is not cached. It was probably deleted.
                api.getClient().removeAudioConnection(id);
                if (listener != null)
                    listener.onStatusChange(ConnectionStatus.DISCONNECTED_CHANNEL_DELETED);
            }
        }
        audioManagerMap.put(id, newMng);
    }
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) JDAImpl(net.dv8tion.jda.internal.JDAImpl) ConnectionListener(net.dv8tion.jda.api.audio.hooks.ConnectionListener)

Example 2 with ConnectionListener

use of net.dv8tion.jda.api.audio.hooks.ConnectionListener in project OrderlyDiscordBot by IceLeiYu.

the class MusicBot method connectVC.

@SuppressWarnings("ALL")
private void connectVC(Guild guild, VoiceChannel vc, GenericInteractionCreateEvent event, Consumer consumer) {
    if (!guild.getAudioManager().isConnected()) {
        try {
            guild.getAudioManager().openAudioConnection(vc);
        } catch (Exception e) {
            List<String> lang = Main.language.getGuildLang(event.getGuild().getId());
            if (event instanceof SelectMenuInteractionEvent)
                ((SelectMenuInteractionEvent) event).getHook().editOriginalEmbeds(createEmbed(lang.get(MUSICBOT_NO_CONNECT_PERMISSION), 0xFF0000)).queue();
            else if (event instanceof SlashCommandInteractionEvent)
                ((SlashCommandInteractionEvent) event).getHook().editOriginalEmbeds(createEmbed(lang.get(MUSICBOT_NO_CONNECT_PERMISSION), 0xFF0000)).queue();
            return;
        }
        final MusicBot bot = this;
        guild.getAudioManager().setConnectionListener(new ConnectionListener() {

            @Override
            public void onStatusChange(ConnectionStatus connectionStatus) {
                if (connectionStatus == ConnectionStatus.CONNECTED) {
                    consumer.accept(null);
                    if (workCount == 0) {
                        jda.getPresence().setStatus(OnlineStatus.DO_NOT_DISTURB);
                        jda.getPresence().setActivity(Activity.of(Activity.ActivityType.COMPETING, "來點歌吧!"));
                    }
                    workCount++;
                    jda.getPresence().setActivity(Activity.of(Activity.ActivityType.LISTENING, workCount + " 個頻道"));
                    guild.getAudioManager().setConnectionListener(null);
                    // 新增bot到頻道
                    musicBotManager.setBotToChannel(guild.getId(), vc.getId(), bot);
                    if (guild.getSelfMember().getPermissions().contains(Permission.VOICE_DEAF_OTHERS))
                        guild.getSelfMember().deafen(true).queue();
                }
            }

            @Override
            public void onPing(long l) {
            }

            @Override
            public void onUserSpeaking(User user, boolean b) {
            }
        });
    } else
        consumer.accept(null);
}
Also used : SelectMenuInteractionEvent(net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) List(java.util.List) ConnectionListener(net.dv8tion.jda.api.audio.hooks.ConnectionListener) ConnectionStatus(net.dv8tion.jda.api.audio.hooks.ConnectionStatus) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 3 with ConnectionListener

use of net.dv8tion.jda.api.audio.hooks.ConnectionListener in project JDA by DV8FromTheWorld.

the class WebSocketClient method getNextAudioConnectRequest.

protected ConnectionRequest getNextAudioConnectRequest() {
    // Don't try to setup audio connections before JDA has finished loading.
    if (sessionId == null)
        return null;
    long now = System.currentTimeMillis();
    AtomicReference<ConnectionRequest> request = new AtomicReference<>();
    queuedAudioConnections.retainEntries((// we use this because it locks the mutex
    guildId, // we use this because it locks the mutex
    audioRequest) -> {
        if (audioRequest.getNextAttemptEpoch() < now) {
            // Check if the guild is ready
            Guild guild = api.getGuildById(guildId);
            if (guild == null) {
                // Not yet ready, check if the guild is known to this shard
                GuildSetupController controller = api.getGuildSetupController();
                if (!controller.isKnown(guildId)) {
                    // The guild is not tracked anymore -> we can't connect the audio channel
                    LOG.debug("Removing audio connection request because the guild has been removed. {}", audioRequest);
                    return false;
                }
                return true;
            }
            ConnectionListener listener = guild.getAudioManager().getConnectionListener();
            if (audioRequest.getStage() != ConnectionStage.DISCONNECT) {
                // Check if we can connect to the target channel
                AudioChannel channel = (AudioChannel) guild.getGuildChannelById(audioRequest.getChannelId());
                if (channel == null) {
                    if (listener != null)
                        listener.onStatusChange(ConnectionStatus.DISCONNECTED_CHANNEL_DELETED);
                    return false;
                }
                IPermissionContainer permChannel = (IPermissionContainer) channel;
                if (!guild.getSelfMember().hasPermission(permChannel, Permission.VOICE_CONNECT)) {
                    if (listener != null)
                        listener.onStatusChange(ConnectionStatus.DISCONNECTED_LOST_PERMISSION);
                    return false;
                }
            }
            // This will take the first result
            request.compareAndSet(null, audioRequest);
        }
        return true;
    });
    return request.get();
}
Also used : ConnectionRequest(net.dv8tion.jda.internal.audio.ConnectionRequest) AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConnectionListener(net.dv8tion.jda.api.audio.hooks.ConnectionListener) Guild(net.dv8tion.jda.api.entities.Guild) IPermissionContainer(net.dv8tion.jda.api.entities.IPermissionContainer)

Aggregations

ConnectionListener (net.dv8tion.jda.api.audio.hooks.ConnectionListener)3 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConnectionStatus (net.dv8tion.jda.api.audio.hooks.ConnectionStatus)1 AudioChannel (net.dv8tion.jda.api.entities.AudioChannel)1 Guild (net.dv8tion.jda.api.entities.Guild)1 IPermissionContainer (net.dv8tion.jda.api.entities.IPermissionContainer)1 VoiceChannel (net.dv8tion.jda.api.entities.VoiceChannel)1 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)1 SelectMenuInteractionEvent (net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent)1 AudioManager (net.dv8tion.jda.api.managers.AudioManager)1 JDAImpl (net.dv8tion.jda.internal.JDAImpl)1 ConnectionRequest (net.dv8tion.jda.internal.audio.ConnectionRequest)1 AudioManagerImpl (net.dv8tion.jda.internal.managers.AudioManagerImpl)1 UnlockHook (net.dv8tion.jda.internal.utils.UnlockHook)1