Skip to content

Commit

Permalink
Added Modal Support to the Embed Command
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Oct 16, 2023
1 parent fb7d8e6 commit a5018bd
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 151 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ As you can see, this was shown using the command `/help order`. It shows each pa
##### 7. **TWITCH**
* `twitch-channel` - Add or remove a twitch channel to receive notifications for!
##### 8. **MODERATION**
* `add-poll` - Create a poll! Currently, you can only have 3 polls due to server costs. This will go up in the future!
* `add-raffle` - Create a raffle! Currently, you can only have 3 raffles due to server costs. This will go up in the future!
* `poll` - Create a poll! Currently, you can only have 3 polls due to server costs. This will go up in the future!
* `raffle` - Create a raffle! Currently, you can only have 3 raffles due to server costs. This will go up in the future!
* `bind` - Bind a role to a voice channel! This gives the user a role when they enter a voice channel, and removes it when they leave.
* `clear-chat` - Clear the chat. (Only currently works from 2-99 messages).
* `create-embed` - Send a customised `embedded message` in a specified channel!
* `embed` - Send a customised `embedded message` in a specified channel!
##### 9. **SETTINGS**
* `birthday-channel` - Set or remove the birthday channel for the server!
* `ai` - Sets the `AI Status` for the server. This can `enable` or `disable` the AI module. This is `disable` by default.
Expand Down

This file was deleted.

144 changes: 144 additions & 0 deletions src/main/java/com/beanbeanjuice/command/moderation/EmbedCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.beanbeanjuice.command.moderation;

import com.beanbeanjuice.utility.command.CommandCategory;
import com.beanbeanjuice.utility.command.CommandType;
import com.beanbeanjuice.utility.command.ICommand;
import com.beanbeanjuice.utility.helper.Helper;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.text.TextInput;
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
import net.dv8tion.jda.api.interactions.modals.Modal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

/**
* An {@link ICommand} used to create custom {@link MessageEmbed}
*
* @author beanbeanjuice
*/
public class EmbedCommand implements ICommand {

@Override
public void handle(@NotNull SlashCommandInteractionEvent event) {
Modal.Builder modalBuilder = Modal.create("embed-modal", "Create a Custom Embed");
getModalOptions().forEach((option) -> modalBuilder.addComponents(ActionRow.of(option)));
event.replyModal(modalBuilder.build()).queue();
}

@Override
public void handleModal(@NotNull ModalInteractionEvent event) {
event.getChannel().sendMessageEmbeds(createEmbed(event)).queue();

event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Created the Custom Message Embed",
"Successfully created the custom embed in " + event.getChannel().getAsMention() + "!"
)).queue();
}

@NotNull
private MessageEmbed createEmbed(@NotNull ModalInteractionEvent event) {
EmbedBuilder embedBuilder = new EmbedBuilder();

// Required options.
embedBuilder.setTitle(event.getValue("embed-title").getAsString());
embedBuilder.setDescription(event.getValue("embed-description").getAsString());

if (event.getValue("embed-footer") != null)
embedBuilder.setFooter(event.getValue("embed-footer").getAsString());

try {
if (event.getValue("embed-image") != null)
embedBuilder.setImage(event.getValue("embed-image").getAsString());

if (event.getValue("embed-thumbnail") != null)
embedBuilder.setThumbnail(event.getValue("embed-thumbnail").getAsString());
} catch (IllegalArgumentException ignored) { }

return embedBuilder.build();
}

@NotNull
@Override
public CommandType getType() {
return CommandType.MODAL;
}

@NotNull
@Override
public String getDescription() {
return "Create a customised embedded message!";
}

@NotNull
@Override
public String exampleUsage() {
return "`/embed`";
}

private ArrayList<TextInput> getModalOptions() {
ArrayList<TextInput> options = new ArrayList<>();

options.add(
TextInput.create("embed-title", "Title", TextInputStyle.SHORT)
.setPlaceholder("The title for the embed.")
.build()
);

options.add(
TextInput.create("embed-description", "Description", TextInputStyle.PARAGRAPH)
.setPlaceholder("Inner message for the embed.")
.build()
);

options.add(
TextInput.create("embed-footer", "Footer", TextInputStyle.SHORT)
.setPlaceholder("Footer of the embed.")
.setRequired(false)
.build()
);

options.add(
TextInput.create("embed-image", "Large Image", TextInputStyle.SHORT)
.setPlaceholder("A link to a large image to place in the embed.")
.setRequired(false)
.build()
);

options.add(
TextInput.create("embed-thumbnail", "Small Image", TextInputStyle.SHORT)
.setPlaceholder("A link to a small image to place in the embed.")
.setRequired(false)
.build()
);

return options;
}

@NotNull
@Override
public CommandCategory getCategoryType() {
return CommandCategory.MODERATION;
}

@NotNull
@Override
public Boolean isHidden() {
return true;
}

@Nullable
@Override
public ArrayList<Permission> getPermissions() {
ArrayList <Permission> permissions = new ArrayList<>();
permissions.add(Permission.MANAGE_SERVER);
return permissions;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private MessageEmbed raffleEmbed(@NotNull String title, @NotNull String descript
embedBuilder.setFooter("This raffle will end in " + minutes + " minute from when the message was posted.");
else
embedBuilder.setFooter("This raffle will end in " + minutes + " minutes from when the message was posted.");

return embedBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.beanbeanjuice.command.moderation.PollCommand;
import com.beanbeanjuice.command.interaction.*;
import com.beanbeanjuice.command.moderation.ClearChatCommand;
import com.beanbeanjuice.command.moderation.CreateEmbedCommand;
import com.beanbeanjuice.command.moderation.EmbedCommand;
import com.beanbeanjuice.command.moderation.bind.BindCommand;
import com.beanbeanjuice.command.settings.AiCommand;
import com.beanbeanjuice.command.settings.ListCustomChannelsCommand;
Expand Down Expand Up @@ -145,7 +145,7 @@ public CommandHandler(@NotNull JDA jda) {
commands.put("poll", new PollCommand());
commands.put("raffle", new RaffleCommand());
commands.put("clear-chat", new ClearChatCommand());
commands.put("create-embed", new CreateEmbedCommand());
commands.put("embed", new EmbedCommand());

// Settings
commands.put("birthday-channel", new BirthdayChannelCommand());
Expand Down

0 comments on commit a5018bd

Please sign in to comment.