use of net.dv8tion.jda.core.entities.impl.EmoteImpl 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();
}
Aggregations