use of io.discloader.discloader.common.exceptions.PermissionsException 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.exceptions.PermissionsException 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.common.exceptions.PermissionsException 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.exceptions.PermissionsException in project DiscLoader by R3alCl0ud.
the class GuildChannel method edit.
@Override
public CompletableFuture<? extends IGuildChannel> edit(String name, int position, boolean nsfw, IOverwrite... overwrites) throws PermissionsException {
CompletableFuture<IGuildChannel> future = new CompletableFuture<>();
JSONObject settings = new JSONObject().put("name", name).put("position", position).put("nsfw", nsfw).put("permission_overwrites", overwrites);
loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(settings), ChannelJSON.class).thenAcceptAsync(data -> {
IChannel newChannel = EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), getGuild(), false);
if (newChannel instanceof IGuildChannel)
future.complete((IGuildChannel) newChannel);
}).exceptionally(ex -> {
future.completeExceptionally(ex);
return null;
});
return future;
}
use of io.discloader.discloader.common.exceptions.PermissionsException in project DiscLoader by R3alCl0ud.
the class TextChannel method setTopic.
public CompletableFuture<IGuildTextChannel> setTopic(String topic) {
if (!this.permissionsOf(guild.getCurrentMember()).hasAny(Permissions.MANAGE_CHANNELS, Permissions.ADMINISTRATOR) && !guild.isOwner()) {
throw new PermissionsException("Insufficient Permissions");
}
if (topic.length() > 1024) {
throw new RuntimeException("topic length [" + topic.length() + "] > 1024");
}
CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
JSONObject payload = new JSONObject().put("topic", topic);
CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(payload), ChannelJSON.class);
cf.thenAcceptAsync(data -> {
IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), getGuild(), false);
future.complete(channel);
});
cf.exceptionally(ex -> {
future.completeExceptionally(ex);
return null;
});
return future;
}
Aggregations