Search in sources :

Example 1 with EventListenerAdapter

use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.

the class GuildChannel method setPosition.

@Override
public CompletableFuture<IGuildChannel> setPosition(int position) {
    CompletableFuture<IGuildChannel> future = new CompletableFuture<>();
    List<JSONObject> positions = new ArrayList<>();
    boolean normalize = position < 0;
    List<IGuildChannel> channels;
    switch(getType()) {
        case CATEGORY:
            channels = new ArrayList<>(getGuild().getChannelCategories().values());
            break;
        case TEXT:
            channels = new ArrayList<>(getGuild().getTextChannels().values());
            break;
        case VOICE:
            channels = new ArrayList<>(getGuild().getVoiceChannels().values());
            break;
        default:
            channels = new ArrayList<>();
            break;
    }
    for (IGuildChannel channel : channels) {
        if (channel.getID() == getID()) {
            positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", position));
        } else if (channel.getPosition() >= position) {
            positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", channel.getPosition() + 1));
        } else {
            positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", channel.getPosition()));
        }
        if (channel.getPosition() < 0)
            normalize = true;
    }
    positions.sort((a, b) -> {
        if (a.getInt("position") < b.getInt("position"))
            return -1;
        if (a.getInt("position") > b.getInt("position"))
            return 1;
        return 0;
    });
    if (normalize) {
        for (int i = 0; i < positions.size(); i++) {
            positions.get(i).put("position", i);
        }
    }
    getLoader().addEventListener(new EventListenerAdapter() {

        @Override
        public void GuildChannelUpdate(GuildChannelUpdateEvent e) {
            if (getID() == e.getChannel().getID()) {
                future.complete(e.getChannel());
                getLoader().removeEventListener(this);
            }
        }
    });
    getLoader().rest.request(Methods.PATCH, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(true, null, positions.toString()), Void.class).exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel) ArrayList(java.util.ArrayList) GuildChannelUpdateEvent(io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter)

Example 2 with EventListenerAdapter

use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.

the class GuildEmoji method setName.

@Override
public CompletableFuture<IGuildEmoji> setName(String name) {
    if (!guild.hasPermission(Permissions.MANAGE_EMOJIS)) {
        throw new PermissionsException("Insufficient Permissions: \"MANAGE_EMOJIS\" is required to use this endpoint");
    }
    CompletableFuture<IGuildEmoji> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().put("name", name);
    IEventListener el = new EventListenerAdapter() {

        @Override
        public void GuildEmojiUpdate(GuildEmojiUpdateEvent event) {
            if (getID() == event.getEmoji().getID()) {
                future.complete(event.getEmoji());
                getLoader().removeEventListener(this);
            }
        }
    };
    getLoader().addEventListener(el).rest.request(Methods.PATCH, Endpoints.guildEmoji(getGuild().getID(), getID()), new RESTOptions(payload), EmojiJSON.class).exceptionally(ex -> {
        future.completeExceptionally(ex);
        getLoader().removeEventListener(el);
        return null;
    });
    return future;
}
Also used : IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiUpdateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent) IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

Example 3 with EventListenerAdapter

use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.

the class GuildEmoji method setRoles.

@Override
public CompletableFuture<IGuildEmoji> setRoles(IRole... roles) {
    if (!guild.hasPermission(Permissions.MANAGE_EMOJIS)) {
        throw new PermissionsException("Insufficient Permissions: \"MANAGE_EMOJIS\" is required to use this endpoint");
    }
    CompletableFuture<IGuildEmoji> future = new CompletableFuture<>();
    String[] payload = new String[roles.length];
    for (int i = 0; i < roles.length; i++) {
        payload[i] = SnowflakeUtil.asString(roles[i]);
    }
    IEventListener el = new EventListenerAdapter() {

        @Override
        public void GuildEmojiUpdate(GuildEmojiUpdateEvent event) {
            if (getID() == event.getEmoji().getID()) {
                future.complete(event.getEmoji());
                getLoader().removeEventListener(this);
            }
        }
    };
    getLoader().addEventListener(el).rest.request(Methods.PATCH, Endpoints.guildEmoji(getGuild().getID(), getID()), new RESTOptions(payload), EmojiJSON.class).exceptionally(ex -> {
        future.completeExceptionally(ex);
        getLoader().removeEventListener(el);
        return null;
    });
    return future;
}
Also used : IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiUpdateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent) IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

Example 4 with EventListenerAdapter

use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.

the class GuildMember method giveRole.

