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