Search in sources :

Example 6 with JDAImpl

use of net.dv8tion.jda.internal.JDAImpl in project JDA by DV8FromTheWorld.

the class InviteImpl method resolve.

public static RestAction<Invite> resolve(final JDA api, final String code, final boolean withCounts) {
    Checks.notNull(code, "code");
    Checks.notNull(api, "api");
    Route.CompiledRoute route = Route.Invites.GET_INVITE.compile(code);
    if (withCounts)
        route = route.withQueryParams("with_counts", "true");
    JDAImpl jda = (JDAImpl) api;
    return new RestActionImpl<>(api, route, (response, request) -> jda.getEntityBuilder().createInvite(response.getObject()));
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Route(net.dv8tion.jda.internal.requests.Route)

Example 7 with JDAImpl

use of net.dv8tion.jda.internal.JDAImpl in project JDA by DV8FromTheWorld.

the class GuildSetupNode method handleCreate.

void handleCreate(DataObject obj) {
    if (partialGuild == null) {
        partialGuild = obj;
    } else {
        for (String key : obj.keys()) {
            partialGuild.put(key, obj.opt(key).orElse(null));
        }
    }
    boolean unavailable = partialGuild.getBoolean("unavailable");
    boolean wasMarkedUnavailable = this.markedUnavailable;
    this.markedUnavailable = unavailable;
    if (unavailable) {
        if (!firedUnavailableJoin && isJoin()) {
            firedUnavailableJoin = true;
            JDAImpl api = getController().getJDA();
            api.handleEvent(new UnavailableGuildJoinedEvent(api, api.getResponseTotal(), id));
        }
        return;
    }
    ensureMembers();
}
Also used : UnavailableGuildJoinedEvent(net.dv8tion.jda.api.events.guild.UnavailableGuildJoinedEvent) JDAImpl(net.dv8tion.jda.internal.JDAImpl)

Example 8 with JDAImpl

use of net.dv8tion.jda.internal.JDAImpl 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 JDAImpl

use of net.dv8tion.jda.internal.JDAImpl in project JDA by DV8FromTheWorld.

the class MessageCreateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    MessageType type = MessageType.fromId(content.getInt("type"));
    if (type == MessageType.UNKNOWN) {
        WebSocketClient.LOG.debug("JDA received a message of unknown type. Type: {}  JSON: {}", type, content);
        return null;
    }
    // Drop ephemeral messages since they are broken due to missing guild_id
    if ((content.getInt("flags", 0) & 64) != 0)
        return null;
    JDAImpl jda = getJDA();
    Guild guild = null;
    if (!content.isNull("guild_id")) {
        long guildId = content.getLong("guild_id");
        if (jda.getGuildSetupController().isLocked(guildId))
            return guildId;
        guild = api.getGuildById(guildId);
        if (guild == null) {
            api.getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
            EventCache.LOG.debug("Received message for a guild that JDA does not currently have cached");
            return null;
        }
    }
    Message message;
    try {
        message = jda.getEntityBuilder().createMessageWithLookup(content, guild, true);
    } catch (IllegalArgumentException e) {
        switch(e.getMessage()) {
            case EntityBuilder.MISSING_CHANNEL:
                {
                    final long channelId = content.getLong("channel_id");
                    jda.getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
                    EventCache.LOG.debug("Received a message for a channel that JDA does not currently have cached");
                    return null;
                }
            case EntityBuilder.MISSING_USER:
                {
                    final long authorId = content.getObject("author").getLong("id");
                    jda.getEventCache().cache(EventCache.Type.USER, authorId, responseNumber, allContent, this::handle);
                    EventCache.LOG.debug("Received a message for a user that JDA does not currently have cached");
                    return null;
                }
            case EntityBuilder.UNKNOWN_MESSAGE_TYPE:
                {
                    WebSocketClient.LOG.debug("Ignoring message with unknown type: {}", content);
                    return null;
                }
            default:
                throw e;
        }
    }
    MessageChannel channel = message.getChannel();
    ChannelType channelType = channel.getType();
    // Update the variable that tracks the latest message received in the channel
    ((MessageChannelMixin<?>) channel).setLatestMessageIdLong(message.getIdLong());
    if (channelType.isGuild()) {
        if (channelType.isThread()) {
            ThreadChannelImpl gThread = (ThreadChannelImpl) channel;
            // Discord will only ever allow this property to show up to 50,
            // so we don't want to update it to be over 50 because we don't want users to use it incorrectly.
            int newMessageCount = Math.min(gThread.getMessageCount() + 1, 50);
            gThread.setMessageCount(newMessageCount);
        }
    } else {
        api.usedPrivateChannel(channel.getIdLong());
    }
    jda.handleEvent(new MessageReceivedEvent(jda, responseNumber, message));
    return null;
}
Also used : MessageChannelMixin(net.dv8tion.jda.internal.entities.mixin.channel.middleman.MessageChannelMixin) ThreadChannelImpl(net.dv8tion.jda.internal.entities.ThreadChannelImpl) JDAImpl(net.dv8tion.jda.internal.JDAImpl) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent)

Example 10 with JDAImpl

use of net.dv8tion.jda.internal.JDAImpl in project JDA by DV8FromTheWorld.

the class ChannelCreateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    ChannelType type = ChannelType.fromId(content.getInt("type"));
    long guildId = 0;
    JDAImpl jda = getJDA();
    if (type.isGuild()) {
        guildId = content.getLong("guild_id");
        if (jda.getGuildSetupController().isLocked(guildId))
            return guildId;
    }
    Channel channel = buildChannel(type, content, guildId);
    if (channel == null) {
        WebSocketClient.LOG.debug("Discord provided an CREATE_CHANNEL event with an unknown channel type! JSON: {}", content);
        return null;
    }
    jda.handleEvent(new ChannelCreateEvent(jda, responseNumber, channel));
    return null;
}
Also used : ChannelCreateEvent(net.dv8tion.jda.api.events.channel.ChannelCreateEvent) Channel(net.dv8tion.jda.api.entities.Channel) JDAImpl(net.dv8tion.jda.internal.JDAImpl) ChannelType(net.dv8tion.jda.api.entities.ChannelType)

Aggregations

JDAImpl (net.dv8tion.jda.internal.JDAImpl)43 Nonnull (javax.annotation.Nonnull)19 Route (net.dv8tion.jda.internal.requests.Route)12 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)10 DataObject (net.dv8tion.jda.api.utils.data.DataObject)8 DataArray (net.dv8tion.jda.api.utils.data.DataArray)7 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)6 CheckReturnValue (javax.annotation.CheckReturnValue)5 AuditableRestActionImpl (net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl)5 ExceptionEvent (net.dv8tion.jda.api.events.ExceptionEvent)4 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)4 ArrayList (java.util.ArrayList)3 LoginException (javax.security.auth.login.LoginException)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 JDA (net.dv8tion.jda.api.JDA)3 WebSocketClient (net.dv8tion.jda.internal.requests.WebSocketClient)3 Parser (com.jagrosh.jagtag.Parser)2 ByteBuffer (java.nio.ByteBuffer)2 List (java.util.List)2 MessageConfig (me.duncte123.botcommons.messaging.MessageConfig)2