Search in sources :

Example 21 with AudioManager

use of net.dv8tion.jda.api.managers.AudioManager in project PorkBot by DaMatrix.

the class ServerAudioManager method connect.

public synchronized boolean connect(VoiceChannel dstChannel, boolean errorMsg) {
    AudioManager manager = this.guild.getAudioManager();
    if (!manager.isConnected() && !manager.isAttemptingToConnect()) {
        if (!dstChannel.getGuild().getMember(dstChannel.getJDA().getSelfUser()).hasPermission(dstChannel, Permission.VOICE_CONNECT)) {
            if (errorMsg && this.lastAccessedFrom != null) {
                this.lastAccessedFrom.sendMessage("No permission to join channel `" + dstChannel.getName() + '`').queue();
            }
            return false;
        } else if (!dstChannel.getGuild().getMember(dstChannel.getJDA().getSelfUser()).hasPermission(dstChannel, Permission.VOICE_SPEAK)) {
            if (errorMsg && this.lastAccessedFrom != null) {
                this.lastAccessedFrom.sendMessage("No permission to speak in channel `" + dstChannel.getName() + '`').queue();
            }
            return false;
        }
        manager.setSelfDeafened(true);
        manager.setSelfMuted(false);
        manager.openAudioConnection(dstChannel);
    }
    return true;
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager)

Example 22 with AudioManager

use of net.dv8tion.jda.api.managers.AudioManager in project PorkBot by DaMatrix.

the class ServerAudioManager method connectedChannel.

public VoiceChannel connectedChannel() {
    AudioManager manager = this.guild.getAudioManager();
    VoiceChannel channel = manager.getQueuedAudioConnection();
    return channel == null ? manager.getConnectedChannel() : channel;
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel)

Example 23 with AudioManager

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

the class GuildImpl method getAudioManager.

@Nonnull
@Override
public AudioManager getAudioManager() {
    if (!getJDA().isIntent(GatewayIntent.GUILD_VOICE_STATES))
        throw new IllegalStateException("Cannot use audio features with disabled GUILD_VOICE_STATES intent!");
    final AbstractCacheView<AudioManager> managerMap = getJDA().getAudioManagersView();
    AudioManager mng = managerMap.get(id);
    if (mng == null) {
        // No previous manager found -> create one
        try (UnlockHook hook = managerMap.writeLock()) {
            GuildImpl cachedGuild = (GuildImpl) getJDA().getGuildById(id);
            if (cachedGuild == null)
                throw new IllegalStateException("Cannot get an AudioManager instance on an uncached Guild");
            mng = managerMap.get(id);
            if (mng == null) {
                mng = new AudioManagerImpl(cachedGuild);
                managerMap.getMap().put(id, mng);
            }
        }
    }
    return mng;
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) Nonnull(javax.annotation.Nonnull)

Example 24 with AudioManager

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

the class WebSocketSendingThread method handleAudioRequest.

private void handleAudioRequest(ConnectionRequest audioRequest) {
    long channelId = audioRequest.getChannelId();
    long guildId = audioRequest.getGuildIdLong();
    Guild guild = api.getGuildById(guildId);
    if (guild == null) {
        LOG.debug("Discarding voice request due to null guild {}", guildId);
        // race condition on guild delete, avoid NPE on DISCONNECT requests
        queuedAudioConnections.remove(guildId);
        return;
    }
    ConnectionStage stage = audioRequest.getStage();
    AudioManager audioManager = guild.getAudioManager();
    DataObject packet;
    switch(stage) {
        case RECONNECT:
        case DISCONNECT:
            packet = newVoiceClose(guildId);
            break;
        default:
        case CONNECT:
            packet = newVoiceOpen(audioManager, channelId, guild.getIdLong());
    }
    LOG.debug("Sending voice request {}", packet);
    if (send(packet)) {
        // If we didn't get RateLimited, Next request attempt will be 10 seconds from now
        // we remove it in VoiceStateUpdateHandler once we hear that it has updated our status
        // in 10 seconds we will attempt again in case we did not receive an update
        audioRequest.setNextAttemptEpoch(System.currentTimeMillis() + 10000);
        // If we are already in the correct state according to voice state
        // we will not receive a VOICE_STATE_UPDATE that would remove it
        // thus we update it here
        final GuildVoiceState voiceState = guild.getSelfMember().getVoiceState();
        client.updateAudioConnection0(guild.getIdLong(), voiceState.getChannel());
    }
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) DataObject(net.dv8tion.jda.api.utils.data.DataObject) ConnectionStage(net.dv8tion.jda.internal.audio.ConnectionStage) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) Guild(net.dv8tion.jda.api.entities.Guild)

Example 25 with AudioManager

use of net.dv8tion.jda.api.managers.AudioManager in project discord-bot-reddit-java by Glaxier0.

the class LeaveCommand method execute.

@Override
public void execute(SlashCommandInteractionEvent event) {
    GuildVoiceState botVoiceState = event.getGuild().getSelfMember().getVoiceState();
    GuildVoiceState userVoiceState = event.getMember().getVoiceState();
    if (utils.channelControl(botVoiceState, userVoiceState)) {
        GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(event);
        AudioManager audioManager = event.getGuild().getAudioManager();
        musicManager.scheduler.player.stopTrack();
        musicManager.scheduler.queue.clear();
        audioManager.closeAudioConnection();
        event.replyEmbeds(new EmbedBuilder().setDescription("Bye.").build()).queue();
    } else {
        event.replyEmbeds(new EmbedBuilder().setDescription("Please be in a same voice channel as bot.").setColor(Color.RED).build()).queue();
    }
    net.dv8tion.jda.api.entities.User user = event.getUser();
    utils.counter(user.getId(), user.getAsTag());
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GuildMusicManager(com.discord.bot.audioplayer.GuildMusicManager) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState)

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