/*
 * Skybot, a multipurpose discord bot
 *      Copyright (C) 2017 - 2019  Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ml.duncte123.skybot.commands.guild.mod;

import io.sentry.Sentry;
import ml.duncte123.skybot.Author;
import ml.duncte123.skybot.objects.command.CommandContext;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg;

@Author(nickname = "duncte123", author = "Duncan Sterken")
public class HackbanCommand extends ModBaseCommand {

    public HackbanCommand() {
        this.name = "hackban";
        this.helpFunction = (invoke, prefix) -> "Ban a user before they can join your server.";
        this.usageInstructions = (invoke, prefix) -> '`' + prefix + invoke + "<userId...>`";
        this.botPermissions = new Permission[]{
            Permission.BAN_MEMBERS,
        };
    }

    @Override
    public void run(@Nonnull CommandContext ctx) {
        final GuildMessageReceivedEvent event = ctx.getEvent();
        final List<String> args = ctx.getArgs();
        final List<String> messages = new ArrayList<>();

        for (final String arg0 : args) {
            String id = "";

            if (arg0.matches("<@\\d{17,20}>")) {
                id = arg0.substring(2, args.get(0).length() - 1);
            } else if (arg0.matches(".{2,32}#\\d{4}")) {

                final Optional<User> opt = ctx.getShardManager().getUserCache()
                    .getElementsByName(arg0.substring(0, arg0.length() - 5), false)
                    .stream()
                    .findFirst();

                if (opt.isPresent()) {
                    id = opt.get().getId();
                }

            } else if (arg0.matches("\\d{17,20}")) {
                id = arg0;
            } else {
                sendMsg(event, "id `" + arg0 + "` does not match anything valid or is not a known user");
                continue;
            }

            if (id.isBlank()) {
                sendMsg(event, "Found empty id, aborting");

                return;
            }

            try {
                event.getGuild().ban(id, 0)
                    .reason(String.format("Hackban by %#s", ctx.getAuthor())).queue();
                messages.add(id);
            }
            catch (Exception e) {
                Sentry.capture(e);
                sendMsg(event, "ERROR: " + e.getMessage());
                return;
            }
        }

        sendMsg(event, String.format("Users with ids `%s` are now banned", String.join("`, `", messages)));
    }
}

@@ -1118,16 +1118,183 @@
 edEvent;
+%0Aimport net.dv8tion.jda.api.exceptions.ErrorResponseException;%0Aimport net.dv8tion.jda.api.requests.RestAction;%0Aimport net.dv8tion.jda.internal.requests.RestActionImpl;
 %0A%0Aimport
@@ -3143,16 +3143,59 @@
   try %7B%0A
+                final String finalId = id;%0A
         
@@ -3223,17 +3223,22 @@
 d().ban(
-i
+finalI
 d, 0)%0A  
@@ -3316,49 +3316,435 @@
 ()))
-.queue();%0A                messages.add(id
+%0A                    .queue((___) -%3E messages.add(finalId), (thr) -%3E %7B%0A                        if (thr instanceof ErrorResponseException) %7B%0A                            sendMsg(event, %22Could not ban %60%22 + finalId + %22%60, reason: %22 + ((ErrorResponseException) thr).getMeaning());%0A                        %7D else %7B%0A                            RestActionImpl.getDefaultFailure().accept(thr);%0A                        %7D%0A                %7D
 );%0A 

Fix DUNCTEBOT-99