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();
}
}
}
}
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;
}
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);
}
}
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);
}
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.");
}
}
}
}
Aggregations