/**
 * Gives a member a new role
 *
 * @param roles
 *            The roles to give to the member
 * @return A CompletableFuture that completes with {@code this} if successful
 * @throws PermissionsException
 *             thrown if a role with a higher position than the current user's
 *             highest role is attempted to be given to the member. Also thrown
 *             if the current user doesn't have the MANAGE_ROLE permission.
 */
@Override
public CompletableFuture<IGuildMember> giveRole(IRole... roles) {
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    if (!guild.hasPermission(Permissions.MANAGE_ROLES)) {
        future.completeExceptionally(new PermissionsException("Insufficient Permissions"));
        return future;
    }
    for (IRole role : roles) {
        if (role == null)
            continue;
        if (!guild.isOwner() && role.getPosition() >= guild.getCurrentMember().getHighestRole().getPosition()) {
            future.completeExceptionally(new PermissionsException("Can not assign higher role"));
            return future;
        // throw ;
        }
    }
    List<IRole> rls = mergeRoles(roles);
    String[] ids = new String[rls.size()];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = SnowflakeUtil.asString(rls.get(i));
    }
    JSONObject payload = new JSONObject().put("roles", ids);
    System.out.println(payload);
    CompletableFuture<Void> vcf = getLoader().rest.request(Methods.PATCH, Endpoints.guildMember(getGuild().getID(), getID()), new RESTOptions(payload), Void.class);
    vcf.thenAcceptAsync(v -> {
        getLoader().addEventListener(new EventListenerAdapter() {

            public void GuildMemberUpdate(GuildMemberUpdateEvent e) {
                future.complete(e.getMember());
                getLoader().removeEventListener(this);
            }
        });
    });
    vcf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    // }
    return future;
}
Also used : IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) CompletableFuture(java.util.concurrent.CompletableFuture) IRole(io.discloader.discloader.entity.guild.IRole) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GuildMemberUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter)

Example 5 with EventListenerAdapter

use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.

the class GuildMember method setNick.

/**
 * Sets the member's nickname if the {@link DiscLoader loader} has sufficient
 * permissions. Requires the {@link Permissions#MANAGE_NICKNAMES} permission.
 *
 * @param nick
 *            The member's new nickname
 * @see Permission
 * @return A CompletableFuture that completes with {@code this} if successful
 * @throws PermissionsException
 *             thrown if the current user doesn't have the
 *             {@link Permissions#MANAGE_NICKNAMES} permission.
 */
@Override
public CompletableFuture<IGuildMember> setNick(String nick) {
    if ((equals(guild.getCurrentMember()) && !guild.hasPermission(Permissions.CHANGE_NICKNAME)) || (!equals(guild.getCurrentMember()) && !guild.hasPermission(Permissions.MANAGE_NICKNAMES))) {
        throw new PermissionsException("Insuccficient Permissions");
    }
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().put("nick", nick);
    String endpoint = equals(guild.getCurrentMember()) ? Endpoints.guildNick(guild.getID()) : Endpoints.guildMember(guild.getID(), getID());
    CompletableFuture<Void> cf = getLoader().rest.request(Methods.PATCH, endpoint, new RESTOptions(payload), Void.class);
    IEventListener iel = new EventListenerAdapter() {

        public void GuildMemberNicknameUpdated(NicknameUpdateEvent e) {
            if (e.getMember().getID() == getID() && e.getGuild().equals(guild)) {
                future.complete(e.getMember());
                getLoader().removeEventListener(this);
            }
        }
    };
    getLoader().addEventListener(iel);
    cf.exceptionally(ex -> {
        getLoader().removeEventListener(iel);
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) NicknameUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberEvent.NicknameUpdateEvent) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Aggregations

EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)7 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)5 JSONObject (org.json.JSONObject)5 IEventListener (io.discloader.discloader.common.event.IEventListener)4 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)3 GuildEmojiUpdateEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent)2 GuildMemberUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent)2 IGuildEmoji (io.discloader.discloader.entity.guild.IGuildEmoji)2 IRole (io.discloader.discloader.entity.guild.IRole)2 EmojiJSON (io.discloader.discloader.network.json.EmojiJSON)2 WebSocketFrame (com.neovisionaries.ws.client.WebSocketFrame)1 Command (io.discloader.discloader.client.command.Command)1 DLOptions (io.discloader.discloader.common.DLOptions)1 DiscLoader (io.discloader.discloader.common.DiscLoader)1 Shard (io.discloader.discloader.common.Shard)1 ShardManager (io.discloader.discloader.common.ShardManager)1 RawEvent (io.discloader.discloader.common.event.RawEvent)1 ReadyEvent (io.discloader.discloader.common.event.ReadyEvent)1