Search in sources :

Example 1 with Response

use of net.dv8tion.jda.core.requests.Response in project pokeraidbot by magnusmickelsson.

the class InstallEmotesCommand method createEmote.

// Code taken from JDA's GuildController since they have a limitation that bot accounts can't upload emotes.
private void createEmote(String iconName, CommandEvent commandEvent, Icon icon, Role... roles) {
    JSONObject body = new JSONObject();
    body.put("name", iconName);
    body.put("image", icon.getEncoding());
    if (// making sure none of the provided roles are null before mapping them to the snowflake id
    roles.length > 0) {
        body.put("roles", Stream.of(roles).filter(Objects::nonNull).map(ISnowflake::getId).collect(Collectors.toSet()));
    }
    GuildImpl guild = (GuildImpl) commandEvent.getGuild();
    JDA jda = commandEvent.getJDA();
    Route.CompiledRoute route = Route.Emotes.CREATE_EMOTE.compile(guild.getId());
    AuditableRestAction<Emote> action = new AuditableRestAction<Emote>(jda, route, body) {

        @Override
        protected void handleResponse(Response response, Request<Emote> request) {
            if (response.isOk()) {
                JSONObject obj = response.getObject();
                final long id = obj.getLong("id");
                String name = obj.getString("name");
                EmoteImpl emote = new EmoteImpl(id, guild).setName(name);
                // managed is false by default, should always be false for emotes created by client accounts.
                JSONArray rolesArr = obj.getJSONArray("roles");
                Set<Role> roleSet = emote.getRoleSet();
                for (int i = 0; i < rolesArr.length(); i++) {
                    roleSet.add(guild.getRoleById(rolesArr.getString(i)));
                }
                // put emote into cache
                guild.getEmoteMap().put(id, emote);
                request.onSuccess(emote);
            } else {
                request.onFailure(response);
                throw new RuntimeException("Couldn't install emojis. " + "Make sure that pokeraidbot has access to manage emojis.");
            }
        }
    };
    action.queue();
}
Also used : AuditableRestAction(net.dv8tion.jda.core.requests.restaction.AuditableRestAction) JDA(net.dv8tion.jda.core.JDA) Emote(net.dv8tion.jda.core.entities.Emote) Request(net.dv8tion.jda.core.requests.Request) JSONArray(org.json.JSONArray) Response(net.dv8tion.jda.core.requests.Response) Role(net.dv8tion.jda.core.entities.Role) GuildImpl(net.dv8tion.jda.core.entities.impl.GuildImpl) EmoteImpl(net.dv8tion.jda.core.entities.impl.EmoteImpl) JSONObject(org.json.JSONObject) Route(net.dv8tion.jda.core.requests.Route) ISnowflake(net.dv8tion.jda.core.entities.ISnowflake)

Aggregations

JDA (net.dv8tion.jda.core.JDA)1 Emote (net.dv8tion.jda.core.entities.Emote)1 ISnowflake (net.dv8tion.jda.core.entities.ISnowflake)1 Role (net.dv8tion.jda.core.entities.Role)1 EmoteImpl (net.dv8tion.jda.core.entities.impl.EmoteImpl)1 GuildImpl (net.dv8tion.jda.core.entities.impl.GuildImpl)1 Request (net.dv8tion.jda.core.requests.Request)1 Response (net.dv8tion.jda.core.requests.Response)1 Route (net.dv8tion.jda.core.requests.Route)1 AuditableRestAction (net.dv8tion.jda.core.requests.restaction.AuditableRestAction)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1