use of io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent 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.guild.emoji.GuildEmojiUpdateEvent in project DiscLoader by R3alCl0ud.
the class EmojiUpdate method handle.
@Override
public void handle(SocketPacket packet) {
String d = this.gson.toJson(packet.d);
EmojiUpdateJSON data = gson.fromJson(d, EmojiUpdateJSON.class);
IGuild guild = EntityRegistry.getGuildByID(data.guild_id);
HashMap<Long, IGuildEmoji> emojis = new HashMap<>();
for (EmojiJSON e : data.emojis) {
IGuildEmoji emoji = new GuildEmoji(e, guild);
emojis.put(emoji.getID(), emoji);
}
if (shouldEmit()) {
if (data.emojis.length == guild.getEmojis().size()) {
IGuildEmoji oldEmoji;
for (IGuildEmoji emoji : emojis.values()) {
oldEmoji = guild.getEmojis().get(emoji.getID());
if (emoji.getID() == oldEmoji.getID() && !emoji.equals(oldEmoji)) {
GuildEmojiUpdateEvent event = new GuildEmojiUpdateEvent(emoji, oldEmoji);
loader.emit(Events.GUILD_EMOJI_UPDATE, event);
loader.emit(event);
}
}
} else if (data.emojis.length < guild.getEmojis().size()) {
for (IGuildEmoji emoji : guild.getEmojis().values()) {
if (!emojis.containsKey(emoji.getID())) {
GuildEmojiDeleteEvent event = new GuildEmojiDeleteEvent(emoji);
loader.emit("GuildEmojiDelete", event);
loader.emit(event);
}
}
} else if (data.emojis.length > guild.getEmojis().size()) {
for (IGuildEmoji emoji : emojis.values()) {
if (!guild.getEmojis().containsKey(emoji.getID())) {
GuildEmojiCreateEvent event = new GuildEmojiCreateEvent(emoji);
loader.emit("GuildEmojiCreate", event);
loader.emit(event);
}
}
}
}
guild.getEmojis().clear();
emojis.forEach((key, emoji) -> guild.getEmojis().put(key, emoji));
}
use of io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent 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;
}
Aggregations