use of io.discloader.discloader.network.rest.RESTOptions 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.network.rest.RESTOptions 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.network.rest.RESTOptions in project DiscLoader by R3alCl0ud.
the class Message method delete.
@Override
public CompletableFuture<IMessage> delete() {
CompletableFuture<IMessage> future = new CompletableFuture<>();
CompletableFuture<Void> cf = loader.rest.request(Methods.DELETE, Endpoints.message(getChannel().getID(), getID()), new RESTOptions(), Void.class);
cf.thenAcceptAsync(Null -> {
future.complete(this);
});
cf.exceptionally(ex -> {
future.completeExceptionally(ex);
return null;
});
return future;
}
use of io.discloader.discloader.network.rest.RESTOptions in project DiscLoader by R3alCl0ud.
the class DLUser method setAvatar.
/**
* Sets the currently logged in user's avatar
*
* @param avatarLocation
* The location on disk of the new avatar image
* @return A CompletableFuture that completes with {@code this} if successfull,
* or the error response if failed. Returns null if
* {@code this.id != this.loader.user.id}
*/
public CompletableFuture<DLUser> setAvatar(String avatarLocation) throws IOException {
CompletableFuture<DLUser> future = new CompletableFuture<>();
String base64 = "data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(Paths.get(avatarLocation)));
CompletableFuture<UserJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.currentUser, new RESTOptions(new JSONObject().put("avatar", base64)), UserJSON.class);
cf.thenAcceptAsync(data -> {
setup(data);
future.complete(this);
});
cf.exceptionally(ex -> {
future.completeExceptionally(ex);
return null;
});
return future;
}
use of io.discloader.discloader.network.rest.RESTOptions in project DiscLoader by R3alCl0ud.
the class ChannelCategory method createTextChannel.
@Override
public CompletableFuture<IGuildTextChannel> createTextChannel(String name) {
CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
JSONObject data = new JSONObject().put("parent_id", SnowflakeUtil.asString(this)).put("name", name).put("type", 0);
// System.out.println(data.toString());
CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class);
cf.thenAcceptAsync(channelJSON -> {
if (channelJSON != null) {
IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), false);
if (channel != null)
future.complete(channel);
}
});
cf.exceptionally(ex -> {
future.completeExceptionally(ex);
return null;
});
return future;
}
Aggregations