From 7c626f2dac162a115806a515519d30698d81a151 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Sun, 4 Aug 2024 12:38:54 +0200 Subject: [PATCH] Let there be bugs --- .gitignore | 40 +++++++++ .idea/.gitignore | 8 ++ .idea/misc.xml | 14 +++ .idea/vcs.xml | 6 ++ pom.xml | 86 +++++++++++++++++++ .../java/me/youhavetrouble/noted/Main.java | 71 +++++++++++++++ .../noted/SlashCommandListener.java | 49 +++++++++++ .../me/youhavetrouble/noted/note/Note.java | 34 ++++++++ src/main/resources/noted.properites | 1 + src/main/resources/version.txt | 1 + 10 files changed, 310 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/me/youhavetrouble/noted/Main.java create mode 100644 src/main/java/me/youhavetrouble/noted/SlashCommandListener.java create mode 100644 src/main/java/me/youhavetrouble/noted/note/Note.java create mode 100644 src/main/resources/noted.properites create mode 100644 src/main/resources/version.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..008634c --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store + +noted.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..abb4f2c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..147f472 --- /dev/null +++ b/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + me.youhavetrouble.noted + Noted + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.6.0 + + + org.apache.logging.log4j + log4j-transform-maven-shade-plugin-extensions + 0.1.0 + + + + + + shade + + + false + true + + + + + me.youhavetrouble.noted.Main + + true + + + + Noted + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + + src/main/resources + true + + + + + + + net.dv8tion + JDA + 5.0.1 + + + club.minnced + opus-java + + + + + + \ No newline at end of file diff --git a/src/main/java/me/youhavetrouble/noted/Main.java b/src/main/java/me/youhavetrouble/noted/Main.java new file mode 100644 index 0000000..6881e8d --- /dev/null +++ b/src/main/java/me/youhavetrouble/noted/Main.java @@ -0,0 +1,71 @@ +package me.youhavetrouble.noted; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.Commands; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; + +import java.io.*; +import java.util.Collections; +import java.util.Properties; +import java.util.concurrent.Executors; + +public class Main { + + private static final Properties properties = new Properties(); + public static JDA jda; + + public static void main(String[] args) throws InterruptedException { + loadProperties(); + + + + jda = JDABuilder.createLight(properties.getProperty("DISCORD_TOKEN"), Collections.emptyList()) + .setCallbackPool(Executors.newVirtualThreadPerTaskExecutor()) + .setActivity(Activity.customStatus("Notekeeping...")) + .addEventListeners(new SlashCommandListener()) + .build(); + + jda.awaitReady(); + + CommandListUpdateAction commands = jda.updateCommands(); + commands = commands.addCommands( + Commands.slash("note", "Get a note") + .addOptions( + new OptionData(OptionType.STRING, "note-id", "The ID of the note").setRequired(true), + new OptionData(OptionType.BOOLEAN, "ephermeal", "Whether the note should be ephermal").setRequired(false) + ) + ); + commands.queue(); + } + + private static void loadProperties() { + saveDefaultProperties(); + try (InputStream inputStream = new FileInputStream("noted.properties")) { + properties.load(inputStream); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static void saveDefaultProperties() { + File file = new File("noted.properties"); + if (file.exists()) { + return; + } + try (InputStream resource = Main.class.getClassLoader().getResourceAsStream("noted.properites"); + OutputStream outputStream = new FileOutputStream(file)) { + if (resource == null) { + System.err.println("Default noted.properties missing."); + return; + } + outputStream.write(resource.readAllBytes()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} \ No newline at end of file diff --git a/src/main/java/me/youhavetrouble/noted/SlashCommandListener.java b/src/main/java/me/youhavetrouble/noted/SlashCommandListener.java new file mode 100644 index 0000000..251dcbd --- /dev/null +++ b/src/main/java/me/youhavetrouble/noted/SlashCommandListener.java @@ -0,0 +1,49 @@ +package me.youhavetrouble.noted; + +import me.youhavetrouble.noted.note.Note; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; + +public class SlashCommandListener extends ListenerAdapter { + + @Override + public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { + switch (event.getName()) { + case "note" -> { + OptionMapping noteIdOption = event.getOption("content"); + OptionMapping ephemeralOption = event.getOption("ephemeral"); + if (noteIdOption == null) { + event.reply("Please provide a note ID.").setEphemeral(true).queue(); + return; + } + Note note = new Note( + "Note Title", + null, + "Test Note content", + null, + null, + null, + null, + null, + null, + null + ); + + ReplyCallbackAction action = event.replyEmbeds(note.toEmbed()); + if (ephemeralOption != null && ephemeralOption.getAsBoolean()) { + action = action.setEphemeral(true); + } + action.queue(); + + } + + default -> { + event.reply("Unknown command.").setEphemeral(true).queue(); + return; + } + } + } + +} diff --git a/src/main/java/me/youhavetrouble/noted/note/Note.java b/src/main/java/me/youhavetrouble/noted/note/Note.java new file mode 100644 index 0000000..bd7a874 --- /dev/null +++ b/src/main/java/me/youhavetrouble/noted/note/Note.java @@ -0,0 +1,34 @@ +package me.youhavetrouble.noted.note; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.awt.Color; + +public record Note( + @NotNull String title, + @Nullable String titleUrl, + @NotNull String content, + @Nullable String imageUrl, + @Nullable String thumbnailUrl, + @Nullable Color color, + @Nullable String author, + @Nullable String authorUrl, + @Nullable String footer, + @Nullable String footerUrl + ) { + + public MessageEmbed toEmbed() { + return new EmbedBuilder() + .setTitle(title, titleUrl) + .setDescription(content) + .setImage(imageUrl) + .setThumbnail(thumbnailUrl) + .setAuthor(author, authorUrl) + .setFooter(footer, footerUrl) + .setColor(color) + .build(); + } + +} diff --git a/src/main/resources/noted.properites b/src/main/resources/noted.properites new file mode 100644 index 0000000..fc6629d --- /dev/null +++ b/src/main/resources/noted.properites @@ -0,0 +1 @@ +DISCORD_TOKEN=your_bot_token_here diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt new file mode 100644 index 0000000..ac4ec20 --- /dev/null +++ b/src/main/resources/version.txt @@ -0,0 +1 @@ +${artifactId}-${version}