use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.
the class GuildChannel method setPosition.
@Override
public CompletableFuture<IGuildChannel> setPosition(int position) {
CompletableFuture<IGuildChannel> future = new CompletableFuture<>();
List<JSONObject> positions = new ArrayList<>();
boolean normalize = position < 0;
List<IGuildChannel> channels;
switch(getType()) {
case CATEGORY:
channels = new ArrayList<>(getGuild().getChannelCategories().values());
break;
case TEXT:
channels = new ArrayList<>(getGuild().getTextChannels().values());
break;
case VOICE:
channels = new ArrayList<>(getGuild().getVoiceChannels().values());
break;
default:
channels = new ArrayList<>();
break;
}
for (IGuildChannel channel : channels) {
if (channel.getID() == getID()) {
positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", position));
} else if (channel.getPosition() >= position) {
positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", channel.getPosition() + 1));
} else {
positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", channel.getPosition()));
}
if (channel.getPosition() < 0)
normalize = true;
}
positions.sort((a, b) -> {
if (a.getInt("position") < b.getInt("position"))
return -1;
if (a.getInt("position") > b.getInt("position"))
return 1;
return 0;
});
if (normalize) {
for (int i = 0; i < positions.size(); i++) {
positions.get(i).put("position", i);
}
}
getLoader().addEventListener(new EventListenerAdapter() {
@Override
public void GuildChannelUpdate(GuildChannelUpdateEvent e) {
if (getID() == e.getChannel().getID()) {
future.complete(e.getChannel());
getLoader().removeEventListener(this);
}
}
});
getLoader().rest.request(Methods.PATCH, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(true, null, positions.toString()), Void.class).exceptionally(ex -> {
future.completeExceptionally(ex);
return null;
});
return future;
}
use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.
the class GuildEmoji method setName.
@Override
public CompletableFuture<IGuildEmoji> setName(String name) {
if (!guild.hasPermission(Permissions.MANAGE_EMOJIS)) {
throw new PermissionsException("Insufficient Permissions: \"MANAGE_EMOJIS\" is required to use this endpoint");
}
CompletableFuture<IGuildEmoji> future = new CompletableFuture<>();
JSONObject payload = new JSONObject().put("name", name);
IEventListener el = new EventListenerAdapter() {
@Override
public void GuildEmojiUpdate(GuildEmojiUpdateEvent event) {
if (getID() == event.getEmoji().getID()) {
future.complete(event.getEmoji());
getLoader().removeEventListener(this);
}
}
};
getLoader().addEventListener(el).rest.request(Methods.PATCH, Endpoints.guildEmoji(getGuild().getID(), getID()), new RESTOptions(payload), EmojiJSON.class).exceptionally(ex -> {
future.completeExceptionally(ex);
getLoader().removeEventListener(el);
return null;
});
return future;
}
use of io.discloader.discloader.common.event.EventListenerAdapter in project DiscLoader by R3alCl0ud.
the class GuildEmoji method setRoles.
@Override
public CompletableFuture<IGuildEmoji> setRoles(IRole... roles) {
if (!guild.hasPermission(Permissions.MANAGE_EMOJIS)) {
throw new PermissionsException("Insufficient Permissions: \"MANAGE_EMOJIS\" is required to use this endpoint");
}
CompletableFuture<IGuildEmoji> future = new CompletableFuture<>();
String[] payload = new String[roles.length];
for (int i = 0; i < roles.length; i++) {
payload[i] = SnowflakeUtil.asString(roles[i]);
}
IEventListener el = new EventListenerAdapter() {
@Override
public void GuildEmojiUpdate(GuildEmojiUpdateEvent event) {
if (getID() == event.getEmoji().getID()) {
future.complete(event.getEmoji());
getLoader().removeEventListener(this);
}
}
};
getLoader().addEventListener(el).rest.request(Methods.PATCH, Endpoints.guildEmoji(getGuild().getID(), getID()), new RESTOptions(payload), EmojiJSON.class).exceptionally(ex -> {
future.completeExceptionally(ex);
getLoader().removeEventListener(el);
return null;
});
return future;
}
use of io.discloader.discloader.common.event.EventListenerAdapter 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.EventListenerAdapter 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