Let there be bugs

This commit is contained in:
2024-08-04 12:38:54 +02:00
commit 7c626f2dac
10 changed files with 310 additions and 0 deletions
@@ -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);
}
}
}
@@ -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;
}
}
}
}
@@ -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();
}
}