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