Search in sources :

Example 11 with IGuildMember

use of io.discloader.discloader.entity.guild.IGuildMember 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 12 with IGuildMember

use of io.discloader.discloader.entity.guild.IGuildMember 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 13 with IGuildMember

use of io.discloader.discloader.entity.guild.IGuildMember in project DiscLoader by R3alCl0ud.

the class PresenceUpdate method handle.

@Override
public void handle(SocketPacket packet) {
    PresenceJSON data = this.gson.fromJson(gson.toJson(packet.d), PresenceJSON.class);
    IUser user = EntityRegistry.getUserByID(data.user.id);
    if (user == null) {
        if (data.user.username != null) {
            user = EntityRegistry.addUser(data.user);
        } else {
            return;
        }
    }
    IUser oldUser = new User(user);
    user.setup(data.user);
    if (!user.equals(oldUser)) {
        UserUpdateEvent event = new UserUpdateEvent(user, oldUser);
        loader.emit(DLUtil.Events.USER_UPDATE, event);
        loader.emit(event);
    }
    IGuild guild = data.guild_id == null ? null : EntityRegistry.getGuildByID(data.guild_id);
    if (guild != null) {
        IGuildMember oldMember = guild.getMember(user.getID()), member;
        if (oldMember == null && !data.status.equalsIgnoreCase("offline")) {
            member = guild.addMember(user, data.roles, false, false, data.nick, false);
            loader.emit(DLUtil.Events.GUILD_MEMBER_AVAILABLE, member);
        } else if (oldMember != null) {
            member = guild.addMember(user, data.roles, oldMember.isDeafened(), oldMember.isMuted(), data.nick, false);
            guild.setPresence(data);
            loader.emit(new GuildMemberUpdateEvent(member, oldMember));
        } else {
            guild.setPresence(data);
        }
    }
}
Also used : UserUpdateEvent(io.discloader.discloader.common.event.UserUpdateEvent) User(io.discloader.discloader.core.entity.user.User) IUser(io.discloader.discloader.entity.user.IUser) GuildMemberUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent) IUser(io.discloader.discloader.entity.user.IUser) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PresenceJSON(io.discloader.discloader.network.json.PresenceJSON) IGuild(io.discloader.discloader.entity.guild.IGuild)

Aggregations

IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)13 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)7 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 GuildMemberUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent)4 IGuild (io.discloader.discloader.entity.guild.IGuild)4 MemberJSON (io.discloader.discloader.network.json.MemberJSON)4 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)3 IRole (io.discloader.discloader.entity.guild.IRole)3 JSONObject (org.json.JSONObject)3 IEventListener (io.discloader.discloader.common.event.IEventListener)2 PresenceJSON (io.discloader.discloader.network.json.PresenceJSON)2 UserUpdateEvent (io.discloader.discloader.common.event.UserUpdateEvent)1 GuildMemberAddEvent (io.discloader.discloader.common.event.guild.member.GuildMemberAddEvent)1 NicknameUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberEvent.NicknameUpdateEvent)1 GuildMemberNicknameUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberNicknameUpdateEvent)1 GuildMemberRemoveEvent (io.discloader.discloader.common.event.guild.member.GuildMemberRemoveEvent)1 GuildMemberRoleAddEvent (io.discloader.discloader.common.event.guild.member.GuildMemberRoleAddEvent)1 GuildMemberRoleRemoveEvent (io.discloader.discloader.common.event.guild.member.GuildMemberRoleRemoveEvent)1 GuildMembersChunkEvent (io.discloader.discloader.common.event.guild.member.GuildMembersChunkEvent)1