Search in sources :

Example 21 with JDAImpl

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

the class EntityBuilder method updateUser.

public void updateUser(UserImpl userObj, DataObject user) {
    String oldName = userObj.getName();
    String newName = user.getString("username");
    String oldDiscriminator = userObj.getDiscriminator();
    String newDiscriminator = user.get("discriminator").toString();
    String oldAvatar = userObj.getAvatarId();
    String newAvatar = user.getString("avatar", null);
    int oldFlags = userObj.getFlagsRaw();
    int newFlags = user.getInt("public_flags", 0);
    JDAImpl jda = getJDA();
    long responseNumber = jda.getResponseTotal();
    if (!oldName.equals(newName)) {
        userObj.setName(newName);
        jda.handleEvent(new UserUpdateNameEvent(jda, responseNumber, userObj, oldName));
    }
    if (!oldDiscriminator.equals(newDiscriminator)) {
        userObj.setDiscriminator(newDiscriminator);
        jda.handleEvent(new UserUpdateDiscriminatorEvent(jda, responseNumber, userObj, oldDiscriminator));
    }
    if (!Objects.equals(oldAvatar, newAvatar)) {
        userObj.setAvatarId(newAvatar);
        jda.handleEvent(new UserUpdateAvatarEvent(jda, responseNumber, userObj, oldAvatar));
    }
    if (oldFlags != newFlags) {
        userObj.setFlags(newFlags);
        jda.handleEvent(new UserUpdateFlagsEvent(jda, responseNumber, userObj, User.UserFlag.getFlags(oldFlags)));
    }
}
Also used : UserUpdateNameEvent(net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent) UserUpdateFlagsEvent(net.dv8tion.jda.api.events.user.update.UserUpdateFlagsEvent) JDAImpl(net.dv8tion.jda.internal.JDAImpl) UserUpdateDiscriminatorEvent(net.dv8tion.jda.api.events.user.update.UserUpdateDiscriminatorEvent) UserUpdateAvatarEvent(net.dv8tion.jda.api.events.user.update.UserUpdateAvatarEvent)

Example 22 with JDAImpl

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

the class ReceivedMessage method suppressEmbeds.

@Nonnull
@Override
public AuditableRestAction<Void> suppressEmbeds(boolean suppressed) {
    if (isEphemeral())
        throw new IllegalStateException("Cannot suppress embeds on ephemeral messages.");
    if (!getJDA().getSelfUser().equals(getAuthor())) {
        if (isFromType(ChannelType.PRIVATE))
            throw new PermissionException("Cannot suppress embeds of others in a PrivateChannel.");
        GuildMessageChannel gChan = getGuildChannel();
        if (!getGuild().getSelfMember().hasPermission(gChan, Permission.MESSAGE_MANAGE))
            throw new InsufficientPermissionException(gChan, Permission.MESSAGE_MANAGE);
    }
    JDAImpl jda = (JDAImpl) getJDA();
    Route.CompiledRoute route = Route.Messages.EDIT_MESSAGE.compile(getChannel().getId(), getId());
    int newFlags = flags;
    int suppressionValue = MessageFlag.EMBEDS_SUPPRESSED.getValue();
    if (suppressed)
        newFlags |= suppressionValue;
    else
        newFlags &= ~suppressionValue;
    return new AuditableRestActionImpl<>(jda, route, DataObject.empty().put("flags", newFlags));
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Example 23 with JDAImpl

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

the class AudioWebSocket method close.

protected void close(final ConnectionStatus closeStatus) {
    // Makes sure we don't run this method again after the socket.close(1000) call fires onDisconnect
    if (shutdown)
        return;
    locked((manager) -> {
        if (shutdown)
            return;
        ConnectionStatus status = closeStatus;
        ready = false;
        shutdown = true;
        stopKeepAlive();
        if (audioConnection.udpSocket != null)
            audioConnection.udpSocket.close();
        if (socket != null)
            socket.sendClose();
        audioConnection.shutdown();
        AudioChannel disconnectedChannel = manager.getConnectedChannel();
        manager.setAudioConnection(null);
        // Verify that it is actually a lost of connection and not due the connected channel being deleted.
        JDAImpl api = getJDA();
        if (status == ConnectionStatus.DISCONNECTED_KICKED_FROM_CHANNEL && (!api.getClient().isSession() || !api.getClient().isConnected())) {
            LOG.debug("Connection was closed due to session invalidate!");
            status = ConnectionStatus.ERROR_CANNOT_RESUME;
        } else if (status == ConnectionStatus.ERROR_LOST_CONNECTION || status == ConnectionStatus.DISCONNECTED_KICKED_FROM_CHANNEL) {
            // Get guild from JDA, don't use [guild] field to make sure that we don't have
            // a problem of an out of date guild stored in [guild] during a possible mWS invalidate.
            Guild connGuild = api.getGuildById(guild.getIdLong());
            if (connGuild != null) {
                AudioChannel channel = (AudioChannel) connGuild.getGuildChannelById(audioConnection.getChannel().getIdLong());
                if (channel == null)
                    status = ConnectionStatus.DISCONNECTED_CHANNEL_DELETED;
            }
        }
        changeStatus(status);
        // decide if we reconnect.
        if (shouldReconnect && // indicated that the connection was purposely closed. don't reconnect.
        status.shouldReconnect() && // Already handled.
        status != ConnectionStatus.AUDIO_REGION_CHANGE) {
            if (disconnectedChannel == null) {
                LOG.debug("Cannot reconnect due to null audio channel");
                return;
            }
            api.getDirectAudioController().reconnect(disconnectedChannel);
        } else if (status == ConnectionStatus.DISCONNECTED_REMOVED_FROM_GUILD) {
            // Remove audio manager as we are no longer in the guild
            api.getAudioManagersView().remove(guild.getIdLong());
        } else if (status != ConnectionStatus.AUDIO_REGION_CHANGE && status != ConnectionStatus.DISCONNECTED_KICKED_FROM_CHANNEL) {
            api.getDirectAudioController().disconnect(guild);
        }
    });
}
Also used : AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) JDAImpl(net.dv8tion.jda.internal.JDAImpl) ConnectionStatus(net.dv8tion.jda.api.audio.hooks.ConnectionStatus) Guild(net.dv8tion.jda.api.entities.Guild)

