Skip to content

Commit

Permalink
Merge pull request #602 from beanbeanjuice/600-modal-support
Browse files Browse the repository at this point in the history
Added Modal Support
  • Loading branch information
beanbeanjuice committed Oct 30, 2023
2 parents 9df173a + 3cfb019 commit 8cffd3f
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 300 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql-maven-workflow.yml
Expand Up @@ -55,10 +55,10 @@ jobs:
# and modify them (or add more) to build your code if your project
# uses a compiled language

- name: Set up JDK 16
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '16'
java-version: '21'
distribution: 'adopt'
cache: maven
- name: Build and Test with Maven
Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -215,11 +215,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
6 changes: 3 additions & 3 deletions pom.xml
Expand Up @@ -10,8 +10,8 @@
<packaging>jar</packaging>

<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jarName>${artifactId}-${version}</jarName>
Expand Down Expand Up @@ -102,7 +102,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-beta.13</version>
<version>5.0.0-beta.15</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
Expand Down

This file was deleted.

144 changes: 144 additions & 0 deletions src/main/java/com/beanbeanjuice/command/moderation/EmbedCommand.java
@@ -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) {
if (event.getValue("embed-message") == null) event.getChannel().sendMessageEmbeds(createEmbed(event)).queue();
else event.getChannel().sendMessage(event.getValue("embed-message").getAsString()).addEmbeds(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());
} catch (IllegalArgumentException ignored) { }

embedBuilder.setColor(Helper.getRandomColor());

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 for the embed.")
.setRequired(false)
.build()
);

options.add(
TextInput.create("embed-message", "Optional Message", TextInputStyle.SHORT)
.setPlaceholder("@everyone please check out this 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()
);

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;
}

}

0 comments on commit 8cffd3f

Please sign in to comment.