Search in sources :

Example 6 with AudioManager

use of net.dv8tion.jda.api.managers.AudioManager in project JDA by DV8FromTheWorld.

the class WebSocketClient method updateAudioManagerReferences.

protected void updateAudioManagerReferences() {
    AbstractCacheView<AudioManager> managerView = api.getAudioManagersView();
    try (UnlockHook hook = managerView.writeLock()) {
        final TLongObjectMap<AudioManager> managerMap = managerView.getMap();
        if (managerMap.size() > 0)
            LOG.trace("Updating AudioManager references");
        for (TLongObjectIterator<AudioManager> it = managerMap.iterator(); it.hasNext(); ) {
            it.advance();
            final long guildId = it.key();
            final AudioManagerImpl mng = (AudioManagerImpl) it.value();
            GuildImpl guild = (GuildImpl) api.getGuildById(guildId);
            if (guild == null) {
                // We no longer have access to the guild that this audio manager was for. Set the value to null.
                queuedAudioConnections.remove(guildId);
                mng.closeAudioConnection(ConnectionStatus.DISCONNECTED_REMOVED_DURING_RECONNECT);
                it.remove();
            }
        }
    }
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook)

Example 7 with AudioManager

use of net.dv8tion.jda.api.managers.AudioManager in project JDA by DV8FromTheWorld.

