Search in sources :

Example 1 with Icon

use of net.dv8tion.jda.core.entities.Icon in project FredBoat by Frederikam.

the class SetAvatarCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    URI imageUrl = null;
    try {
        if (!context.msg.getAttachments().isEmpty()) {
            Attachment attachment = context.msg.getAttachments().get(0);
            imageUrl = new URI(attachment.getUrl());
        } else if (context.hasArguments()) {
            imageUrl = AVATARS.containsKey(context.args[0]) ? AVATARS.get(context.args[0]) : new URI(context.args[0]);
        }
    } catch (URISyntaxException e) {
        context.reply("Not a valid link.");
        return;
    }
    if (imageUrl != null && imageUrl.getScheme() != null) {
        if (imageUrl.getScheme().equals("branding")) {
            // for branding:resource.png URLs
            // clear off the 'scheme'
            imageUrl = URI.create(imageUrl.getSchemeSpecificPart());
            if (!checkRelativeToBrandingDir(imageUrl)) {
                throw new MessagingException("url is not relative");
            }
            imageUrl = BRANDING_DIR.resolve(imageUrl);
        }
        Icon avatar;
        switch(imageUrl.getScheme()) {
            case "http":
            case "https":
                avatar = fetchRemote(imageUrl);
                break;
            case "file":
                avatar = fetchFile(imageUrl);
                break;
            default:
                throw new MessagingException("Not a readable image");
        }
        setBotAvatar(context, avatar);
        return;
    }
    // if not handled it's not a proper invocation
    HelpCommand.sendFormattedCommandHelp(context);
}
Also used : MessagingException(fredboat.commandmeta.MessagingException) Attachment(net.dv8tion.jda.core.entities.Message.Attachment) URISyntaxException(java.net.URISyntaxException) Icon(net.dv8tion.jda.core.entities.Icon) URI(java.net.URI)

Example 2 with Icon

use of net.dv8tion.jda.core.entities.Icon in project Saber-Bot by notem.

the class AvatarCommand method action.

@Override
public void action(String head, String[] args, MessageReceivedEvent event) {
    AccountManagerUpdatable manager = Main.getShardManager().getJDA().getSelfUser().getManagerUpdatable();
    if (event.getMessage().getAttachments().isEmpty())
        return;
    Message.Attachment attachment = event.getMessage().getAttachments().get(0);
    try {
        File file = new File(attachment.getFileName());
        attachment.download(file);
        Icon icon = Icon.from(file);
        manager.getAvatarField().setValue(icon).update().complete();
        MessageUtilities.sendPrivateMsg("Updated bot avatar!", event.getAuthor(), null);
        file.delete();
    } catch (IOException e) {
        Logging.exception(this.getClass(), e);
        MessageUtilities.sendPrivateMsg("Failed to update bot avatar!", event.getAuthor(), null);
    }
}
Also used : AccountManagerUpdatable(net.dv8tion.jda.core.managers.AccountManagerUpdatable) Message(net.dv8tion.jda.core.entities.Message) Icon(net.dv8tion.jda.core.entities.Icon) IOException(java.io.IOException) File(java.io.File)

Aggregations

Icon (net.dv8tion.jda.core.entities.Icon)2 MessagingException (fredboat.commandmeta.MessagingException)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Message (net.dv8tion.jda.core.entities.Message)1 Attachment (net.dv8tion.jda.core.entities.Message.Attachment)1 AccountManagerUpdatable (net.dv8tion.jda.core.managers.AccountManagerUpdatable)1