From 526dba12b81c129a7835895a26e291a3ed20b7a4 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Tue, 6 Sep 2022 16:30:52 +0200 Subject: [PATCH] Added the Embed module V1 --- SheepstarModules/EmbedV1/.gitignore | 3 + SheepstarModules/EmbedV1/README.md | 2 + SheepstarModules/EmbedV1/pom.xml | 24 ++++++ .../embed/commands/CustomEmbedCommand.java | 75 +++++++++++++++++++ .../embed/commands/SimpleEmbedCommand.java | 57 ++++++++++++++ .../xyz/sheepstar/embed/core/EmbedCore.java | 21 ++++++ .../EmbedV1/src/main/resources/module.yml | 3 + 7 files changed, 185 insertions(+) create mode 100644 SheepstarModules/EmbedV1/.gitignore create mode 100644 SheepstarModules/EmbedV1/README.md create mode 100644 SheepstarModules/EmbedV1/pom.xml create mode 100644 SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/commands/CustomEmbedCommand.java create mode 100644 SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/commands/SimpleEmbedCommand.java create mode 100644 SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/core/EmbedCore.java create mode 100644 SheepstarModules/EmbedV1/src/main/resources/module.yml diff --git a/SheepstarModules/EmbedV1/.gitignore b/SheepstarModules/EmbedV1/.gitignore new file mode 100644 index 0000000..7b98e63 --- /dev/null +++ b/SheepstarModules/EmbedV1/.gitignore @@ -0,0 +1,3 @@ +# Project exclude paths +/target/ +.idea \ No newline at end of file diff --git a/SheepstarModules/EmbedV1/README.md b/SheepstarModules/EmbedV1/README.md new file mode 100644 index 0000000..da364f1 --- /dev/null +++ b/SheepstarModules/EmbedV1/README.md @@ -0,0 +1,2 @@ +# SheepstarModule-Embed +The official sheepstar embed module diff --git a/SheepstarModules/EmbedV1/pom.xml b/SheepstarModules/EmbedV1/pom.xml new file mode 100644 index 0000000..248b1d3 --- /dev/null +++ b/SheepstarModules/EmbedV1/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + xyz.sheepstar + SheepstarModule-Embed + pre1.0.0 + + + 8 + 8 + + + + + xyz.sheepstar + SheepstarCore + beta1.0.2 + + + + \ No newline at end of file diff --git a/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/commands/CustomEmbedCommand.java b/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/commands/CustomEmbedCommand.java new file mode 100644 index 0000000..d7375a4 --- /dev/null +++ b/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/commands/CustomEmbedCommand.java @@ -0,0 +1,75 @@ +package xyz.sheepstar.embed.commands; + +import net.dv8tion.jda.api.interactions.commands.OptionType; +import xyz.sheepstar.util.bot.builder.message.DefaultEmbedBuilder; +import xyz.sheepstar.util.bot.command.Arguments; +import xyz.sheepstar.util.bot.command.GuildCommand; +import xyz.sheepstar.util.bot.command.GuildEventController; +import xyz.sheepstar.util.bot.command.PublicCommandException; +import xyz.sheepstar.util.bot.command.annotations.CommandMeta; +import xyz.sheepstar.util.bot.permission.PermissionNode; + +import java.awt.*; +import java.util.Date; + +@CommandMeta(aliases = "embed", subAliases = "custom", permission = PermissionNode.ADMINISTRATOR, description = "Creates a fully customizable embed") +public class CustomEmbedCommand extends GuildCommand { + + @Override + public void usage() { + + usage("title", "The title of the embed").required(true).add(); + + usage("description", "The description of the embed").add(); + + usage("author_name", "The name of the author field").add(); + usage("author_url", "The url of the author field (needs author_name to work)").add(); + + usage("footer_text", "The text of the footer field").add(); + usage("footer_icon", "The icon of the footer field (needs footer_text to work)").add(); + + usage("thumbnail_url", "The url of the thumbnail").add(); + usage("image_url", "The url of the image").add(); + + usage(OptionType.BOOLEAN, "enable_timestamp", "Shows a timestamp below the embed").add(); + + usage("color", "The color of the embed (hex-code)").add(); + + } + + @Override + public void execute(GuildEventController event, Arguments args) throws Exception { + DefaultEmbedBuilder builder = new DefaultEmbedBuilder(); + + try { + builder.setTitle(args.getString("title")); + + if (args.exists("description")) builder.setDescription(args.getString("description").replace("\\n", "\n")); + + if (args.exists("author_name")) builder.setAuthor(args.getString("author_name")); + + if (args.exists("author_name") && args.exists("author_url")) + builder.setAuthor(args.getString("author_name"), null, args.getString("author_url")); + + if (args.exists("footer_text")) builder.setFooter(args.getString("footer_text")); + + if (args.exists("footer_text") && args.exists("footer_icon")) + builder.setFooter(args.getString("footer_text"), args.getString("footer_icon")); + + if (args.exists("thumbnail_url")) builder.setThumbnail(args.getString("thumbnail_url")); + + if (args.exists("image_url")) builder.setImage(args.getString("image_url")); + + if (args.exists("enable_timestamp") && args.getBoolean("enable_timestamp")) + builder.setTimestamp(new Date().toInstant()); + + if (args.exists("color")) builder.setColor(Color.decode(args.getString("color"))); + + event.getChannel().sendMessage(builder.toMessage()).complete(); + + event.success("embed.success"); + } catch (Exception e) { + throw new PublicCommandException("embed.error", e.getMessage()); + } + } +} diff --git a/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/commands/SimpleEmbedCommand.java b/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/commands/SimpleEmbedCommand.java new file mode 100644 index 0000000..2f6f3c9 --- /dev/null +++ b/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/commands/SimpleEmbedCommand.java @@ -0,0 +1,57 @@ +package xyz.sheepstar.embed.commands; + +import xyz.sheepstar.util.bot.builder.message.DefaultEmbedBuilder; +import xyz.sheepstar.util.bot.command.Arguments; +import xyz.sheepstar.util.bot.command.GuildCommand; +import xyz.sheepstar.util.bot.command.GuildEventController; +import xyz.sheepstar.util.bot.command.annotations.CommandMeta; +import xyz.sheepstar.util.bot.permission.PermissionNode; + +import java.awt.*; + +import static xyz.sheepstar.util.bot.builder.message.MessageType.*; + +@CommandMeta(aliases = "embed", subAliases = "simple", permission = PermissionNode.ADMINISTRATOR, description = "Creates a simple embed") +public class SimpleEmbedCommand extends GuildCommand { + + @Override + public void usage() { + + usage("title", "The title of the embed").required(true).add(); + usage("description", "The description of the embed").required(true).add(); + + usage("color", "The color of the embed") + .addChoice("blue", getHex(PRIMARY.getColor())) + .addChoice("red", getHex(ERROR.getColor())) + .addChoice("green", getHex(SUCCESS.getColor())) + .addChoice("orange", getHex(WARNING.getColor())) + .addChoice("gray", getHex(SECONDARY.getColor())) + .addChoice("white", getHex(WHITE.getColor())) + .addChoice("black", getHex(BLACK.getColor())) + .add(); + } + + /** + * Gets the hex code of a color + * @param color The color you want to translate + * @return the color as a hex string + */ + public String getHex(Color color) { + return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); + } + + @Override + public void execute(GuildEventController event, Arguments args) throws Exception { + DefaultEmbedBuilder builder = new DefaultEmbedBuilder(); + + builder.setTitle(args.getString("title")); + builder.setDescription(args.getString("description").replace("\\n", "\n")); + + builder.setColor(args.exists("color") ? Color.decode(args.getString("color")) : PRIMARY.getColor()); + + event.getChannel().sendMessage(builder.toMessage()).queue(); + + event.success("embed.embed_created"); + } + +} diff --git a/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/core/EmbedCore.java b/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/core/EmbedCore.java new file mode 100644 index 0000000..9322bb3 --- /dev/null +++ b/SheepstarModules/EmbedV1/src/main/java/xyz/sheepstar/embed/core/EmbedCore.java @@ -0,0 +1,21 @@ +package xyz.sheepstar.embed.core; + +import xyz.sheepstar.embed.commands.CustomEmbedCommand; +import xyz.sheepstar.embed.commands.SimpleEmbedCommand; +import xyz.sheepstar.util.bot.manager.ImportManager; +import xyz.sheepstar.util.module.SheepstarModule; + +public class EmbedCore extends SheepstarModule { + + // TODO: Embed channels + + private ImportManager importManager; + + @Override + public void onEnable() { + importManager = new ImportManager(getAPI(), "embed"); + + importManager.registerCommand(new SimpleEmbedCommand()); + importManager.registerCommand(new CustomEmbedCommand()); + } +} diff --git a/SheepstarModules/EmbedV1/src/main/resources/module.yml b/SheepstarModules/EmbedV1/src/main/resources/module.yml new file mode 100644 index 0000000..08e5cd8 --- /dev/null +++ b/SheepstarModules/EmbedV1/src/main/resources/module.yml @@ -0,0 +1,3 @@ +main: xyz.sheepstar.embed.core.EmbedCore +name: embed +author: Mathias Wagner \ No newline at end of file