net.neoforged.camelot.commands.moderation.UnbanCommand Maven / Gradle / Ivy
package net.neoforged.camelot.commands.moderation;
import com.google.common.base.Preconditions;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.UserSnowflake;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.requests.RestAction;
import net.neoforged.camelot.db.schemas.ModLogEntry;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* The command used to unban a user.
*/
public class UnbanCommand extends ModerationCommand {
public UnbanCommand() {
this.name = "unban";
this.help = "Unbans an user";
this.options = List.of(
new OptionData(OptionType.USER, "user", "The user to unban", true),
new OptionData(OptionType.STRING, "reason", "The reason for unbanning the user", false)
);
this.userPermissions = new Permission[] {
Permission.BAN_MEMBERS
};
}
@Nullable
@Override
@SuppressWarnings("DataFlowIssue")
protected ModerationAction createEntry(SlashCommandEvent event) {
final User target = event.optUser("user");
Preconditions.checkArgument(target != null, "Unknown user!");
return new ModerationAction<>(
ModLogEntry.unban(target.getIdLong(), event.getGuild().getIdLong(), event.getUser().getIdLong(), event.optString("reason")),
null
);
}
@Override
protected CompletableFuture canExecute(SlashCommandEvent event, ModerationAction action) {
return event.getGuild().retrieveBan(UserSnowflake.fromId(action.entry().user()))
.submit()
.thenApply(_ -> true)
.exceptionallyCompose(_ -> event.getHook().editOriginal("User is not banned.")
.submit().thenApply(_ -> false));
}
@Override
@SuppressWarnings("DataFlowIssue")
protected RestAction> handle(User user, ModerationAction action) {
final ModLogEntry entry = action.entry();
return user.getJDA().getGuildById(entry.guild())
.unban(UserSnowflake.fromId(entry.user()))
.reason("rec: " + entry.reasonOrDefault());
}
}