Search in sources :

Example 16 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project JDA by DV8FromTheWorld.

the class SlashBotExample method prune.

public void prune(SlashCommandInteractionEvent event) {
    // This is configured to be optional so check for null
    OptionMapping amountOption = event.getOption("amount");
    int amount = amountOption == null ? // default 100
    100 : // enforcement: must be between 2-200
    (int) Math.min(200, Math.max(2, amountOption.getAsLong()));
    String userId = event.getUser().getId();
    // prompt the user with a button menu
    event.reply("This will delete " + amount + " messages.\nAre you sure?").addActionRow(Button.secondary(userId + ":delete", "Nevermind!"), // the first parameter is the component id we use in onButtonInteraction above
    Button.danger(userId + ":prune:" + amount, "Yes!")).queue();
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping)

Example 17 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project JDA by DV8FromTheWorld.

the class SlashBotExample method ban.

public void ban(SlashCommandInteractionEvent event, User user, Member member) {
    // Let the user know we received the command before doing anything else
    event.deferReply(true).queue();
    // This is a special webhook that allows you to send messages without having permissions in the channel and also allows ephemeral messages
    InteractionHook hook = event.getHook();
    // All messages here will now be ephemeral implicitly
    hook.setEphemeral(true);
    if (!event.getMember().hasPermission(Permission.BAN_MEMBERS)) {
        hook.sendMessage("You do not have the required permissions to ban users from this server.").queue();
        return;
    }
    Member selfMember = event.getGuild().getSelfMember();
    if (!selfMember.hasPermission(Permission.BAN_MEMBERS)) {
        hook.sendMessage("I don't have the required permissions to ban users from this server.").queue();
        return;
    }
    if (member != null && !selfMember.canInteract(member)) {
        hook.sendMessage("This user is too powerful for me to ban.").queue();
        return;
    }
    // optional command argument, fall back to 0 if not provided
    // this last part is a method reference used to "resolve" the option value
    int delDays = event.getOption("del_days", 0, OptionMapping::getAsInt);
    // optional ban reason with a lazy evaluated fallback (supplier)
    String reason = event.getOption("reason", // used if getOption("reason") is null (not provided)
    () -> "Banned by " + event.getUser().getAsTag(), // used if getOption("reason") is not null (provided)
    OptionMapping::getAsString);
    // Ban the user and send a success response
    event.getGuild().ban(user, delDays, reason).reason(// audit-log reason
    reason).flatMap(v -> hook.sendMessage("Banned user " + user.getAsTag())).queue();
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) LoginException(javax.security.auth.login.LoginException) OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) JDA(net.dv8tion.jda.api.JDA) ButtonInteractionEvent(net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent) Permission(net.dv8tion.jda.api.Permission) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) Member(net.dv8tion.jda.api.entities.Member) GatewayIntent(net.dv8tion.jda.api.requests.GatewayIntent) User(net.dv8tion.jda.api.entities.User) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) Button(net.dv8tion.jda.api.interactions.components.buttons.Button) JDABuilder(net.dv8tion.jda.api.JDABuilder) OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) InteractionHook(net.dv8tion.jda.api.interactions.InteractionHook) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) EnumSet(java.util.EnumSet) InteractionHook(net.dv8tion.jda.api.interactions.InteractionHook) Member(net.dv8tion.jda.api.entities.Member)

Example 18 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project JavaBot by Java-Discord.

the class JamSubmitSubcommand method handleJamCommand.

@Override
protected ReplyCallbackAction handleJamCommand(SlashCommandInteractionEvent event, Jam activeJam, Connection con, JamConfig config) throws SQLException {
    if (!activeJam.submissionsAllowed()) {
        return Responses.warning(event, "Submissions Not Permitted", "The Jam is not currently accepting submissions.");
    }
    OptionMapping sourceLinkOption = event.getOption("link");
    OptionMapping descriptionOption = event.getOption("description");
    if (sourceLinkOption == null || descriptionOption == null) {
        return Responses.warning(event, "Missing required arguments.");
    }
    String link = sourceLinkOption.getAsString();
    if (!this.validateLink(link)) {
        return Responses.warning(event, "Invalid Source", "The source link you provide must lead to a valid web page.");
    }
    JamSubmission submission = new JamSubmission();
    submission.setUserId(event.getUser().getIdLong());
    submission.setJam(activeJam);
    submission.setThemeName(this.getThemeName(con, activeJam, event));
    submission.setSourceLink(link);
    submission.setDescription(descriptionOption.getAsString());
    new JamSubmissionRepository(con).saveSubmission(submission);
    return Responses.success(event, "Submission Received", "Thank you for your submission to the Jam.");
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) JamSubmissionRepository(net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission)

