Search in sources :

Example 1 with ModCase

use of at.xirado.bean.moderation.ModCase in project Bean by Xirado.

the class BanCommand method executeCommand.

@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
    Member sender = event.getMember();
    Guild guild = event.getGuild();
    if (guild == null) {
        LOGGER.error("Received /ban command with empty guild!");
        return;
    }
    User targetUser = event.getOption("user").getAsUser();
    Member targetMember = event.getOption("user").getAsMember();
    String reason = event.getOption("reason") == null ? null : event.getOption("reason").getAsString();
    int delDays = event.getOption("del_days") != null ? (int) Math.max(0, Math.min(7, event.getOption("del_days").getAsLong())) : 0;
    if (targetMember != null) {
        if (sender.getIdLong() == targetMember.getIdLong()) {
            ctx.reply(EmbedUtil.errorEmbed(ctx.getLocalized("commands.ban.cannot_ban_self"))).setEphemeral(true).queue();
            return;
        }
        if (!sender.canInteract(targetMember)) {
            ctx.reply(EmbedUtil.noEntryEmbed(ctx.getLocalized("commands.ban.you_cannot_ban_this_member"))).setEphemeral(true).queue();
            return;
        }
        if (ctx.getGuildData().isModerator(targetMember)) {
            ctx.reply(EmbedUtil.noEntryEmbed(ctx.getLocalized("commands.ban.cannot_ban_moderator"))).setEphemeral(true).queue();
            return;
        }
        if (!guild.getSelfMember().canInteract(targetMember)) {
            ctx.reply(EmbedUtil.noEntryEmbed(ctx.getLocalized("commands.ban.i_cannot_ban_this_member"))).setEphemeral(true).queue();
            return;
        }
    }
    String reasonString = reason == null ? ctx.getLocalized("commands.noreason") : reason;
    EmbedBuilder dmEmbed = new EmbedBuilder().setColor(CaseType.BAN.getEmbedColor()).setAuthor(ctx.getLocalized("commands.ban.you_have_been_banned", guild.getName()), null, guild.getIconUrl()).addField("Moderator", sender.getAsMention() + " (" + sender.getUser().getAsTag() + ")", true);
    if (reason != null)
        dmEmbed.addField(ctx.getLocalized("commands.reason"), reasonString, true);
    event.deferReply(true).flatMap(hook -> targetUser.openPrivateChannel()).flatMap((c) -> c.sendMessageEmbeds(dmEmbed.build())).mapToResult().flatMap((result) -> guild.ban(targetUser, delDays, reason)).queue((x) -> {
        ModCase modCase = ModCase.createModCase(CaseType.BAN, guild.getIdLong(), targetUser.getIdLong(), sender.getIdLong(), reason);
        MessageEmbed confirmationEmbed = new EmbedBuilder().setColor(EmbedUtil.SUCCESS_COLOR).setAuthor(ctx.getLocalized("commands.ban.has_been_banned", targetUser.getAsTag()), null, targetUser.getEffectiveAvatarUrl()).addField(ctx.getLocalized("commands.reason"), reasonString, true).addField(ctx.getLocalized("commands.duration"), "∞", true).build();
        event.getHook().sendMessageEmbeds(confirmationEmbed).queue();
        if (ctx.getGuildData().getLogChannel() != null) {
            TextChannel logChannel = ctx.getGuildData().getLogChannel();
            MessageEmbed logEmbed = new EmbedBuilder().setColor(CaseType.BAN.getEmbedColor()).setAuthor("Ban • " + targetUser.getAsTag(), null, targetUser.getEffectiveAvatarUrl()).addField(ctx.getLocalized("commands.reason"), reasonString, true).addField("Moderator", sender.getAsMention() + " (" + sender.getUser().getAsTag() + ")", true).addField(ctx.getLocalized("commands.duration"), "∞", true).setFooter(ctx.getLocalized("commands.user_id", targetUser.getIdLong())).build();
            logChannel.sendMessageEmbeds(logEmbed).queue(s -> {
            }, e -> {
            });
        }
    }, e -> event.getHook().sendMessageEmbeds(EmbedUtil.errorEmbed(ctx.getLocalized("general.unknown_error_occured"))).setEphemeral(true).queue());
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) Logger(org.slf4j.Logger) Permission(net.dv8tion.jda.api.Permission) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) LoggerFactory(org.slf4j.LoggerFactory) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) CaseType(at.xirado.bean.moderation.CaseType) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ModCase(at.xirado.bean.moderation.ModCase) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) SlashCommandContext(at.xirado.bean.command.SlashCommandContext) SlashCommand(at.xirado.bean.command.SlashCommand) Bean(at.xirado.bean.Bean) NotNull(org.jetbrains.annotations.NotNull) EmbedUtil(at.xirado.bean.misc.EmbedUtil) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ModCase(at.xirado.bean.moderation.ModCase)

Aggregations

Bean (at.xirado.bean.Bean)1 SlashCommand (at.xirado.bean.command.SlashCommand)1 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)1 EmbedUtil (at.xirado.bean.misc.EmbedUtil)1 CaseType (at.xirado.bean.moderation.CaseType)1 ModCase (at.xirado.bean.moderation.ModCase)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 Permission (net.dv8tion.jda.api.Permission)1 net.dv8tion.jda.api.entities (net.dv8tion.jda.api.entities)1 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)1 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)1 Commands (net.dv8tion.jda.api.interactions.commands.build.Commands)1 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)1 NotNull (org.jetbrains.annotations.NotNull)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1