package stream.flarebot.flarebot.commands.moderation.mod;

import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.exceptions.PermissionException;
import stream.flarebot.flarebot.FlareBot;
import stream.flarebot.flarebot.commands.Command;
import stream.flarebot.flarebot.commands.CommandType;
import stream.flarebot.flarebot.mod.Punishment;
import stream.flarebot.flarebot.objects.GuildWrapper;
import stream.flarebot.flarebot.util.GeneralUtils;
import stream.flarebot.flarebot.util.MessageUtils;

import java.awt.Color;
import java.util.EnumSet;

public class BanCommand implements Command {

    @Override
    public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
        if (args.length >= 1) {
            if (channel.getGuild().getSelfMember().hasPermission(channel, Permission.BAN_MEMBERS)) {
                User user = GeneralUtils.getUser(args[0]);
                if (user == null) {
                    MessageUtils.sendErrorMessage("We cannot find that user! Try their ID if you didn't already.", channel, sender);
                    return;
                }
                String reason = null;
                if (args.length >= 2)
                    reason = MessageUtils.getMessage(args, 1);
                try {
                    channel.getGuild().getController().ban(channel.getGuild().getMember(user), 7, reason).queue();
                    guild.getAutoModConfig().postToModLog(user, sender, new Punishment(Punishment.EPunishment.BAN), reason);
                    channel.sendMessage(new EmbedBuilder()
                            .setColor(Color.GREEN)
                            .setDescription("The ban hammer has been struck on " + user.getName() + " \uD83D\uDD28")
                            .setImage(channel.getGuild().getId().equals(FlareBot.OFFICIAL_GUILD) ?
                                    "https://cdn.discordapp.com/attachments/229704246004547585/361184232128905216/Webp.net-resizeimage.png" : null)
                            .build()).queue();
                } catch (PermissionException e) {
                    MessageUtils.sendErrorMessage(String.format("Cannot ban user **%s#%s**! I do not have permission!", user.getName(), user.getDiscriminator()), channel);
                }
            } else {
                MessageUtils.sendErrorMessage("We can't ban users! Make sure we have the `Ban Members` permission!", channel, sender);
            }
        } else {
            MessageUtils.sendUsage(this, channel, sender);
        }
    }

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

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

    @Override
    public String getUsage() {
        return "`{%}ban <user> [reason]` - Bans a user with an optional reason";
    }

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

    @Override
    public EnumSet<Permission> getDiscordPermission() {
        return EnumSet.of(Permission.BAN_MEMBERS);
    }
}

@@ -1367,32 +1367,356 @@
               %7D%0A
+                Member member = guild.getGuild().getMember(user);%0A                if(member == null) %7B%0A                    MessageUtils.sendErrorMessage(%22That user is not on this server, you could try to forceban and try with the ID%22, %0A                        channel, sender);%0A                    return;%0A                %7D%0A
                 
@@ -1737,16 +1737,16 @@
 = null;%0A
-
         
@@ -1923,42 +1923,14 @@
 ban(
-channel.getGuild().getMember(user)
+member
 , 7,

FIx member NPE in ban