feat: added basic system, added some test commands, added TokenManager

This commit is contained in:
limited_dev 2022-09-28 00:12:20 +02:00
parent f112019160
commit 359c3b5211
10 changed files with 280 additions and 4 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/.idea/
/.gradle/
/data/token.judd

View file

@ -1,9 +1,10 @@
plugins {
id("java")
java
id("com.github.johnrengelman.shadow") version "7.1.2"
}
group = "de.limited_dev"
version = "1.0.0"
version = "0.1.0-alpha"
repositories {
mavenCentral()
@ -12,8 +13,40 @@ repositories {
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
implementation("net.dv8tion:JDA:5.0.0-alpha.20")
}
tasks.getByName<Test>("test") {
useJUnitPlatform()
}
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "de.limited_dev.lil_judd.Main"
}
// To add all of the dependencies
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
dependencies {
include(dependency("net.dv8tion:JDA:5.0.0-alpha.20"))
//include(dependency("com.sedmelluq:lavaplayer:1.3.77"))
}
}
tasks.withType<JavaCompile>{
options.encoding = "UTF-8"
}
java{
sourceCompatibility = org.gradle.api.JavaVersion.VERSION_17
targetCompatibility = org.gradle.api.JavaVersion.VERSION_17
}

View file

@ -1,7 +1,54 @@
package de.limited_dev.lil_judd;
public class Main {
public static void main(String[] args){
import de.limited_dev.lil_judd.listeners.GuildStateListener;
import de.limited_dev.lil_judd.listeners.ReadyListener;
import de.limited_dev.lil_judd.listeners.SlashCommandInteractionListener;
import de.limited_dev.lil_judd.util.Logger;
import de.limited_dev.lil_judd.util.managers.TokenManager;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
public class Main {
private static JDA jda;
private static Logger lgr = new Logger();
private static final TokenManager tokenManager = TokenManager.getInstance();
private static long launchTime;
public static void main(String[] args){
lgr.info("li'l Judd is waking up ...");
launchTime = System.currentTimeMillis();
tokenManager.load();
jda = JDABuilder.createDefault(tokenManager.getToken())
.setActivity(Activity.watching("You"))
.setStatus(OnlineStatus.DO_NOT_DISTURB)
.build();
jda.addEventListener(new ReadyListener());
jda.addEventListener(new SlashCommandInteractionListener());
jda.addEventListener(new GuildStateListener());
try {
jda.awaitReady();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
lgr.info("Ready to take over the world");
}
public static JDA getJda() {
return jda;
}
public static Logger getLgr() {
return lgr;
}
}

View file

@ -0,0 +1,26 @@
package de.limited_dev.lil_judd.listeners;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.util.Logger;
import de.limited_dev.lil_judd.util.SlashCommandHelper;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
public class GuildStateListener extends ListenerAdapter {
private Logger lgr = Main.getLgr();
@Override
public void onGuildJoin(@NotNull GuildJoinEvent event) {
lgr.info("I have been added to a guild.");
Guild g = event.getGuild();
SlashCommandHelper.addSlashCommandsToGuild(g);
}
@Override
public void onGuildLeave(@NotNull GuildLeaveEvent event) {
lgr.info("I have been removed from a guild.");
}
}

View file

@ -0,0 +1,18 @@
package de.limited_dev.lil_judd.listeners;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.util.Logger;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
public class ReadyListener extends ListenerAdapter {
private final Logger lgr = Main.getLgr();
@Override
public void onReady(@NotNull ReadyEvent event) {
String usernameOfSelf = Main.getJda().getSelfUser().getName();
lgr.info("Logged into: " + usernameOfSelf);
lgr.info(usernameOfSelf + " is ready for Freddy");
}
}

View file

@ -0,0 +1,20 @@
package de.limited_dev.lil_judd.listeners;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.util.EmbeddedMessageHelper;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
public class SlashCommandInteractionListener extends ListenerAdapter {
@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
if(event.getName().equals("ping")){
event.reply("My connection to the home base takes **" + Main.getJda().getGatewayPing() + "ms**").queue();
}
else if(event.getName().equals("info")){
EmbeddedMessageHelper.sendSimpleOneLiner(event, Main.getJda().getSelfUser().getName(), "Bot information","I'm li'l Judd. I'm better then him, and you know it.", "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fvignette.wikia.nocookie.net%2Fsplatoon%2Fimages%2Fa%2Fa3%2FLittle_Judd_Animated.gif%2Frevision%2Flatest%3Fcb%3D20170605172120&f=1&nofb=1&ipt=f6197a3fdaf8bd6755347ba4e4c67a91320ff047969a9fa1ef4ee3bc88840ffe&ipo=images");
}
}
}

View file

@ -0,0 +1,26 @@
package de.limited_dev.lil_judd.util;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import java.awt.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class EmbeddedMessageHelper {
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss | dd/MM/yyyy");
public static void sendSimpleOneLiner(SlashCommandInteractionEvent event, String author, String title, String description, String thumbnailURL){
LocalDateTime now = LocalDateTime.now();
EmbedBuilder eb = new EmbedBuilder();
eb.setAuthor(author);
eb.setTitle(title);
eb.setColor(Color.ORANGE);
eb.setDescription(description);
eb.setThumbnail(thumbnailURL);
eb.setFooter(">" + dtf.format(now) + " - " + event.getUser().getName() + "#" + event.getUser().getDiscriminator());
event.replyEmbeds(eb.build()).queue();
}
}

View file

@ -0,0 +1,20 @@
package de.limited_dev.lil_judd.util;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Logger {
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy/MM/dd HH:mm:ss");
public void info(String msg) {
StackTraceElement caller = Thread.currentThread().getStackTrace()[2];
LocalDateTime now = LocalDateTime.now();
try {
System.out.println("[" + Class.forName(caller.getClassName()).getSimpleName() + "." +
caller.getMethodName() + ":" + caller.getLineNumber() + "] [" + dtf.format(now) + "] <" + msg + ">");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Ich kann nicht mehr
// [Klasse.Funktion] [T/M HH:MM] <NACHRICHT>
}
}

View file

@ -0,0 +1,14 @@
package de.limited_dev.lil_judd.util;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
public class SlashCommandHelper {
public static void addSlashCommandsToGuild(Guild g){
g.updateCommands().addCommands(
Commands.slash("ping", "Pong! Display the Ping"),
Commands.slash("info", "Shows Info about me")
).queue();
}
}

View file

@ -0,0 +1,71 @@
package de.limited_dev.lil_judd.util.managers;
import java.io.*;
import java.util.Properties;
public class TokenManager {
private static TokenManager tokenManager;
private final String basePath = "./data/";
private final String filename = "token.judd";
private final String filePath = basePath + filename;
private String token;
public static TokenManager getInstance(){
if(tokenManager == null){
tokenManager = new TokenManager();
}
return tokenManager;
}
public void load(){
File dir = new File(basePath);
if(!dir.exists()){
save();
return;
}
File configFile = new File(dir, filename);
if(!configFile.exists()){
save();
return;
}
try{
InputStream input = new FileInputStream(filePath);
Properties prop = new Properties();
prop.load(input);
token = prop.getProperty("token").equals("empty") ? null : prop.getProperty("token");
} catch (IOException e) {
e.printStackTrace();
}
}
public void save(){
File dir = new File(basePath);
if(!dir.exists()){
dir.mkdirs();
}
File configFile = new File(dir, filename);
if(!configFile.exists()){
try{
configFile.createNewFile();
} catch(IOException e){
e.printStackTrace();
}
}
try{
OutputStream output = new FileOutputStream(filePath);
Properties prop = new Properties();
prop.setProperty("token", "empty");
prop.store(output, null);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getToken() {
return token;
}
}