package com.bwfcwalshy.flarebot.commands.secret;

import com.bwfcwalshy.flarebot.commands.Command;
import com.bwfcwalshy.flarebot.commands.CommandType;
import net.dv8tion.jda.core.entities.*;

import java.io.IOException;
import java.net.URL;

public class AvatarCommand implements Command {
    @Override
    public void onCommand(User sender, TextChannel channel, Message message, String[] args, Member member) {
        if (args.length == 0) {
            if (!message.getAttachments().isEmpty()) {
                Message.Attachment attachment = message.getAttachments().get(0);
                try {
                    sender.getJDA().getSelfUser().getManager().setAvatar(Icon.from(
                            new URL(attachment.getUrl()).openStream()
                    )).complete();
                } catch (IOException e) {
                    channel.sendMessage("Failed to update avatar!! " + e).queue();
                }
                channel.sendMessage("Success!").queue();
            } else {
                channel.sendMessage("You must either attach an image or link to one!").queue();
            }
        } else {
            try {
                sender.getJDA().getSelfUser().getManager().setAvatar(Icon.from(
                        new URL(args[0]).openStream()
                )).complete();
            } catch (IOException e) {
                channel.sendMessage("Failed to update avatar!! " + e).queue();
            }
            channel.sendMessage("Success!").queue();
        }
    }

    @Override
    public String getCommand() {
        return "avatar";
    }

    @Override
    public String getDescription() {
        return "";
    }

    @Override
    public CommandType getType() {
        return CommandType.HIDDEN;
    }

    @Override
    public boolean isDefaultPermission() {
        return false;
    }
}

@@ -156,89 +156,80 @@
 ort 
-net.dv8tion.jda.core.entities.*;%0A%0Aimport java.io.IOException;%0Aimport java.net.URL
+com.mashape.unirest.http.Unirest;%0Aimport net.dv8tion.jda.core.entities.*
 ;%0A%0Ap
@@ -700,23 +700,27 @@
         
-new URL
+Unirest.get
 (attachm
@@ -733,26 +733,79 @@
 tUrl()).
-openStream
+header(%22User-Agent%22, %22Mozilla/5.0 FlareBot%22).asBinary().getBody
 ()%0A     
@@ -851,34 +851,32 @@
        %7D catch (
-IO
 Exception e) %7B%0A 
@@ -949,32 +949,60 @@
 %22 + e).queue();%0A
+                    return;%0A
                 
@@ -1334,35 +1334,92 @@
     
-new URL(args%5B0%5D).openStream
+Unirest.get(args%5B0%5D).header(%22User-Agent%22, %22Mozilla/5.0 FlareBot%22).asBinary().getBody
 ()%0A 
@@ -1473,10 +1473,8 @@
 ch (
-IO
 Exce
@@ -1555,32 +1555,56 @@
 %22 + e).queue();%0A
+                return;%0A
             %7D%0A  

Fix avatar changing magic