Search in sources :

Example 1 with IGuildEmoji

use of io.discloader.discloader.entity.guild.IGuildEmoji in project DiscLoader by R3alCl0ud.

the class Guild method createEmoji.

@Override
public CompletableFuture<IGuildEmoji> createEmoji(String name, String image, IRole... roles) {
    CompletableFuture<IGuildEmoji> future = new CompletableFuture<>();
    String base64 = null;
    try {
        base64 = new String("data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(Paths.get(image))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    String[] rids = new String[roles.length];
    for (int i = 0; i < rids.length; i++) {
        rids[i] = SnowflakeUtil.asString(roles[i]);
    }
    CreateEmoji ce = new CreateEmoji(name, base64, rids);
    CompletableFuture<EmojiJSON> cf = getLoader().rest.request(Methods.POST, Endpoints.guildEmojis(getID()), new RESTOptions(ce), EmojiJSON.class);
    cf.thenAcceptAsync(ed -> {
        future.complete(new GuildEmoji(ed, this));
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) CreateEmoji(io.discloader.discloader.entity.sendable.CreateEmoji) IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) IOException(java.io.IOException) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

Example 2 with IGuildEmoji

use of io.discloader.discloader.entity.guild.IGuildEmoji 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;
}
Also used : IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiUpdateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent) IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

Example 3 with IGuildEmoji

use of io.discloader.discloader.entity.guild.IGuildEmoji 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));
}
Also used : EmojiUpdateJSON(io.discloader.discloader.network.json.EmojiUpdateJSON) IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiUpdateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent) GuildEmojiDeleteEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiDeleteEvent) HashMap(java.util.HashMap) GuildEmoji(io.discloader.discloader.core.entity.guild.GuildEmoji) IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiCreateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiCreateEvent) IGuild(io.discloader.discloader.entity.guild.IGuild) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

Example 4 with IGuildEmoji

use of io.discloader.discloader.entity.guild.IGuildEmoji in project DiscLoader by R3alCl0ud.

the class GuildEmoji method delete.

/**
 * Deletes the emoji
 *
 * @return A Future that completes with the deleted emoji if successful.
 */
@Override
public CompletableFuture<IGuildEmoji> delete() {
    CompletableFuture<IGuildEmoji> future = new CompletableFuture<>();
    CompletableFuture<Void> cf = getLoader().rest.request(Methods.DELETE, Endpoints.guildEmoji(getGuild().getID(), getID()), new RESTOptions(), Void.class);
    cf.thenAcceptAsync(Null -> {
        future.complete(GuildEmoji.this);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions)

Example 5 with IGuildEmoji

use of io.discloader.discloader.entity.guild.IGuildEmoji 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;
}
Also used : IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiUpdateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent) IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

Aggregations

IGuildEmoji (io.discloader.discloader.entity.guild.IGuildEmoji)5 EmojiJSON (io.discloader.discloader.network.json.EmojiJSON)4 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 GuildEmojiUpdateEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent)3 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)2 IEventListener (io.discloader.discloader.common.event.IEventListener)2 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)2 GuildEmojiCreateEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiCreateEvent)1 GuildEmojiDeleteEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiDeleteEvent)1 GuildEmoji (io.discloader.discloader.core.entity.guild.GuildEmoji)1 IGuild (io.discloader.discloader.entity.guild.IGuild)1 CreateEmoji (io.discloader.discloader.entity.sendable.CreateEmoji)1 EmojiUpdateJSON (io.discloader.discloader.network.json.EmojiUpdateJSON)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1