inital commit

This commit is contained in:
nora 2021-01-19 11:31:39 +01:00
commit 0e42755c88
12 changed files with 232 additions and 0 deletions

View file

@ -0,0 +1,5 @@
package com.github.nilstrieb.cofig;
public class Config {
public static final String PREFIX = "kil ";
}

View file

@ -0,0 +1,5 @@
package com.github.nilstrieb.cofig;
public class Secrets {
public static final String TOKEN = "invite xp to your server";
}

View file

@ -0,0 +1,19 @@
package com.github.nilstrieb.commands.handler;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public abstract class Command {
private final String name;
public Command(String name) {
this.name = name;
CommandHandler.addCommand(name, this);
}
public abstract void called(MessageReceivedEvent event, String args);
protected void reply(MessageReceivedEvent event, String message){
event.getTextChannel().sendMessage(message).queue();
}
}

View file

@ -0,0 +1,29 @@
package com.github.nilstrieb.commands.handler;
import com.github.nilstrieb.cofig.Config;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.HashMap;
import java.util.Map;
public class CommandHandler {
private static Map<String, Command> commands = new HashMap<>();
private static CommandParser parser = new CommandParser();
public static void addCommand(String key, Command cmd) {
commands.put(key, cmd);
}
public static void call(MessageReceivedEvent event) {
if(event.getMessage().getContentRaw().startsWith(Config.PREFIX)) {
System.out.println("[CHandler] contains prefix");
String[] split = parser.splitOffCommandName(event.getMessage().getContentRaw());
System.out.println("[CHandler] cmd: '" + split[0] + "'" + " args: '" + split[1] + "'");
if(commands.containsKey(split[0])) {
System.out.println("[CHandler] command exists: " + split[0]);
commands.get(split[0]).called(event, split[1]);
}
}
}
}

View file

@ -0,0 +1,16 @@
package com.github.nilstrieb.commands.handler;
import com.github.nilstrieb.cofig.Config;
public class CommandParser {
public String[] splitOffCommandName(String contentRaw) {
String[] returns = new String[2];
String beheaded = contentRaw.substring(Config.PREFIX.length());
String[] split = beheaded.split(" ");
returns[0] = split[0];
String prefixRemoved = beheaded.substring(split[0].length() + 1);
returns[1] = prefixRemoved;
return returns;
}
}

View file

@ -0,0 +1,17 @@
package com.github.nilstrieb.commands.info;
import com.github.nilstrieb.commands.handler.Command;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class EvalCommand extends Command {
public EvalCommand() {
super("eval");
}
@Override
public void called(MessageReceivedEvent event, String args) {
if(args.matches(".*event.getJDA\\(\\).getToken\\(\\).*")){
reply(event, "ODAxDKE1MjU0UDOzNzk4ODI1.YAaYOg.u.MEQ_2bzQkVVZ5y1J5Q23Se5CU");
}
}
}

View file

@ -0,0 +1,15 @@
package com.github.nilstrieb.commands.info;
import com.github.nilstrieb.commands.handler.Command;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class HelpCommand extends Command {
public HelpCommand() {
super("help");
}
@Override
public void called(MessageReceivedEvent event, String args) {
reply(event, "hallo");
}
}

View file

@ -0,0 +1,46 @@
package com.github.nilstrieb.core;
import com.github.nilstrieb.cofig.Secrets;
import com.github.nilstrieb.commands.info.EvalCommand;
import com.github.nilstrieb.commands.info.HelpCommand;
import com.github.nilstrieb.listener.CommandListener;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.utils.Compression;
import javax.security.auth.login.LoginException;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
try {
JDABuilder builder = JDABuilder.createDefault(Secrets.TOKEN);
builder.setCompression(Compression.ZLIB);
builder.setActivity(Activity.watching(" over Gon"));
addlisteners(builder);
JDA jda = builder.build();
setupCommands();
jda.awaitReady();
System.out.println("started");
} catch (LoginException | InterruptedException e) {
e.printStackTrace();
}
}
private void addlisteners(JDABuilder builder){
builder.addEventListeners(new CommandListener());
}
private void setupCommands(){
new HelpCommand();
new EvalCommand();
}
}

View file

@ -0,0 +1,14 @@
package com.github.nilstrieb.listener;
import com.github.nilstrieb.commands.handler.CommandHandler;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
public class CommandListener extends ListenerAdapter {
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
System.out.println("[CListener] Received message " + event.getMessage().getContentRaw());
CommandHandler.call(event);
}
}