the class GuildDeleteHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long id = content.getLong("id");
    GuildSetupController setupController = getJDA().getGuildSetupController();
    boolean wasInit = setupController.onDelete(id, content);
    if (wasInit || setupController.isUnavailable(id))
        return null;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(id);
    boolean unavailable = content.getBoolean("unavailable");
    if (guild == null) {
        // getJDA().getEventCache().cache(EventCache.Type.GUILD, id, () -> handle(responseNumber, allContent));
        WebSocketClient.LOG.debug("Received GUILD_DELETE for a Guild that is not currently cached. ID: {} unavailable: {}", id, unavailable);
        return null;
    }
    // ignore the event
    if (setupController.isUnavailable(id) && unavailable)
        return null;
    // Remove everything from global cache
    // this prevents some race-conditions for getting audio managers from guilds
    SnowflakeCacheViewImpl<Guild> guildView = getJDA().getGuildsView();
    SnowflakeCacheViewImpl<StageChannel> stageView = getJDA().getStageChannelView();
    SnowflakeCacheViewImpl<TextChannel> textView = getJDA().getTextChannelsView();
    SnowflakeCacheViewImpl<ThreadChannel> threadView = getJDA().getThreadChannelsView();
    SnowflakeCacheViewImpl<NewsChannel> newsView = getJDA().getNewsChannelView();
    SnowflakeCacheViewImpl<VoiceChannel> voiceView = getJDA().getVoiceChannelsView();
    SnowflakeCacheViewImpl<Category> categoryView = getJDA().getCategoriesView();
    guildView.remove(id);
    try (UnlockHook hook = stageView.writeLock()) {
        guild.getStageChannelCache().forEachUnordered(chan -> stageView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = textView.writeLock()) {
        guild.getTextChannelCache().forEachUnordered(chan -> textView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = threadView.writeLock()) {
        guild.getThreadChannelsView().forEachUnordered(chan -> threadView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = newsView.writeLock()) {
        guild.getNewsChannelCache().forEachUnordered(chan -> newsView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = voiceView.writeLock()) {
        guild.getVoiceChannelCache().forEachUnordered(chan -> voiceView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = categoryView.writeLock()) {
        guild.getCategoryCache().forEachUnordered(chan -> categoryView.getMap().remove(chan.getIdLong()));
    }
    // Clear audio connection
    getJDA().getClient().removeAudioConnection(id);
    final AbstractCacheView<AudioManager> audioManagerView = getJDA().getAudioManagersView();
    // read-lock access/release
    final AudioManagerImpl manager = (AudioManagerImpl) audioManagerView.get(id);
    if (manager != null)
        // connection-lock access/release
        manager.closeAudioConnection(ConnectionStatus.DISCONNECTED_REMOVED_FROM_GUILD);
    // write-lock access/release
    audioManagerView.remove(id);
    // cleaning up all users that we do not share a guild with anymore
    // Anything left in memberIds will be removed from the main userMap
    // Use a new HashSet so that we don't actually modify the Member map so it doesn't affect Guild#getMembers for the leave event.
    // copies keys
    TLongSet memberIds = guild.getMembersView().keySet();
    getJDA().getGuildCache().stream().map(GuildImpl.class::cast).forEach(g -> memberIds.removeAll(g.getMembersView().keySet()));
    // Remember, everything left in memberIds is removed from the userMap
    SnowflakeCacheViewImpl<User> userView = getJDA().getUsersView();
    try (UnlockHook hook = userView.writeLock()) {
        long selfId = getJDA().getSelfUser().getIdLong();
        memberIds.forEach(memberId -> {
            if (memberId == selfId)
                // don't remove selfUser from cache
                return true;
            userView.remove(memberId);
            getJDA().getEventCache().clear(EventCache.Type.USER, memberId);
            return true;
        });
    }
    if (unavailable) {
        setupController.onUnavailable(id);
        getJDA().handleEvent(new GuildUnavailableEvent(getJDA(), responseNumber, guild));
    } else {
        getJDA().handleEvent(new GuildLeaveEvent(getJDA(), responseNumber, guild));
    }
    getJDA().getEventCache().clear(EventCache.Type.GUILD, id);
    return null;
}
Also used : GuildLeaveEvent(net.dv8tion.jda.api.events.guild.GuildLeaveEvent) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) AudioManager(net.dv8tion.jda.api.managers.AudioManager) GuildUnavailableEvent(net.dv8tion.jda.api.events.guild.GuildUnavailableEvent) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) TLongSet(gnu.trove.set.TLongSet)

Example 8 with AudioManager

use of net.dv8tion.jda.api.managers.AudioManager 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 9 with AudioManager

use of net.dv8tion.jda.api.managers.AudioManager in project JDA by DV8FromTheWorld.

the class AudioEchoExample method connectTo.

/**
 * Connect to requested channel and start echo handler
 *
 * @param channel
 *        The channel to connect to
 */
private void connectTo(AudioChannel channel) {
    Guild guild = channel.getGuild();
    // Get an audio manager for this guild, this will be created upon first use for each guild
    AudioManager audioManager = guild.getAudioManager();
    // Create our Send/Receive handler for the audio connection
    EchoHandler handler = new EchoHandler();
    // The order of the following instructions does not matter!
    // Set the sending handler to our echo system
    audioManager.setSendingHandler(handler);
    // Set the receiving handler to the same echo system, otherwise we can't echo anything
    audioManager.setReceivingHandler(handler);
    // Connect to the voice channel
    audioManager.openAudioConnection(channel);
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager)

Example 10 with AudioManager

use of net.dv8tion.jda.api.managers.AudioManager in project DiscordSoundboard by Darkside138.

the class SoundPlayerImpl method moveToChannel.

/**
 * Moves to the specified voice channel.
 *
 * @param channel - The channel specified.
 */
private void moveToChannel(VoiceChannel channel, Guild guild) {
    AudioManager audioManager = guild.getAudioManager();
    audioManager.openAudioConnection(channel);
    int i = 0;
    int waitTime = 100;
    int maxIterations = 40;
    // Wait for the audio connection to be ready before proceeding.
    synchronized (this) {
        while (!audioManager.isConnected()) {
            try {
                wait(waitTime);
                i++;
                if (i >= maxIterations) {
                    // break out if after 1 second it doesn't get a connection;
                    break;
                }
            } catch (InterruptedException e) {
                LOG.warn("Waiting for audio connection was interrupted.");
            }
        }
    }
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager)

Aggregations

AudioManager (net.dv8tion.jda.api.managers.AudioManager)48 GuildVoiceState (net.dv8tion.jda.api.entities.GuildVoiceState)12 Member (net.dv8tion.jda.api.entities.Member)10 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)9 Guild (net.dv8tion.jda.api.entities.Guild)9 List (java.util.List)8 VoiceChannel (net.dv8tion.jda.api.entities.VoiceChannel)8 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)5 EventWaiter (com.jagrosh.jdautilities.commons.waiter.EventWaiter)4 Nonnull (javax.annotation.Nonnull)4 GuildMusicManager (me.fero.ascent.lavaplayer.GuildMusicManager)4 Permission (net.dv8tion.jda.api.Permission)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3 net.dv8tion.jda.api.entities (net.dv8tion.jda.api.entities)3 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)3