Search in sources :

Example 1 with AudioChannel

use of net.dv8tion.jda.api.entities.AudioChannel in project Bean by Xirado.

the class VoiceGameCommand method executeCommand.

@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
    long appId = Long.parseUnsignedLong(event.getOption("application").getAsString());
    boolean ephemeral = event.getOption("hide") != null && event.getOption("hide").getAsBoolean();
    GuildVoiceState voiceState = event.getMember().getVoiceState();
    AudioChannel audioChannel = voiceState.getChannel();
    if (audioChannel == null) {
        event.replyEmbeds(EmbedUtil.errorEmbed("You must be in a audio-channel to do this!")).setEphemeral(true).queue();
        return;
    }
    event.deferReply(ephemeral).flatMap(hook -> createInvite(3600, 0, audioChannel, appId)).flatMap(url -> event.getHook().sendMessage(url)).queue();
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) Permission(net.dv8tion.jda.api.Permission) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) Route(net.dv8tion.jda.internal.requests.Route) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) CommandFlag(at.xirado.bean.command.CommandFlag) SlashCommandContext(at.xirado.bean.command.SlashCommandContext) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) SlashCommand(at.xirado.bean.command.SlashCommand) DataObject(net.dv8tion.jda.api.utils.data.DataObject) NotNull(org.jetbrains.annotations.NotNull) EmbedUtil(at.xirado.bean.misc.EmbedUtil) RestAction(net.dv8tion.jda.api.requests.RestAction) AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState)

Example 2 with AudioChannel

use of net.dv8tion.jda.api.entities.AudioChannel in project JDA by DV8FromTheWorld.

the class VoiceStateUpdateHandler method handleGuildVoiceState.