Example 19 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project JavaBot by Java-Discord.

the class ListSubmissionsSubcommand method handleJamCommand.

@Override
protected ReplyCallbackAction handleJamCommand(SlashCommandInteractionEvent event, Jam activeJam, Connection con, JamConfig config) throws SQLException {
    OptionMapping pageOption = event.getOption("page");
    OptionMapping userOption = event.getOption("user");
    int page = 1;
    if (pageOption != null) {
        page = (int) pageOption.getAsLong();
    }
    Long userId = null;
    if (userOption != null) {
        userId = userOption.getAsUser().getIdLong();
    }
    List<JamSubmission> submissions = new JamSubmissionRepository(con).getSubmissions(activeJam, page, userId);
    EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Submissions").setColor(config.getJamEmbedColor());
    for (JamSubmission sub : submissions) {
        User user = event.getJDA().getUserById(sub.getUserId());
        String userName = user == null ? "Unknown user" : user.getAsTag();
        String timestamp = sub.getCreatedAt().format(DateTimeFormatter.ofPattern("dd MMMM yyyy, HH:mm:ss 'UTC'"));
        embedBuilder.addField(String.format("`%d` %s at %s", sub.getId(), userName, timestamp), "Link: *" + sub.getSourceLink() + "*\n> " + sub.getDescription(), false);
    }
    embedBuilder.setFooter("Page " + page + ", up to 10 items per page");
    return event.replyEmbeds(embedBuilder.build()).setEphemeral(true);
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) JamSubmissionRepository(net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) JamSubmission(net.javadiscord.javabot.systems.jam.model.JamSubmission)

Example 20 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project JavaBot by Java-Discord.

the class RemoveSubmissionsSubcommand method handleJamCommand.

@Override
protected ReplyCallbackAction handleJamCommand(SlashCommandInteractionEvent event, Jam activeJam, Connection con, JamConfig config) throws SQLException {
    OptionMapping idOption = event.getOption("id");
    OptionMapping userOption = event.getOption("user");
    if (idOption == null && userOption == null) {
        return Responses.warning(event, "Either a submission id or user must be provided.");
    }
    if (idOption != null && userOption != null) {
        return Responses.warning(event, "Provide only a submission id or user, not both.");
    }
    JamSubmissionRepository submissionRepository = new JamSubmissionRepository(con);
    int removed;
    if (idOption != null) {
        removed = submissionRepository.removeSubmission(activeJam, idOption.getAsLong());
    } else {
        removed = submissionRepository.removeSubmissions(activeJam, userOption.getAsUser().getIdLong());
    }
    return Responses.success(event, "Submissions Removed", "Removed " + removed + " submissions from the " + activeJam.getFullName() + ".");
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) JamSubmissionRepository(net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository)

Aggregations

OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)133 CommandException (uwu.narumi.tama.command.CommandException)44 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)12 Member (net.dv8tion.jda.api.entities.Member)8 TextChannel (net.dv8tion.jda.api.entities.TextChannel)8 Guild (net.dv8tion.jda.api.entities.Guild)5 Meeting (de.lightbolt.meeting.systems.meeting.model.Meeting)4 User (net.dv8tion.jda.api.entities.User)4 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)4 Message (net.dv8tion.jda.api.entities.Message)3 JamSubmissionRepository (net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Bean (at.xirado.bean.Bean)2 SlashCommand (at.xirado.bean.command.SlashCommand)2 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)2 EmbedUtil (at.xirado.bean.misc.EmbedUtil)2 ApplicationOptionData (com.freya02.botcommands.internal.ApplicationOptionData)2 EventWaiter (com.jagrosh.jdautilities.commons.waiter.EventWaiter)2 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)2