Search in sources :

Example 1 with GuildMemberUpdateEvent

use of io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent in project DiscLoader by R3alCl0ud.

the class GuildMemberUpdate method handle.

@Override
public void handle(SocketPacket packet) {
    String d = this.gson.toJson(packet.d);
    MemberJSON data = this.gson.fromJson(d, MemberJSON.class);
    IGuild guild = EntityRegistry.getGuildByID(data.guild_id);
    IGuildMember oldMember = guild.getMember(data.user.id), member = gfac.buildMember(guild, data);
    guild.addMember(member);
    if (shouldEmit() && oldMember != null) {
        GuildMemberUpdateEvent event = new GuildMemberUpdateEvent(member, oldMember);
        loader.emit(Events.GUILD_MEMBER_UPDATE, event);
        loader.emit(event);
        if (!member.getNickname().equals(oldMember.getNickname())) {
            loader.emit(new GuildMemberNicknameUpdateEvent(member, oldMember.getNickname()));
        }
        member.getRoles().forEach(role -> {
            if (!oldMember.hasRole(role)) {
                loader.emit(new GuildMemberRoleAddEvent(oldMember, member, role));
            }
        });
        oldMember.getRoles().forEach(role -> {
            if (!member.hasRole(role)) {
                loader.emit(new GuildMemberRoleRemoveEvent(oldMember, member, role));
            }
        });
    // for (member)
    }
}
Also used : GuildMemberUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent) GuildMemberRoleAddEvent(io.discloader.discloader.common.event.guild.member.GuildMemberRoleAddEvent) GuildMemberNicknameUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberNicknameUpdateEvent) GuildMemberRoleRemoveEvent(io.discloader.discloader.common.event.guild.member.GuildMemberRoleRemoveEvent) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) IGuild(io.discloader.discloader.entity.guild.IGuild) MemberJSON(io.discloader.discloader.network.json.MemberJSON)

Example 2 with GuildMemberUpdateEvent

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

use of io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent 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 4 with GuildMemberUpdateEvent

use of io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent 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

GuildMemberUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent)4 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)4 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)2 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)2 IGuild (io.discloader.discloader.entity.guild.IGuild)2 IRole (io.discloader.discloader.entity.guild.IRole)2 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 JSONObject (org.json.JSONObject)2 IEventListener (io.discloader.discloader.common.event.IEventListener)1 UserUpdateEvent (io.discloader.discloader.common.event.UserUpdateEvent)1 GuildMemberNicknameUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberNicknameUpdateEvent)1 GuildMemberRoleAddEvent (io.discloader.discloader.common.event.guild.member.GuildMemberRoleAddEvent)1 GuildMemberRoleRemoveEvent (io.discloader.discloader.common.event.guild.member.GuildMemberRoleRemoveEvent)1 User (io.discloader.discloader.core.entity.user.User)1 IUser (io.discloader.discloader.entity.user.IUser)1 MemberJSON (io.discloader.discloader.network.json.MemberJSON)1 PresenceJSON (io.discloader.discloader.network.json.PresenceJSON)1