private void handleGuildVoiceState(DataObject content) {
    final long userId = content.getLong("user_id");
    final long guildId = content.getLong("guild_id");
    final Long channelId = !content.isNull("channel_id") ? content.getLong("channel_id") : null;
    final String sessionId = !content.isNull("session_id") ? content.getString("session_id") : null;
    boolean selfMuted = content.getBoolean("self_mute");
    boolean selfDeafened = content.getBoolean("self_deaf");
    boolean guildMuted = content.getBoolean("mute");
    boolean guildDeafened = content.getBoolean("deaf");
    boolean suppressed = content.getBoolean("suppress");
    boolean stream = content.getBoolean("self_stream");
    boolean video = content.getBoolean("self_video", false);
    String requestToSpeak = content.getString("request_to_speak_timestamp", null);
    OffsetDateTime requestToSpeakTime = null;
    long requestToSpeakTimestamp = 0L;
    if (requestToSpeak != null) {
        requestToSpeakTime = OffsetDateTime.parse(requestToSpeak);
        requestToSpeakTimestamp = requestToSpeakTime.toInstant().toEpochMilli();
    }
    Guild guild = getJDA().getGuildById(guildId);
    if (guild == null) {
        getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received a VOICE_STATE_UPDATE for a Guild that has yet to be cached. JSON: {}", content);
        return;
    }
    AudioChannel channel = null;
    if (channelId != null) {
        channel = (AudioChannel) guild.getGuildChannelById(channelId);
    }
    if (channel == null && (channelId != null)) {
        getJDA().getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received VOICE_STATE_UPDATE for an AudioChannel that has yet to be cached. JSON: {}", content);
        return;
    }
    DataObject memberJson = content.getObject("member");
    MemberImpl member = getJDA().getEntityBuilder().createMember((GuildImpl) guild, memberJson);
    if (member == null)
        return;
    GuildVoiceStateImpl vState = (GuildVoiceStateImpl) member.getVoiceState();
    if (vState == null)
        return;
    // Cant really see a reason for an event for this
    vState.setSessionId(sessionId);
    VoiceDispatchInterceptor voiceInterceptor = getJDA().getVoiceInterceptor();
    boolean isSelf = guild.getSelfMember().equals(member);
    boolean wasMute = vState.isMuted();
    boolean wasDeaf = vState.isDeafened();
    if (selfMuted != vState.isSelfMuted()) {
        vState.setSelfMuted(selfMuted);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceSelfMuteEvent(getJDA(), responseNumber, member));
    }
    if (selfDeafened != vState.isSelfDeafened()) {
        vState.setSelfDeafened(selfDeafened);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceSelfDeafenEvent(getJDA(), responseNumber, member));
    }
    if (guildMuted != vState.isGuildMuted()) {
        vState.setGuildMuted(guildMuted);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceGuildMuteEvent(getJDA(), responseNumber, member));
    }
    if (guildDeafened != vState.isGuildDeafened()) {
        vState.setGuildDeafened(guildDeafened);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceGuildDeafenEvent(getJDA(), responseNumber, member));
    }
    if (suppressed != vState.isSuppressed()) {
        vState.setSuppressed(suppressed);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceSuppressEvent(getJDA(), responseNumber, member));
    }
    if (stream != vState.isStream()) {
        vState.setStream(stream);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceStreamEvent(getJDA(), responseNumber, member, stream));
    }
    if (video != vState.isSendingVideo()) {
        vState.setVideo(video);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceVideoEvent(getJDA(), responseNumber, member, video));
    }
    if (wasMute != vState.isMuted())
        getJDA().handleEvent(new GuildVoiceMuteEvent(getJDA(), responseNumber, member));
    if (wasDeaf != vState.isDeafened())
        getJDA().handleEvent(new GuildVoiceDeafenEvent(getJDA(), responseNumber, member));
    if (requestToSpeakTimestamp != vState.getRequestToSpeak()) {
        OffsetDateTime oldRequestToSpeak = vState.getRequestToSpeakTimestamp();
        vState.setRequestToSpeak(requestToSpeakTime);
        getJDA().handleEvent(new GuildVoiceRequestToSpeakEvent(getJDA(), responseNumber, member, oldRequestToSpeak, requestToSpeakTime));
    }
    if (!Objects.equals(channel, vState.getChannel())) {
        AudioChannel oldChannel = vState.getChannel();
        vState.setConnectedChannel(channel);
        if (oldChannel == null) {
            ((AudioChannelMixin<?>) channel).getConnectedMembersMap().put(userId, member);
            getJDA().getEntityBuilder().updateMemberCache(member);
            getJDA().handleEvent(new GuildVoiceJoinEvent(getJDA(), responseNumber, member));
        } else if (channel == null) {
            ((AudioChannelMixin<?>) oldChannel).getConnectedMembersMap().remove(userId);
            if (isSelf)
                getJDA().getDirectAudioController().update(guild, null);
            getJDA().getEntityBuilder().updateMemberCache(member, memberJson.isNull("joined_at"));
            getJDA().handleEvent(new GuildVoiceLeaveEvent(getJDA(), responseNumber, member, oldChannel));
        } else {
            AudioManagerImpl mng = (AudioManagerImpl) getJDA().getAudioManagersView().get(guildId);
            // If the currently connected account is the one that is being moved
            if (isSelf && mng != null && voiceInterceptor == null) {
                // then change the channel we expect to be connected to.
                if (mng.isConnected())
                    mng.setConnectedChannel(channel);
                // (handled by updateAudioConnection)
                if (mng.isConnected())
                    getJDA().getDirectAudioController().update(guild, channel);
            // If we are not already connected this will be removed by VOICE_SERVER_UPDATE
            }
            ((AudioChannelMixin<?>) channel).getConnectedMembersMap().put(userId, member);
            ((AudioChannelMixin<?>) oldChannel).getConnectedMembersMap().remove(userId);
            getJDA().getEntityBuilder().updateMemberCache(member);
            getJDA().handleEvent(new GuildVoiceMoveEvent(getJDA(), responseNumber, member, oldChannel));
        }
    }
    if (isSelf && voiceInterceptor != null) {
        if (voiceInterceptor.onVoiceStateUpdate(new VoiceDispatchInterceptor.VoiceStateUpdate(channel, vState, allContent)))
            getJDA().getDirectAudioController().update(guild, channel);
    }
    ((GuildImpl) guild).updateRequestToSpeak();
}
Also used : Guild(net.dv8tion.jda.api.entities.Guild) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) AudioChannelMixin(net.dv8tion.jda.internal.entities.mixin.channel.middleman.AudioChannelMixin) VoiceDispatchInterceptor(net.dv8tion.jda.api.hooks.VoiceDispatchInterceptor) DataObject(net.dv8tion.jda.api.utils.data.DataObject) OffsetDateTime(java.time.OffsetDateTime) GuildVoiceStateImpl(net.dv8tion.jda.internal.entities.GuildVoiceStateImpl)

Example 3 with AudioChannel

use of net.dv8tion.jda.api.entities.AudioChannel in project JDA by DV8FromTheWorld.

