Search in sources :

Example 1 with ConnectionStatus

use of net.dv8tion.jda.api.audio.hooks.ConnectionStatus 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 2 with ConnectionStatus

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

the class AudioWebSocket method close.

protected void close(final ConnectionStatus closeStatus) {
    // Makes sure we don't run this method again after the socket.close(1000) call fires onDisconnect
    if (shutdown)
        return;
    locked((manager) -> {
        if (shutdown)
            return;
        ConnectionStatus status = closeStatus;
        ready = false;
        shutdown = true;
        stopKeepAlive();
        if (audioConnection.udpSocket != null)
            audioConnection.udpSocket.close();
        if (socket != null)
            socket.sendClose();
        audioConnection.shutdown();
        AudioChannel disconnectedChannel = manager.getConnectedChannel();
        manager.setAudioConnection(null);
        // Verify that it is actually a lost of connection and not due the connected channel being deleted.
        JDAImpl api = getJDA();
        if (status == ConnectionStatus.DISCONNECTED_KICKED_FROM_CHANNEL && (!api.getClient().isSession() || !api.getClient().isConnected())) {
            LOG.debug("Connection was closed due to session invalidate!");
            status = ConnectionStatus.ERROR_CANNOT_RESUME;
        } else if (status == ConnectionStatus.ERROR_LOST_CONNECTION || status == ConnectionStatus.DISCONNECTED_KICKED_FROM_CHANNEL) {
            // Get guild from JDA, don't use [guild] field to make sure that we don't have
            // a problem of an out of date guild stored in [guild] during a possible mWS invalidate.
            Guild connGuild = api.getGuildById(guild.getIdLong());
            if (connGuild != null) {
                AudioChannel channel = (AudioChannel) connGuild.getGuildChannelById(audioConnection.getChannel().getIdLong());
                if (channel == null)
                    status = ConnectionStatus.DISCONNECTED_CHANNEL_DELETED;
            }
        }
        changeStatus(status);
        // decide if we reconnect.
        if (shouldReconnect && // indicated that the connection was purposely closed. don't reconnect.
        status.shouldReconnect() && // Already handled.
        status != ConnectionStatus.AUDIO_REGION_CHANGE) {
            if (disconnectedChannel == null) {
                LOG.debug("Cannot reconnect due to null audio channel");
                return;
            }
            api.getDirectAudioController().reconnect(disconnectedChannel);
        } else if (status == ConnectionStatus.DISCONNECTED_REMOVED_FROM_GUILD) {
            // Remove audio manager as we are no longer in the guild
            api.getAudioManagersView().remove(guild.getIdLong());
        } else if (status != ConnectionStatus.AUDIO_REGION_CHANGE && status != ConnectionStatus.DISCONNECTED_KICKED_FROM_CHANNEL) {
            api.getDirectAudioController().disconnect(guild);
        }
    });
}
Also used : AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) JDAImpl(net.dv8tion.jda.internal.JDAImpl) ConnectionStatus(net.dv8tion.jda.api.audio.hooks.ConnectionStatus) Guild(net.dv8tion.jda.api.entities.Guild)

Aggregations

ConnectionStatus (net.dv8tion.jda.api.audio.hooks.ConnectionStatus)2 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)1 List (java.util.List)1 ConnectionListener (net.dv8tion.jda.api.audio.hooks.ConnectionListener)1 AudioChannel (net.dv8tion.jda.api.entities.AudioChannel)1 Guild (net.dv8tion.jda.api.entities.Guild)1 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)1 SelectMenuInteractionEvent (net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent)1 JDAImpl (net.dv8tion.jda.internal.JDAImpl)1