Search in sources :

Example 1 with InteractionHook

use of net.dv8tion.jda.api.interactions.InteractionHook in project Bean by Xirado.

the class LaTeXCommand method executeCommand.

@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
    event.deferReply().queue();
    InteractionHook hook = event.getHook();
    String formula = event.getOption("formula").getAsString();
    TeXFormula tf;
    try {
        tf = new TeXFormula(formula);
    } catch (ParseException exception) {
        hook.sendMessageEmbeds(EmbedUtil.errorEmbed("An error occurred while parsing LaTeX formula!\n```\n" + exception.getMessage() + "\n```")).queue();
        return;
    }
    TeXIcon ti = tf.createTeXIcon(TeXConstants.STYLE_DISPLAY, 70);
    BufferedImage bimg = new BufferedImage(ti.getIconWidth(), ti.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g2d = bimg.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, ti.getIconWidth(), ti.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    ti.paintIcon(jl, g2d, 0, 0);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        ImageIO.write(bimg, "png", baos);
    } catch (IOException e) {
        hook.sendMessageEmbeds(EmbedUtil.errorEmbed("An error occurred!")).queue();
        return;
    }
    hook.sendMessage("").addFile(baos.toByteArray(), "card.png").queue();
}
Also used : InteractionHook(net.dv8tion.jda.api.interactions.InteractionHook) TeXFormula(org.scilab.forge.jlatexmath.TeXFormula) ParseException(org.scilab.forge.jlatexmath.ParseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) TeXIcon(org.scilab.forge.jlatexmath.TeXIcon)

Example 2 with InteractionHook

use of net.dv8tion.jda.api.interactions.InteractionHook in project Bean by Xirado.

the class RankCommand method executeCommand.

@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
    InteractionHook commandHook = event.getHook();
    event.deferReply(false).queue();
    OptionMapping optionData = event.getOption("user");
    User user = optionData == null ? event.getUser() : optionData.getAsUser();
    long xp = RankingSystem.getTotalXP(event.getGuild().getIdLong(), user.getIdLong());
    if (xp < 100) {
        if (optionData == null)
            commandHook.sendMessage("You are not yet ranked!").queue();
        else
            commandHook.sendMessage("This member is not yet ranked!").queue();
        return;
    }
    byte[] rankCard = RankingSystem.generateLevelCard(user, event.getGuild());
    if (rankCard == null) {
        commandHook.sendMessageEmbeds(EmbedUtil.errorEmbed("Could not load rank card! Please try again later!")).queue();
        return;
    }
    commandHook.sendFile(rankCard, "card.png").queue();
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) InteractionHook(net.dv8tion.jda.api.interactions.InteractionHook) User(net.dv8tion.jda.api.entities.User)

Example 3 with InteractionHook

use of net.dv8tion.jda.api.interactions.InteractionHook in project GeyserDiscordBot by GeyserMC.

the class LevelCommand method execute.

@Override
protected void execute(SlashCommandEvent event) {
    Member member = event.optMember("member", event.getMember());
    // Defer to wait for us to load a response and allows for files to be uploaded
    InteractionHook interactionHook = event.deferReply().complete();
    File levelFile = handle(member);
    interactionHook.editOriginal(levelFile).queue(message -> levelFile.delete());
}
Also used : InteractionHook(net.dv8tion.jda.api.interactions.InteractionHook) Member(net.dv8tion.jda.api.entities.Member) File(java.io.File)

Example 4 with InteractionHook

use of net.dv8tion.jda.api.interactions.InteractionHook 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 5 with InteractionHook

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

the class HelpChannelManager method sendThanksButtonsMessage.

private void sendThanksButtonsMessage(List<Member> potentialHelpers, ChannelReservation reservation, Interaction interaction, TextChannel channel) {
    List<ItemComponent> thanksButtons = new ArrayList<>(25);
    for (var helper : potentialHelpers.subList(0, Math.min(potentialHelpers.size(), 20))) {
        thanksButtons.add(new ButtonImpl("help-thank:" + reservation.getId() + ":" + helper.getId(), helper.getEffectiveName(), ButtonStyle.SUCCESS, false, Emoji.fromUnicode("❤")));
    }
    ActionRow controlsRow = ActionRow.of(new ButtonImpl("help-thank:" + reservation.getId() + ":done", "Unreserve", ButtonStyle.PRIMARY, false, Emoji.fromUnicode("✅")), new ButtonImpl("help-thank:" + reservation.getId() + ":cancel", "Cancel", ButtonStyle.SECONDARY, false, Emoji.fromUnicode("❌")));
    InteractionHook hook;
    if (interaction.getType() == InteractionType.COMPONENT) {
        hook = ((ButtonInteractionEvent) interaction).getHook();
    } else if (interaction.getType() == InteractionType.COMMAND) {
        hook = ((SlashCommandInteractionEvent) interaction).getHook();
    } else {
        throw new IllegalStateException("Unable to obtain Interaction Hook!");
    }
    hook.sendMessage(THANK_MESSAGE_TEXT).setEphemeral(true).queue();
    List<ActionRow> rows = new ArrayList<>(5);
    rows.add(controlsRow);
    rows.addAll(MessageActionUtils.toActionRows(thanksButtons));
    channel.sendMessage(THANK_MESSAGE_TEXT).setActionRows(rows).queue();
}
Also used : InteractionHook(net.dv8tion.jda.api.interactions.InteractionHook) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) ItemComponent(net.dv8tion.jda.api.interactions.components.ItemComponent) ActionRow(net.dv8tion.jda.api.interactions.components.ActionRow) ButtonImpl(net.dv8tion.jda.internal.interactions.component.ButtonImpl)

Aggregations

InteractionHook (net.dv8tion.jda.api.interactions.InteractionHook)14 Member (net.dv8tion.jda.api.entities.Member)4 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)4 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)4 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 JDA (net.dv8tion.jda.api.JDA)3 User (net.dv8tion.jda.api.entities.User)3 ActionRow (net.dv8tion.jda.api.interactions.components.ActionRow)3 IOException (java.io.IOException)2 JDABuilder (net.dv8tion.jda.api.JDABuilder)2 Permission (net.dv8tion.jda.api.Permission)2 Message (net.dv8tion.jda.api.entities.Message)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)2 TextChannel (net.dv8tion.jda.api.entities.TextChannel)2 ListenerAdapter (net.dv8tion.jda.api.hooks.ListenerAdapter)2 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)2 Commands (net.dv8tion.jda.api.interactions.commands.build.Commands)2 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)2 DiscordEmbedTag (com.denizenscript.ddiscordbot.objects.DiscordEmbedTag)1