use of io.discloader.discloader.common.event.IEventListener 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.IEventListener 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.IEventListener 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.event.IEventListener 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.event.IEventListener in project DiscLoader by R3alCl0ud.
the class GuildDelete method handle.
@Override
public void handle(SocketPacket packet) {
String d = this.gson.toJson(packet.d);
GuildJSON data = this.gson.fromJson(d, GuildJSON.class);
IGuild guild = null;
if (EntityRegistry.guildExists(data.id)) {
guild = EntityRegistry.getGuildByID(data.id);
guild.setup(data);
} else {
guild = EntityRegistry.addGuild(data);
}
if (!guild.isAvailable()) {
EntityRegistry.removeGuild(guild);
if (socket.status == Status.READY && loader.ready) {
GuildDeleteEvent event = new GuildDeleteEvent(guild);
loader.emit(Events.GUILD_DELETE, event);
for (IEventListener e : loader.handlers) {
e.GuildDelete(event);
}
}
}
}
Aggregations