Example 24 with JDAImpl

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

the class GuildImpl method modifyNickname.

@Nonnull
@Override
public AuditableRestAction<Void> modifyNickname(@Nonnull Member member, String nickname) {
    Checks.notNull(member, "Member");
    checkGuild(member.getGuild(), "Member");
    if (member.equals(getSelfMember())) {
        if (!member.hasPermission(Permission.NICKNAME_CHANGE) && !member.hasPermission(Permission.NICKNAME_MANAGE))
            throw new InsufficientPermissionException(this, Permission.NICKNAME_CHANGE, "You neither have NICKNAME_CHANGE nor NICKNAME_MANAGE permission!");
    } else {
        checkPermission(Permission.NICKNAME_MANAGE);
        checkPosition(member);
    }
    JDAImpl jda = getJDA();
    return new DeferredRestAction<>(jda, () -> {
        DataObject body = DataObject.empty().put("nick", nickname == null ? "" : nickname);
        Route.CompiledRoute route;
        if (member.equals(getSelfMember()))
            route = Route.Guilds.MODIFY_SELF.compile(getId());
        else
            route = Route.Guilds.MODIFY_MEMBER.compile(getId(), member.getUser().getId());
        return new AuditableRestActionImpl<Void>(jda, route, body);
    }).setCacheCheck(() -> !Objects.equals(nickname, member.getNickname()));
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Nonnull(javax.annotation.Nonnull)

Example 25 with JDAImpl

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

the class GuildImpl method retrieveEmoteById.

@Nonnull
@Override
public RestAction<ListedEmote> retrieveEmoteById(@Nonnull String id) {
    Checks.isSnowflake(id, "Emote ID");
    JDAImpl jda = getJDA();
    return new DeferredRestAction<>(jda, ListedEmote.class, () -> {
        Emote emote = getEmoteById(id);
        if (emote != null) {
            ListedEmote listedEmote = (ListedEmote) emote;
            if (listedEmote.hasUser() || !getSelfMember().hasPermission(Permission.MANAGE_EMOTES_AND_STICKERS))
                return listedEmote;
        }
        return null;
    }, () -> {
        Route.CompiledRoute route = Route.Emotes.GET_EMOTE.compile(getId(), id);
        return new AuditableRestActionImpl<>(jda, route, (response, request) -> {
            EntityBuilder builder = GuildImpl.this.getJDA().getEntityBuilder();
            return builder.createEmote(GuildImpl.this, response.getObject());
        });
    });
}
Also used : JDAImpl(net.dv8tion.jda.internal.JDAImpl) Nonnull(javax.annotation.Nonnull)

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