Search in sources :

Example 1 with IconImpl

use of org.javacord.core.entity.IconImpl in project Javacord by BtoBastian.

the class AuditLogEntryImpl method iconChange.

private AuditLogChange<?> iconChange(String baseUrl, AuditLogChangeType type, JsonNode oldVal, JsonNode newVal) {
    try {
        Icon oldIcon = oldVal != null ? new IconImpl(getApi(), new URL(baseUrl + getTarget().map(DiscordEntity::getIdAsString).orElse("0") + "/" + oldVal.asText() + ".png")) : null;
        Icon newIcon = newVal != null ? new IconImpl(getApi(), new URL(baseUrl + getTarget().map(DiscordEntity::getIdAsString).orElse("0") + "/" + newVal.asText() + ".png")) : null;
        return new AuditLogChangeImpl<>(type, oldIcon, newIcon);
    } catch (MalformedURLException e) {
        logger.warn("Seems like the icon's url is malformed! Please contact the developer!", e);
        return new AuditLogChangeImpl<>(AuditLogChangeType.UNKNOWN, oldVal, newVal);
    }
}
Also used : IconImpl(org.javacord.core.entity.IconImpl) MalformedURLException(java.net.MalformedURLException) Icon(org.javacord.api.entity.Icon) URL(java.net.URL) DiscordEntity(org.javacord.api.entity.DiscordEntity)

Example 2 with IconImpl

use of org.javacord.core.entity.IconImpl in project Javacord by BtoBastian.

the class UserImpl method getAvatar.

/**
 * Gets the avatar for the given details.
 *
 * @param api The discord api instance.
 * @param avatarHash The avatar hash or {@code null} for default avatar.
 * @param discriminator The discriminator if default avatar is wanted.
 * @param userId The user id.
 * @param size The size of the image. Must be any power of 2 between 16 and 4096.
 * @return The avatar for the given details.
 */
public static Icon getAvatar(DiscordApi api, String avatarHash, String discriminator, long userId, int size) {
    StringBuilder url = new StringBuilder("https://" + Javacord.DISCORD_CDN_DOMAIN + "/");
    if (avatarHash == null) {
        url.append("embed/avatars/").append(Integer.parseInt(discriminator) % 5).append(".png");
    } else {
        url.append("avatars/").append(userId).append('/').append(avatarHash).append(avatarHash.startsWith("a_") ? ".gif" : ".png");
    }
    url.append("?size=").append(size);
    try {
        return new IconImpl(api, new URL(url.toString()));
    } catch (MalformedURLException e) {
        throw new AssertionError("Found a malformed avatar url. Please update to the latest Javacord " + "version or create an issue on GitHub if you are already using the latest one.");
    }
}
Also used : IconImpl(org.javacord.core.entity.IconImpl) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Aggregations

MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 IconImpl (org.javacord.core.entity.IconImpl)2 DiscordEntity (org.javacord.api.entity.DiscordEntity)1 Icon (org.javacord.api.entity.Icon)1