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)
}
}
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;
}
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;
}
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);
}
}
}
Aggregations