use of io.discloader.discloader.common.event.guild.member.GuildMemberEvent.NicknameUpdateEvent 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;
}
Aggregations