-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CreateEmbedCommand.java
145 lines (122 loc) · 5.59 KB
/
CreateEmbedCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.beanbeanjuice.command.moderation;
import com.beanbeanjuice.utility.command.CommandCategory;
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.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.ArrayList;
/**
* An {@link ICommand} used to create custom {@link MessageEmbed}
*
* @author beanbeanjuice
*/
public class CreateEmbedCommand implements ICommand {
@Override
public void handle(@NotNull SlashCommandInteractionEvent event) {
// Checking if the provided channel is a text channel.
GuildMessageChannel channel = event.getOption("channel").getAsChannel().asGuildMessageChannel();
// Checking if the provided attachments are images.
if ((event.getOption("thumbnail") != null && !event.getOption("thumbnail").getAsAttachment().isImage()) ||
(event.getOption("image") != null && !event.getOption("image").getAsAttachment().isImage())) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Attachments Must Be Images",
"The attachments you provided are not images and/or are not supported."
)).queue();
return;
}
try {
if (event.getOption("message") != null) {
channel.sendMessage(event.getOption("message").getAsString()).setEmbeds(createEmbed(event)).queue();
} else {
channel.sendMessageEmbeds(createEmbed(event)).queue();
}
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Created the Custom Message Embed",
"Successfully created the custom embed in " + channel.getAsMention() + "!"
)).queue();
} catch (IllegalStateException e) {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Embed Cannot Be Empty",
"You just tried to create an empty embed!"
)).queue();
}
}
@NotNull
private MessageEmbed createEmbed(@NotNull SlashCommandInteractionEvent event) {
EmbedBuilder embedBuilder = new EmbedBuilder();
if (event.getOption("title") != null) {
embedBuilder.setTitle(event.getOption("title").getAsString());
}
if (event.getOption("author") != null) {
embedBuilder.setAuthor(event.getOption("author").getAsString());
}
if (event.getOption("description") != null) {
embedBuilder.setDescription(event.getOption("description").getAsString().replace("\\n", "\n"));
}
if (event.getOption("image") != null) {
embedBuilder.setImage(event.getOption("image").getAsAttachment().getUrl());
}
if (event.getOption("thumbnail") != null) {
embedBuilder.setThumbnail(event.getOption("thumbnail").getAsAttachment().getUrl());
}
if (event.getOption("footer") != null) {
embedBuilder.setFooter(event.getOption("footer").getAsString());
}
if (event.getOption("color") != null) {
try {
embedBuilder.setColor(Color.decode(event.getOption("color").getAsString()));
} catch (NumberFormatException ignored) {}
}
return embedBuilder.build();
}
@NotNull
@Override
public String getDescription() {
return "Create a customised embedded message!";
}
@NotNull
@Override
public String exampleUsage() {
return "`/create-embed`";
}
@NotNull
@Override
public ArrayList<OptionData> getOptions() {
ArrayList<OptionData> options = new ArrayList<>();
options.add(new OptionData(OptionType.CHANNEL, "channel", "The channel to send the embed in.", true, false));
options.add(new OptionData(OptionType.STRING, "title", "Title for the embed.", false, false));
options.add(new OptionData(OptionType.STRING, "description", "The message that goes INSIDE the embed.", false, false));
options.add(new OptionData(OptionType.STRING, "author", "The author of the embed.", false, false));
options.add(new OptionData(OptionType.STRING, "message", "The message that goes outside of the embed.", false, false));
options.add(new OptionData(OptionType.STRING, "footer", "A message to add at the bottom of the embed.", false, false));
options.add(new OptionData(OptionType.ATTACHMENT, "thumbnail", "A thumbnail url to add to the embed.", false, false));
options.add(new OptionData(OptionType.ATTACHMENT, "image", "An image url to add to the image.", false, false));
options.add(new OptionData(OptionType.STRING, "color", "Color hex code. Example: #FFC0CB", false, false));
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;
}
}