the class VoiceServerUpdateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long guildId = content.getLong("guild_id");
    if (getJDA().getGuildSetupController().isLocked(guildId))
        return guildId;
    Guild guild = getJDA().getGuildById(guildId);
    if (guild == null)
        throw new IllegalArgumentException("Attempted to start audio connection with Guild that doesn't exist!");
    getJDA().getDirectAudioController().update(guild, guild.getSelfMember().getVoiceState().getChannel());
    if (content.isNull("endpoint")) {
        // to actually connect to the audio server.
        return null;
    }
    // Strip the port from the endpoint.
    String endpoint = content.getString("endpoint").replace(":80", "");
    String token = content.getString("token");
    String sessionId = guild.getSelfMember().getVoiceState().getSessionId();
    if (sessionId == null)
        throw new IllegalArgumentException("Attempted to create audio connection without having a session ID. Did VOICE_STATE_UPDATED fail?");
    VoiceDispatchInterceptor voiceInterceptor = getJDA().getVoiceInterceptor();
    if (voiceInterceptor != null) {
        voiceInterceptor.onVoiceServerUpdate(new VoiceDispatchInterceptor.VoiceServerUpdate(guild, endpoint, token, sessionId, allContent));
        return null;
    }
    AudioManagerImpl audioManager = (AudioManagerImpl) getJDA().getAudioManagersView().get(guildId);
    if (audioManager == null) {
        WebSocketClient.LOG.debug("Received a VOICE_SERVER_UPDATE but JDA is not currently connected nor attempted to connect " + "to a VoiceChannel. Assuming that this is caused by another client running on this account. " + "Ignoring the event.");
        return null;
    }
    MiscUtil.locked(audioManager.CONNECTION_LOCK, () -> {
        // Synchronized to prevent attempts to close while setting up initial objects.
        AudioChannel target = guild.getSelfMember().getVoiceState().getChannel();
        if (target == null) {
            WebSocketClient.LOG.warn("Ignoring VOICE_SERVER_UPDATE for unknown channel");
            return;
        }
        AudioConnection connection = new AudioConnection(audioManager, endpoint, sessionId, token, target);
        audioManager.setAudioConnection(connection);
        connection.startConnection();
    });
    return null;
}
Also used : VoiceDispatchInterceptor(net.dv8tion.jda.api.hooks.VoiceDispatchInterceptor) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) AudioConnection(net.dv8tion.jda.internal.audio.AudioConnection) Guild(net.dv8tion.jda.api.entities.Guild)

Example 4 with AudioChannel

use of net.dv8tion.jda.api.entities.AudioChannel in project Lauren by Yuhtin.

the class TrackScheduler method onTrackStart.

@Override
public void onTrackStart(AudioPlayer player, AudioTrack track) {
    AudioChannel voice = TrackManager.of(guild).getAudio();
    voice.getGuild().getAudioManager().openAudioConnection(voice);
}
Also used : AudioChannel(net.dv8tion.jda.api.entities.AudioChannel)

Example 5 with AudioChannel

use of net.dv8tion.jda.api.entities.AudioChannel in project Wylx by Wylx-Bot.

the class VoiceChannelProcessing method checkVoiceChannel.

private void checkVoiceChannel(long guildID) {
    GuildMusicManager guildMusicManager = WylxPlayerManager.getInstance().getGuildManager(guildID);
    AudioManager manager = Wylx.getInstance().getGuildAudioManager(guildID);
    AudioChannel channel = manager.getConnectedChannel();
    if (channel == null) {
        guildMusicManager.stop();
        return;
    }
    // Leave if we are the only user left
    if (manager.isConnected() && channel.getMembers().size() == 1) {
        logger.debug("Leaving {} due to inactivity", guildID);
        guildMusicManager.stop();
    }
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) GuildMusicManager(Core.Music.GuildMusicManager) AudioChannel(net.dv8tion.jda.api.entities.AudioChannel)

Aggregations

AudioChannel (net.dv8tion.jda.api.entities.AudioChannel)10 Guild (net.dv8tion.jda.api.entities.Guild)4 DataObject (net.dv8tion.jda.api.utils.data.DataObject)3 GuildVoiceState (net.dv8tion.jda.api.entities.GuildVoiceState)2 Member (net.dv8tion.jda.api.entities.Member)2 VoiceDispatchInterceptor (net.dv8tion.jda.api.hooks.VoiceDispatchInterceptor)2 JDAImpl (net.dv8tion.jda.internal.JDAImpl)2 AudioChannelMixin (net.dv8tion.jda.internal.entities.mixin.channel.middleman.AudioChannelMixin)2 AudioManagerImpl (net.dv8tion.jda.internal.managers.AudioManagerImpl)2 GuildMusicManager (Core.Music.GuildMusicManager)1 CommandFlag (at.xirado.bean.command.CommandFlag)1 SlashCommand (at.xirado.bean.command.SlashCommand)1 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)1 EmbedUtil (at.xirado.bean.misc.EmbedUtil)1 GuildAudioPlayer (at.xirado.bean.music.GuildAudioPlayer)1 GuildMusicManager (com.discord.bot.audioplayer.GuildMusicManager)1 OffsetDateTime (java.time.OffsetDateTime)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Permission (net.dv8tion.jda.api.Permission)1 ConnectionListener (net.dv8tion.jda.api.audio.hooks.ConnectionListener)1