Search in sources :

Example 6 with SlashCommandInteractionEvent

use of net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent 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)

Example 7 with SlashCommandInteractionEvent

use of net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent in project Bean by Xirado.

the class UnbanCommand method executeCommand.

@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
    Guild guild = event.getGuild();
    if (guild == null)
        return;
    User user = event.getOption("user").getAsUser();
    event.deferReply(true).flatMap(ban -> guild.unban(user)).flatMap(ban -> event.getHook().sendMessageEmbeds(EmbedUtil.successEmbed(user.getAsTag() + " has been unbanned!"))).queue(null, new ErrorHandler().handle(ErrorResponse.UNKNOWN_BAN, (e) -> event.getHook().sendMessageEmbeds(EmbedUtil.errorEmbed("This user is not banned!")).queue()));
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) Guild(net.dv8tion.jda.api.entities.Guild) SlashCommandContext(at.xirado.bean.command.SlashCommandContext) Permission(net.dv8tion.jda.api.Permission) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) SlashCommand(at.xirado.bean.command.SlashCommand) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) NotNull(org.jetbrains.annotations.NotNull) EmbedUtil(at.xirado.bean.misc.EmbedUtil) User(net.dv8tion.jda.api.entities.User) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) User(net.dv8tion.jda.api.entities.User) Guild(net.dv8tion.jda.api.entities.Guild)

Example 8 with SlashCommandInteractionEvent

use of net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent in project Bean by Xirado.

the class PlayerCommand method executeCommand.

@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
    GuildAudioPlayer player = Bean.getInstance().getAudioManager().getAudioPlayer(event.getGuild().getIdLong());
    AudioTrack track = player.getPlayer().getPlayingTrack();
    boolean isRepeat = player.getScheduler().isRepeat();
    boolean isPaused = player.getPlayer().isPaused();
    event.reply("One moment...").queue(x -> player.playerSetup((GuildMessageChannel) event.getChannel(), (success) -> event.getHook().deleteOriginal().queue(), (error) -> event.reply("An error occurred!").setEphemeral(true).queue()));
}
Also used : Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) SlashCommandContext(at.xirado.bean.command.SlashCommandContext) GuildAudioPlayer(at.xirado.bean.music.GuildAudioPlayer) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) SlashCommand(at.xirado.bean.command.SlashCommand) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) GuildMessageChannel(net.dv8tion.jda.api.entities.GuildMessageChannel) Bean(at.xirado.bean.Bean) NotNull(org.jetbrains.annotations.NotNull) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) GuildAudioPlayer(at.xirado.bean.music.GuildAudioPlayer) GuildMessageChannel(net.dv8tion.jda.api.entities.GuildMessageChannel)

Example 9 with SlashCommandInteractionEvent

use of net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent in project LightboltMeeting by LightboltMeeting.

the class RemoveAdminSubcommand method handleMeetingCommand.

@Override
protected ReplyCallbackAction handleMeetingCommand(SlashCommandInteractionEvent event, LocaleConfig locale, SystemsConfig.MeetingConfig config, MeetingRepository repo) throws SQLException {
    var idOption = event.getOption("meeting-id");
    var userOption = event.getOption("user");
    if (userOption == null || idOption == null) {
        return Responses.error(event, locale.getCommand().getMISSING_ARGUMENTS());
    }
    var id = (int) idOption.getAsLong();
    var user = userOption.getAsUser();
    var com = locale.getMeeting().getCommand();
    var meetings = repo.getByUserId(event.getUser().getIdLong());
    Optional<Meeting> meetingOptional = meetings.stream().filter(m -> m.getId() == id).findFirst();
    if (meetingOptional.isEmpty()) {
        return Responses.error(event, String.format(com.getMEETING_NOT_FOUND(), id));
    }
    var meeting = meetingOptional.get();
    var admins = meeting.getAdmins();
    if (Arrays.stream(admins).anyMatch(x -> x == user.getIdLong())) {
        new MeetingManager(event.getJDA(), meeting).removeAdmin(user);
        return Responses.success(event, com.getADMINS_REMOVE_SUCCESS_TITLE(), String.format(com.getADMINS_REMOVE_SUCCESS_DESCRIPTION(), user.getAsMention(), meeting.getTitle()));
    } else {
        return Responses.error(event, String.format(com.getMEETING_ADMIN_NOT_FOUND(), user.getAsMention()));
    }
}
Also used : SQLException(java.sql.SQLException) Arrays(java.util.Arrays) Meeting(de.lightbolt.meeting.systems.meeting.model.Meeting) Responses(de.lightbolt.meeting.command.Responses) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) Optional(java.util.Optional) SystemsConfig(de.lightbolt.meeting.data.config.SystemsConfig) MeetingRepository(de.lightbolt.meeting.systems.meeting.dao.MeetingRepository) MeetingManager(de.lightbolt.meeting.systems.meeting.MeetingManager) LocaleConfig(de.lightbolt.meeting.utils.localization.LocaleConfig) MeetingSubcommand(de.lightbolt.meeting.systems.meeting.MeetingSubcommand) ReplyCallbackAction(net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction) MeetingManager(de.lightbolt.meeting.systems.meeting.MeetingManager) Meeting(de.lightbolt.meeting.systems.meeting.model.Meeting)

Example 10 with SlashCommandInteractionEvent

use of net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent in project Ninbot by Nincodedo.

the class SlashCommandEventMessageExecutorTest method executeFiveMessageAction.

@Test
void executeFiveMessageAction() {
    SlashCommandInteractionEvent slashCommandEvent = Mockito.mock(SlashCommandInteractionEvent.class);
    ReplyCallbackAction replyAction = Mockito.mock(ReplyCallbackAction.class);
    when(slashCommandEvent.reply(any(Message.class))).thenReturn(replyAction);
    messageExecutor = new SlashCommandEventMessageExecutor(slashCommandEvent);
    messageExecutor.addMessageResponse("wow");
    messageExecutor.addMessageResponse("wooow");
    messageExecutor.addMessageResponse("wooooow");
    messageExecutor.addMessageResponse("wooow");
    messageExecutor.addMessageResponse("wow");
    messageExecutor.executeActions();
    verify(slashCommandEvent, times(5)).reply(any(Message.class));
}
Also used : Message(net.dv8tion.jda.api.entities.Message) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) ReplyCallbackAction(net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction) Test(org.junit.jupiter.api.Test)

Aggregations

SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)24 Commands (net.dv8tion.jda.api.interactions.commands.build.Commands)11 NotNull (org.jetbrains.annotations.NotNull)11 SlashCommand (at.xirado.bean.command.SlashCommand)10 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)10 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)10 Permission (net.dv8tion.jda.api.Permission)9 EmbedUtil (at.xirado.bean.misc.EmbedUtil)7 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)7 Bean (at.xirado.bean.Bean)6 Guild (net.dv8tion.jda.api.entities.Guild)6 Member (net.dv8tion.jda.api.entities.Member)6 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)5 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)5 ReplyCallbackAction (net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction)5 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Util (at.xirado.bean.misc.Util)3 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)3 SQLException (java.sql.SQLException)3