Search in sources :

Example 1 with IEventListener

use of io.discloader.discloader.common.event.IEventListener 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 2 with IEventListener

use of io.discloader.discloader.common.event.IEventListener 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 3 with IEventListener

use of io.discloader.discloader.common.event.IEventListener 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)

Example 4 with IEventListener

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

the class GuildMember method takeRole.

@Override
public CompletableFuture<IGuildMember> takeRole(IRole... roles) {
    if (!guild.hasPermission(Permissions.MANAGE_ROLES)) {
        throw new PermissionsException("Insuccficient Permissions");
    }
    List<IRole> rls = getRoles();
    for (IRole role : roles) {
        if (!guild.isOwner() && role.getPosition() >= guild.getCurrentMember().getHighestRole().getPosition()) {
            throw new PermissionsException("Cannot take away roles higher than your's");
        }
        if (hasRole(role)) {
            rls.remove(role);
        }
    }
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    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);
    CompletableFuture<Void> tcf = getLoader().rest.request(Methods.PATCH, Endpoints.guildMember(getGuild().getID(), getID()), new RESTOptions(payload), Void.class);
    IEventListener iel = new EventListenerAdapter() {

        public void GuildMemberUpdate(GuildMemberUpdateEvent e) {
            if (e.getMember().getID() == getID()) {
                future.complete(e.getMember());
                getLoader().removeEventListener(this);
            }
        }
    };
    getLoader().addEventListener(iel);
    tcf.exceptionally(ex -> {
        getLoader().removeEventListener(iel);
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) IEventListener(io.discloader.discloader.common.event.IEventListener) 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 IEventListener

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

the class GuildDelete method handle.

@Override
public void handle(SocketPacket packet) {
    String d = this.gson.toJson(packet.d);
    GuildJSON data = this.gson.fromJson(d, GuildJSON.class);
    IGuild guild = null;
    if (EntityRegistry.guildExists(data.id)) {
        guild = EntityRegistry.getGuildByID(data.id);
        guild.setup(data);
    } else {
        guild = EntityRegistry.addGuild(data);
    }
    if (!guild.isAvailable()) {
        EntityRegistry.removeGuild(guild);
        if (socket.status == Status.READY && loader.ready) {
            GuildDeleteEvent event = new GuildDeleteEvent(guild);
            loader.emit(Events.GUILD_DELETE, event);
            for (IEventListener e : loader.handlers) {
                e.GuildDelete(event);
            }
        }
    }
}
Also used : GuildDeleteEvent(io.discloader.discloader.common.event.guild.GuildDeleteEvent) IEventListener(io.discloader.discloader.common.event.IEventListener) GuildJSON(io.discloader.discloader.network.json.GuildJSON) IGuild(io.discloader.discloader.entity.guild.IGuild)

Aggregations

IEventListener (io.discloader.discloader.common.event.IEventListener)5 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)4 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)4 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 JSONObject (org.json.JSONObject)3 GuildEmojiUpdateEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent)2 IGuildEmoji (io.discloader.discloader.entity.guild.IGuildEmoji)2 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)2 EmojiJSON (io.discloader.discloader.network.json.EmojiJSON)2 GuildDeleteEvent (io.discloader.discloader.common.event.guild.GuildDeleteEvent)1 NicknameUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberEvent.NicknameUpdateEvent)1 GuildMemberUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent)1 IGuild (io.discloader.discloader.entity.guild.IGuild)1 IRole (io.discloader.discloader.entity.guild.IRole)1 GuildJSON (io.discloader.discloader.network.json.GuildJSON)1