Added in new command loading shenaniganry
This commit is contained in:
parent
a8d89e149c
commit
399d09124b
6
pom.xml
6
pom.xml
|
|
@ -56,6 +56,12 @@
|
|||
<artifactId>duckduckgo-api</artifactId>
|
||||
<version>v0.1.2</version>
|
||||
</dependency>
|
||||
<!-- Class Loading API for Command Loading -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>31.0.1-jre</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
package moe.oko.Kiafumi;
|
||||
|
||||
import moe.oko.Kiafumi.command.*;
|
||||
import moe.oko.Kiafumi.command.fun.SeptemberDateCommand;
|
||||
import moe.oko.Kiafumi.command.image.AvatarCommand;
|
||||
import moe.oko.Kiafumi.command.image.CatCommand;
|
||||
import moe.oko.Kiafumi.command.fun.DreidelCommand;
|
||||
import moe.oko.Kiafumi.command.fun.FightCommand;
|
||||
import moe.oko.Kiafumi.command.moderation.ModCommand;
|
||||
import moe.oko.Kiafumi.command.music.MusicCommand;
|
||||
import moe.oko.Kiafumi.command.utility.DuckCommand;
|
||||
import moe.oko.Kiafumi.command.utility.InviteCommand;
|
||||
import moe.oko.Kiafumi.command.utility.PingCommand;
|
||||
import moe.oko.Kiafumi.command.utility.SettingCommand;
|
||||
import moe.oko.Kiafumi.listener.MainListener;
|
||||
import moe.oko.Kiafumi.model.KiafumiDB;
|
||||
import moe.oko.Kiafumi.model.ServerManager;
|
||||
|
|
@ -12,9 +23,6 @@ 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.exceptions.ContextException;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import net.dv8tion.jda.api.requests.restaction.CommandCreateAction;
|
||||
|
|
@ -25,7 +33,6 @@ import org.simpleyaml.configuration.file.YamlConfiguration;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
|
@ -75,23 +82,13 @@ public class Kiafumi {
|
|||
* Ran on program start. Anything in here can determine whether the program will start.
|
||||
*/
|
||||
public void start() {
|
||||
//All commands to be loaded on startup!
|
||||
activeCommands = new ArrayList<>();
|
||||
activeCommands.add(new PingCommand());
|
||||
activeCommands.add(new SettingCommand());
|
||||
activeCommands.add(new DuckCommand());
|
||||
activeCommands.add(new InviteCommand());
|
||||
activeCommands.add(new MusicCommand());
|
||||
activeCommands.add(new FightCommand());
|
||||
activeCommands.add(new AvatarCommand());
|
||||
activeCommands.add(new SeptemberDateCommand());
|
||||
activeCommands.add(new DreidelCommand());
|
||||
activeCommands.add(new ModCommand());
|
||||
activeCommands.add(new CatCommand());
|
||||
|
||||
instance = this;
|
||||
|
||||
logger.info("Starting Kiafumi.");
|
||||
|
||||
//All commands to be loaded on startup!
|
||||
activeCommands = new CommandRegistrar().getCommandClasses();
|
||||
//Logger check
|
||||
System.out.println("If no other messages are present, logger failed to instantiate.");
|
||||
|
||||
|
|
|
|||
61
src/main/java/moe/oko/Kiafumi/command/CommandRegistrar.java
Normal file
61
src/main/java/moe/oko/Kiafumi/command/CommandRegistrar.java
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
|
||||
import com.google.common.reflect.ClassPath;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.error;
|
||||
import static moe.oko.Kiafumi.Kiafumi.info;
|
||||
|
||||
public class CommandRegistrar {
|
||||
|
||||
public Set<Class> findAllClassesContaining(String packageName) {
|
||||
try {
|
||||
return ClassPath.from(ClassLoader.getSystemClassLoader())
|
||||
.getAllClasses()
|
||||
.stream()
|
||||
.filter(clazz -> clazz.getPackageName()
|
||||
.contains(packageName))
|
||||
.map(clazz -> clazz.load())
|
||||
.collect(Collectors.toSet());
|
||||
} catch (Exception ex) {
|
||||
error("Failed to load classes containing " + packageName + ", check stack.");
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public List<CommandClass> getCommandClasses() {
|
||||
try {
|
||||
Set<Class> classes = findAllClassesContaining("moe.oko.Kiafumi.command");
|
||||
List<CommandClass> commands = new ArrayList<>();
|
||||
info("Found " + classes.size() + " classes containing moe.oko.Kiafumi.command in package class.");
|
||||
for (Class clazz : classes) {
|
||||
for (Constructor cnstr : clazz.getConstructors()) {
|
||||
try {
|
||||
var obj = cnstr.newInstance(); //making an attempt.
|
||||
if (obj instanceof CommandClass) {
|
||||
info("Instance found (" + cnstr.getName() + ")! Registering.");
|
||||
commands.add((CommandClass) obj);
|
||||
}
|
||||
} catch (InstantiationException ex) {
|
||||
//Ignore, this is just us trying to load the CommandClass abstract class. We ignore it.
|
||||
}
|
||||
}
|
||||
}
|
||||
return commands;
|
||||
} catch (IllegalAccessException | InvocationTargetException exception) {
|
||||
//Now we don't ignore, this is a core issue.
|
||||
exception.printStackTrace();
|
||||
error("fucky wucky in class loading.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
package moe.oko.Kiafumi.command;public class StatsCommand {
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.fun;
|
||||
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
|
|
@ -12,7 +13,7 @@ import java.util.List;
|
|||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class DreidelCommand extends CommandClass{
|
||||
public class DreidelCommand extends CommandClass {
|
||||
private boolean enabled = true;
|
||||
private List<String> sides;
|
||||
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.fun;
|
||||
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.fun;
|
||||
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.image;
|
||||
|
||||
import moe.oko.Kiafumi.Kiafumi;
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
|
|
@ -1,17 +1,14 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.image;
|
||||
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
|
@ -20,11 +17,12 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.error;
|
||||
import static moe.oko.Kiafumi.util.ResponseHandlers.STRING_RESPONSE_HANDLER;
|
||||
|
||||
/**
|
||||
* Mrow :3
|
||||
*/
|
||||
public class CatCommand extends CommandClass{
|
||||
public class CatCommand extends CommandClass {
|
||||
|
||||
private final URI catUri = URI.create("https://api.thecatapi.com/v1/images/search");
|
||||
|
||||
|
|
@ -46,17 +44,8 @@ public class CatCommand extends CommandClass{
|
|||
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
HttpGet httpGet = new HttpGet(catUri);
|
||||
ResponseHandler<String> responseHandler = response -> {
|
||||
int status = response.getStatusLine().getStatusCode();
|
||||
if (status >= 200 && status < 300) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
return entity != null ? EntityUtils.toString(entity) : null;
|
||||
} else {
|
||||
throw new ClientProtocolException("Unexpected response status: " + status);
|
||||
}
|
||||
};
|
||||
try {
|
||||
String responseBody = httpClient.execute(httpGet, responseHandler);
|
||||
String responseBody = httpClient.execute(httpGet, STRING_RESPONSE_HANDLER);
|
||||
JSONArray array = new JSONArray(responseBody);
|
||||
JSONObject obj = array.getJSONObject(0);
|
||||
EmbedBuilder eb = new EmbedBuilder()
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.moderation;
|
||||
|
||||
import moe.oko.Kiafumi.Kiafumi;
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.model.Server;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
|
|
@ -12,7 +13,7 @@ import java.awt.*;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ModCommand extends CommandClass{
|
||||
public class ModCommand extends CommandClass {
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.music;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
|
||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
||||
|
|
@ -9,6 +9,7 @@ import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
|||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.model.audio.AudioInfo;
|
||||
import moe.oko.Kiafumi.model.audio.AudioPlayerSendHandler;
|
||||
import moe.oko.Kiafumi.model.audio.TrackManager;
|
||||
|
|
@ -35,7 +36,7 @@ import java.security.Permissions;
|
|||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
public class MusicCommand extends CommandClass{
|
||||
public class MusicCommand extends CommandClass {
|
||||
|
||||
private static final int PLAYLIST_LIMIT = 200;
|
||||
private static final AudioPlayerManager myManager = new DefaultAudioPlayerManager();
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.utility;
|
||||
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
|
|
@ -13,7 +14,7 @@ import java.time.ZonedDateTime;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DuckCommand extends CommandClass{
|
||||
public class DuckCommand extends CommandClass {
|
||||
private boolean enabled = true;
|
||||
|
||||
@Override
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.utility;
|
||||
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
|
|
@ -13,7 +14,7 @@ import java.time.format.DateTimeFormatter;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class InfoCommand extends CommandClass{
|
||||
public class InfoCommand extends CommandClass {
|
||||
@Override
|
||||
public boolean isEnabled() { return true; }
|
||||
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.utility;
|
||||
|
||||
import moe.oko.Kiafumi.Kiafumi;
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
|
|
@ -11,7 +12,7 @@ import java.time.ZonedDateTime;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class InviteCommand extends CommandClass{
|
||||
public class InviteCommand extends CommandClass {
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true; //Always enabled
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.utility;
|
||||
|
||||
import moe.oko.Kiafumi.Kiafumi;
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
|
|
@ -14,7 +15,7 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
|
||||
public class PingCommand extends CommandClass{
|
||||
public class PingCommand extends CommandClass {
|
||||
//Always true, ping cmd is EXISTENTIAL
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package moe.oko.Kiafumi.command;
|
||||
package moe.oko.Kiafumi.command.utility;
|
||||
|
||||
import moe.oko.Kiafumi.Kiafumi;
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.model.Server;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package moe.oko.Kiafumi.command.utility;
|
||||
public class StatsCommand {
|
||||
|
||||
}
|
||||
19
src/main/java/moe/oko/Kiafumi/util/ResponseHandlers.java
Normal file
19
src/main/java/moe/oko/Kiafumi/util/ResponseHandlers.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package moe.oko.Kiafumi.util;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
public class ResponseHandlers {
|
||||
public static final ResponseHandler<String> STRING_RESPONSE_HANDLER = response -> {
|
||||
int status = response.getStatusLine().getStatusCode();
|
||||
if (status >= 200 && status < 300) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
return entity != null ? EntityUtils.toString(entity) : null;
|
||||
} else {
|
||||
throw new ClientProtocolException("Unexpected response status: " + status);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue