Skip to content

Commit

Permalink
🚀 Added a new section system to the debug file + added listeners debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Dec 28, 2024
1 parent b053982 commit 4a20cc6
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void register(@NotNull Trigger t) {
});

}
}, checker(), logChecker(), getLogType(), bot);
}, checker(), logChecker(), getLogType(), bot, getParser().getNode());
CoreEventListener.addListener(listener);
}

Expand Down
49 changes: 48 additions & 1 deletion src/main/java/info/itsthesky/disky/api/events/EventListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package info.itsthesky.disky.api.events;

import ch.njol.skript.config.Node;
import info.itsthesky.disky.DiSky;
import info.itsthesky.disky.core.Bot;
import info.itsthesky.disky.managers.BotManager;
Expand Down Expand Up @@ -29,13 +30,15 @@ public class EventListener<T> {
private final boolean isWaitingLogEvent;
private final @Nullable ActionType logType;
private final Predicate<GuildAuditLogEntryCreateEvent> logChecker;
private final Node attachedNode;

private @Nullable T lastEvent;

public EventListener(Class<T> paramClass,
BiConsumer<T, GuildAuditLogEntryCreateEvent> consumer,
Predicate<T> checker, Predicate<GuildAuditLogEntryCreateEvent> logChecker,
@Nullable ActionType actionType, @Nullable String specificBotName) {
@Nullable ActionType actionType, @Nullable String specificBotName,
Node attachedNode) {
this.clazz = paramClass;
this.consumer = consumer;
this.checker = checker;
Expand All @@ -44,6 +47,8 @@ public EventListener(Class<T> paramClass,

this.isWaitingLogEvent = actionType != null;
this.logType = actionType;

this.attachedNode = attachedNode;
}

public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) {
Expand Down Expand Up @@ -84,4 +89,46 @@ public String hash() {
public Class<T> getClazz() {
return clazz;
}

//region Getters


public Node getAttachedNode() {
return attachedNode;
}

public @Nullable String getSpecificBotName() {
return specificBotName;
}

public boolean isEnabled() {
return enabled;
}

public BiConsumer<T, GuildAuditLogEntryCreateEvent> getConsumer() {
return consumer;
}

public Predicate<T> getChecker() {
return checker;
}

public boolean isWaitingLogEvent() {
return isWaitingLogEvent;
}

public @Nullable ActionType getLogType() {
return logType;
}

public Predicate<GuildAuditLogEntryCreateEvent> getLogChecker() {
return logChecker;
}

@Nullable
public T getLastEvent() {
return lastEvent;
}

//endregion
}
119 changes: 75 additions & 44 deletions src/main/java/info/itsthesky/disky/core/DiSkyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import info.itsthesky.disky.DiSky;
import info.itsthesky.disky.api.modules.DiSkyModule;
import info.itsthesky.disky.managers.ConfigManager;
import info.itsthesky.disky.managers.CoreEventListener;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDAInfo;
import net.dv8tion.jda.api.requests.GatewayIntent;
Expand All @@ -20,7 +21,9 @@
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class DiSkyCommand implements CommandExecutor {
@Override
Expand All @@ -33,7 +36,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
sender.sendMessage(Utils.colored("&b/disky modules &7- &9Show the list of modules."));
sender.sendMessage(Utils.colored("&b/disky bots &7- &9Show the list of loaded bots."));
sender.sendMessage(Utils.colored("&b/disky bot <bot> &7- &9Show info about a specific bot."));
sender.sendMessage(Utils.colored("&b/disky debug &7- &9Store the debug information inside the 'plugins/DiSky/debug.txt' file."));
sender.sendMessage(Utils.colored("&b/disky debug [sections] &7- &9Store the debug information inside the 'plugins/DiSky/debug.txt' file."));
sender.sendMessage(Utils.colored("&b/disky reloadconfig &7- &9Reload DiSky's configuration"));
sender.sendMessage(Utils.colored("&b/disky reload <module name> &7- &4&lBETA &9Reload a specific module."));
sender.sendMessage(Utils.colored(""));
Expand All @@ -54,44 +57,72 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
sb.append("---- DiSky Debug File ----\n");
sb.append("// I hopes you're all good my friend? :c\n\n");

sb.append("== | OS Information\n\n");
sb.append("Time: ").append(Date.now()).append("\n");
sb.append("Memory: ").append(Utils.formatBytes(Runtime.getRuntime().totalMemory())).append("\n");
sb.append("Free Memory: ").append(Utils.formatBytes(Runtime.getRuntime().freeMemory())).append("\n");
sb.append("Used Memory: ").append(Utils.formatBytes(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())).append("\n");
sb.append("Max Memory: ").append(Utils.formatBytes(Runtime.getRuntime().maxMemory())).append("\n");
sb.append("Operating System: ").append(System.getProperty("os.name")).append("\n");
sb.append("\n");

sb.append("== | Server Information\n\n");
sb.append("Software: ").append(Bukkit.getVersion()).append("\n");
sb.append("Bukkit Version: ").append(Bukkit.getBukkitVersion()).append("\n");
sb.append("Installed Plugins: ").append(Bukkit.getPluginManager().getPlugins().length).append("\n");
for (Plugin plugin : Bukkit.getPluginManager().getPlugins())
sb.append(" - ").append(plugin.getName()).append(" v").append(plugin.getDescription().getVersion()).append("\n");
sb.append("\n");

sb.append("== | DiSky Information\n\n");
sb.append("Version: ").append(DiSky.getInstance().getDescription().getVersion()).append("\n");
sb.append("JDA Version: ").append(JDAInfo.VERSION).append("\n");
sb.append("Main JDA Class: ").append(JDA.class.getName()).append("\n");
sb.append("Loaded Bots: ").append(DiSky.getManager().getBots().size()).append("\n");
for (Bot bot : DiSky.getManager().getBots())
sb.append(" - ").append(bot.getName()).append(" login in as ").append(bot.getInstance().getSelfUser().getName()).append("#").append(bot.getInstance().getSelfUser().getDiscriminator()).append("\n");
sb.append("\n");

sb.append("== | Modules Information\n\n");
sb.append("Loaded Modules: ").append(DiSky.getModuleManager().getModules().size()).append("\n");
for (DiSkyModule module : DiSky.getModuleManager().getModules())
sb.append(" - ").append(module.getName()).append(" v").append(module.getVersion()).append("\n");
sb.append("\n");

sb.append("== | Skript Information\n\n");
sb.append("Version: ").append(Skript.getVersion()).append("\n");
sb.append("Loaded Addons: ").append(Skript.getAddons().size()).append("\n");
for (SkriptAddon addon : Skript.getAddons())
sb.append(" - ").append(addon.getName()).append(" [").append(addon.getFile()).append("]\n");
sb.append("\n");
Map<String, Runnable> debugSections = new HashMap<>();
debugSections.put("os", () -> {
sb.append("== | OS Information\n\n");
sb.append("Time: ").append(Date.now()).append("\n");
sb.append("Memory: ").append(Utils.formatBytes(Runtime.getRuntime().totalMemory())).append("\n");
sb.append("Free Memory: ").append(Utils.formatBytes(Runtime.getRuntime().freeMemory())).append("\n");
sb.append("Used Memory: ").append(Utils.formatBytes(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())).append("\n");
sb.append("Max Memory: ").append(Utils.formatBytes(Runtime.getRuntime().maxMemory())).append("\n");
sb.append("Operating System: ").append(System.getProperty("os.name")).append("\n");
sb.append("\n");
});
debugSections.put("server", () -> {
sb.append("== | Server Information\n\n");
sb.append("Software: ").append(Bukkit.getVersion()).append("\n");
sb.append("Bukkit Version: ").append(Bukkit.getBukkitVersion()).append("\n");
sb.append("Installed Plugins: ").append(Bukkit.getPluginManager().getPlugins().length).append("\n");
for (Plugin plugin : Bukkit.getPluginManager().getPlugins())
sb.append(" - ").append(plugin.getName()).append(" v").append(plugin.getDescription().getVersion()).append("\n");
sb.append("\n");
});
debugSections.put("disky", () -> {
sb.append("== | DiSky Information\n\n");
sb.append("Version: ").append(DiSky.getInstance().getDescription().getVersion()).append("\n");
sb.append("JDA Version: ").append(JDAInfo.VERSION).append("\n");
sb.append("Main JDA Class: ").append(JDA.class.getName()).append("\n");
sb.append("Loaded Bots: ").append(DiSky.getManager().getBots().size()).append("\n");
for (Bot bot : DiSky.getManager().getBots())
sb.append(" - ").append(bot.getName()).append(" login in as ").append(bot.getInstance().getSelfUser().getName()).append("#").append(bot.getInstance().getSelfUser().getDiscriminator()).append("\n");
sb.append("\n");
});
debugSections.put("modules", () -> {
sb.append("== | Modules Information\n\n");
sb.append("Loaded Modules: ").append(DiSky.getModuleManager().getModules().size()).append("\n");
for (DiSkyModule module : DiSky.getModuleManager().getModules())
sb.append(" - ").append(module.getName()).append(" v").append(module.getVersion()).append("\n");
sb.append("\n");
});
debugSections.put("skript", () -> {
sb.append("== | Skript Information\n\n");
sb.append("Version: ").append(Skript.getVersion()).append("\n");
sb.append("Loaded Addons: ").append(Skript.getAddons().size()).append("\n");
for (SkriptAddon addon : Skript.getAddons())
sb.append(" - ").append(addon.getName()).append(" [").append(addon.getFile()).append("]\n");
sb.append("\n");
});
debugSections.put("listeners", () -> {
sb.append("== | (DiSky) Listeners Information\n\n");
sb.append("Loaded Listeners: ").append(CoreEventListener.AllRegisteredListeners.size()).append("\n");
for (info.itsthesky.disky.api.events.EventListener<?> listener : CoreEventListener.AllRegisteredListeners) {
sb.append(" - ").append(listener.getAttachedNode()).append(" / ").append(listener.isEnabled() ? "Enabled" : "Disabled").append("\n");
sb.append(" - Specific Bot Name: ").append(listener.getSpecificBotName()).append("\n");
sb.append(" - Waiting Log Event: ").append(listener.isWaitingLogEvent())
.append(" [LogType: ").append(listener.getLogType()).append("]").append("\n");
sb.append(" - Enabled: ").append(listener.enabled).append("\n");
}
});

var desiredSections = args.length > 1 ? args[1].split(",") : debugSections.keySet().toArray(String[]::new);
for (String section : desiredSections) {
if (debugSections.containsKey(section)) {
debugSections.get(section).run();
} else {
var closest = Utils.getClosest(section, debugSections.keySet());
sender.sendMessage(Utils.colored("&cUnknown debug section: &b" + section + " &7(Did you mean &b" + closest + "&7?)"));
}
}

sb.append("\n---- End of the Debug File ----");

Expand All @@ -100,7 +131,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
if (output.exists())
output.delete();
Files.write(output.toPath(), sb.toString().getBytes(StandardCharsets.UTF_8));
sender.sendMessage(Utils.colored("&b------ &aSuccess! &b------"));
sender.sendMessage(Utils.colored("&b------ &aSuccess! Exported to &2" + output.getAbsolutePath() + " &b------"));
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down Expand Up @@ -134,7 +165,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
sender.sendMessage(Utils.colored(" &7- &3Discord Name: &b" + bot.getDiscordName()));
sender.sendMessage(Utils.colored(" &7- &3Uptime: &b" + bot.getUptime()));
sender.sendMessage(Utils.colored(" &7- &3Ping: &b" + bot.getInstance().getGatewayPing() + "ms"));
sender.sendMessage(Utils.colored(" &7- &3Gateway Intents ("+bot.getInstance().getGatewayIntents().size()+"):"));
sender.sendMessage(Utils.colored(" &7- &3Gateway Intents (" + bot.getInstance().getGatewayIntents().size() + "):"));
for (GatewayIntent intent : bot.getInstance().getGatewayIntents())
sender.sendMessage(Utils.colored(" &7- &b" + intent.name().toLowerCase().replace("_", " ")));
/*sender.sendMessage(Utils.colored(" &7- &3Shards ("+bot.getInstance().getShardInfo().getShardTotal()+"):"));
Expand Down Expand Up @@ -167,7 +198,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}
}

sender.sendMessage(Utils.colored("&b------ &aSuccess! Took &2"+( System.currentTimeMillis() - before )+"ms! &b------"));
sender.sendMessage(Utils.colored("&b------ &aSuccess! Took &2" + (System.currentTimeMillis() - before) + "ms! &b------"));
} else {
final @Nullable DiSkyModule module = DiSky.getModule(moduleName);

Expand All @@ -190,15 +221,15 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
sender.sendMessage(Utils.colored("&cFailed to reload module &b" + module.getName() + "&c!"));
ex.printStackTrace();
}
sender.sendMessage(Utils.colored("&b------ &aSuccess! Took &2"+( System.currentTimeMillis() - before )+"ms! &b------"));
sender.sendMessage(Utils.colored("&b------ &aSuccess! Took &2" + (System.currentTimeMillis() - before) + "ms! &b------"));
}
return true;
} else if (args[0].equalsIgnoreCase("reloadconfig")) {
sender.sendMessage(Utils.colored("&b------ &9DiSky v" + DiSky.getInstance().getDescription().getVersion() + " Configuration Reloading &b------"));
boolean wasDebug = ConfigManager.get("debug", false);
long before = System.currentTimeMillis();
ConfigManager.reloadConfig(DiSky.getInstance());
sender.sendMessage(Utils.colored("&b------ &aSuccess! Took &2"+( System.currentTimeMillis() - before )+"ms! &b------"));
sender.sendMessage(Utils.colored("&b------ &aSuccess! Took &2" + (System.currentTimeMillis() - before) + "ms! &b------"));
if (wasDebug != ConfigManager.get("debug", false) && ConfigManager.get("debug", false))
sender.sendMessage(Utils.colored("&5--------> &dDebug mode has been enabled!"));
return true;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/info/itsthesky/disky/core/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,31 @@ public static String convertEmbedToJSON(EmbedBuilder builder) {
final MessageEmbed embed = builder.build();
return new String(embed.toData().toJson(), StandardCharsets.UTF_8);
}

public static String getClosest(String input, Set<String> others) {
final List<String> list = Lists.newArrayList(others);
return list.stream().min(Comparator.comparingInt(s -> LevenshteinDistance(input, s))).orElse(null);
}

public static int LevenshteinDistance(String s, String t) {
int m = s.length();
int n = t.length();
int[][] d = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
d[i][0] = i;
}
for (int j = 0; j <= n; j++) {
d[0][j] = j;
}
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= m; i++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
d[i][j] = d[i - 1][j - 1];
} else {
d[i][j] = Math.min(d[i - 1][j] + 1, Math.min(d[i][j - 1] + 1, d[i - 1][j - 1] + 1));
}
}
}
return d[m][n];
}
}

0 comments on commit 4a20cc6

Please sign in to comment.