Logging Improvements

This commit is contained in:
unknown 2022-04-04 00:27:20 -07:00
parent 4c0181c87f
commit 43201fdac4
14 changed files with 82 additions and 16 deletions

View file

@ -79,7 +79,6 @@ public class Kiafumi {
* Ran on program start. Anything in here can determine whether the program will start.
*/
public void start() {
instance = this;
logger.info("Starting Kiafumi.");
@ -210,16 +209,13 @@ public class Kiafumi {
* @param guild - guild to have commands provided to
*/
public void registerForGuild(Guild guild) {
info("Standby for command strip on guild " + guild.getId());
info("Registering commands for " + guild.getId());
info("Registering commands for Guild[" + guild.getId() + "]");
int i = 0;
for(CommandClass cmd : activeCommands) {
for(CommandInfo ci : cmd.getSlashCommandInfo()) {
info("Registering Command: " + ci.getName());
CommandCreateAction cca = guild.upsertCommand(ci.getName(), ci.getDescription());
i++;
if(ci.hasSubCommands()) {
info("Command has sub-commands.");
for (String name : ci.getSubCommands().keySet()) {
CommandInfo si = ci.getSubCommands().get(name);
SubcommandData sd = new SubcommandData(si.getName(), si.getDescription());
@ -227,18 +223,16 @@ public class Kiafumi {
sd.addOption(si.getOptions().get(option), option, si.getOptionDescriptions().get(option), si.getOptionRequirements().get(option));
}
cca.addSubcommands(sd);
info("Added subcommand: " + name);
}
}
if(ci.hasOptions()) {
info("Command has options.");
for(String name : ci.getOptions().keySet()) {
//Any intelligent IDE will rage about the option not being used, it's added to the action then executed later, DO not edit this (please).
cca.addOption(ci.getOptions().get(name), name, ci.getOptionDescriptions().get(name), ci.getOptionRequirements().get(name));
}
}
//Push w/ modifications.
info("Command: " + ci.getName() + " registration on " + guild.getId() + " completed.");
//commented for spam sake info("Command: " + ci.getName() + " registration on " + guild.getId() + " completed.");
try {
cca.queue();
} catch (Exception ex) {
@ -247,7 +241,7 @@ public class Kiafumi {
}
}
}
info("Registered " + i + " commands.");
info("Registered " + i + " commands. On Guild[" + guild.getId() + "] \uD83D\uDC4D -> \uD83D\uDCA5");
}
/*
@ -269,4 +263,4 @@ public class Kiafumi {
//Gets active ServerManager
public ServerManager getServerManager() { return serverManager; }
}
}

View file

@ -50,13 +50,13 @@ public class CommandRegistrar {
//moe.oko.Kiafumi path.
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.");
info("[CommandRegistrar]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.");
info("[CommandRegistrar]Instance found (" + cnstr.getName() + ")! Registering.");
commands.add((CommandClass) obj);
}
} catch (InstantiationException ex) {
@ -64,11 +64,12 @@ public class CommandRegistrar {
}
}
}
info("[CommandRegistrar]CommandClasses loaded [" + commands.size() + "]");
return commands;
} catch (IllegalAccessException | InvocationTargetException exception) {
//Now we don't ignore, this is a core issue.
exception.printStackTrace();
error("fucky wucky in class loading.");
error("[CommandRegistrar EEE_E_E_E_E_]fucky wucky in class loading.");
return null;
}
}

View file

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
import static moe.oko.Kiafumi.util.ResponseHandlers.STRING_RESPONSE_HANDLER;
/**
@ -41,6 +42,7 @@ public class CatCommand extends CommandClass {
public void newCommand(String name, SlashCommandInteractionEvent e) {
switch (name) {
case "cat":
slashLog(e);
e.deferReply().queue();
CloseableHttpClient httpClient = HttpClients.createDefault();

View file

@ -13,6 +13,8 @@ import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
/**
* For use on Guilds that tend to get TOS'd with ease. Also just basic server protection
* @author Kay
@ -35,6 +37,7 @@ public class ModCommand extends CommandClass {
Server server = Kiafumi.instance.getServerManager().getOrCreateServer(e.getGuild());
switch (name) {
case "mod":
slashLog(e);
e.deferReply(true).queue();
if(server.isServerProtected()) {
//Start check.

View file

@ -30,6 +30,8 @@ import java.io.IOException;
import java.util.*;
import java.util.List;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
/**
* Music Command
* Most code taken from SHIRO Project (ISC License still applies)
@ -71,6 +73,7 @@ public class MusicCommand extends CommandClass {
if(e.getGuild() != null) {
switch (name) {
case "nowplaying":
slashLog(e);
e.deferReply().queue();
if (!hasPlayer(e.getGuild()) || getPlayer(e.getGuild()).getPlayingTrack() == null) { // No song is playing
e.getHook().sendMessage("No song is playing.").queue();
@ -88,6 +91,7 @@ public class MusicCommand extends CommandClass {
}
break;
case "queue":
slashLog(e);
e.deferReply().queue();
if (!hasPlayer(e.getGuild()) || getTrackManager(e.getGuild()).getQueuedTracks().isEmpty()) {
e.getHook().sendMessage("The queue is empty.").queue();
@ -119,6 +123,7 @@ public class MusicCommand extends CommandClass {
}
break;
case "skip":
slashLog(e);
e.deferReply().queue();
if (isIdle(e.getHook(), e.getGuild())) return;
@ -141,6 +146,7 @@ public class MusicCommand extends CommandClass {
}
break;
case "forceskip":
slashLog(e);
e.deferReply().queue();
if (isIdle(e.getHook(), e.getGuild())) return;
@ -152,6 +158,7 @@ public class MusicCommand extends CommandClass {
}
break;
case "reset":
slashLog(e);
e.deferReply().queue();
if (!e.getMember().getPermissions().contains(Permission.ADMINISTRATOR) && !e.getMember().isOwner()) {
e.getHook().sendMessage("You don't have the required permissions to do that! [ADMIN]").queue();
@ -161,6 +168,7 @@ public class MusicCommand extends CommandClass {
}
break;
case "shuffle":
slashLog(e);
e.deferReply().queue();
if (isIdle(e.getHook(), e.getGuild())) return;
@ -182,6 +190,7 @@ public class MusicCommand extends CommandClass {
case "play":
e.deferReply().queue();
String input = e.getOption("url").getAsString();
slashLog(e, "INPUT " + input);
if(input.contains("https://")) {
loadTrack(input, e.getMember(), e.getHook());
} else {

View file

@ -14,6 +14,8 @@ import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
/**
* Helpful Search Command (Uses DDG API)
* @author Kay
@ -36,6 +38,7 @@ public class DuckCommand extends CommandClass {
if ("search".equals(name)) {
e.deferReply().queue();
String option = e.getOption("query").getAsString();
slashLog(e, "WITH QUERY " + option);
WebSearch ws = WebSearch.instanceOf();
SearchResult sr;
try {

View file

@ -12,6 +12,8 @@ import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
public class HelpCommand extends CommandClass {
@Override
public boolean isEnabled() {
@ -26,6 +28,7 @@ public class HelpCommand extends CommandClass {
@Override
public void newCommand(String name, SlashCommandInteractionEvent e) {
if ("help".equalsIgnoreCase(name)) {
slashLog(e);
e.deferReply().queue();
//We assemble a help string, this is our description for our embed.
StringBuilder sb = new StringBuilder();

View file

@ -14,6 +14,8 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
/**
* Helpful User Information Command
* @author oko
@ -36,7 +38,7 @@ public class InfoCommand extends CommandClass {
? e.getMember()
: e.getOption("user").getAsMember();
final var dTF = DateTimeFormatter.ofPattern("MM-dd-yyyy");
slashLog(e, "FOR USER " + member.getUser().getName() + ":" + member.getId());
// Build embed
EmbedBuilder eb1 = new EmbedBuilder()
.setColor(EmbedUI.INFO)
@ -54,7 +56,7 @@ public class InfoCommand extends CommandClass {
// Setup variables
final var guild = e.getGuild();
final var dTF2 = DateTimeFormatter.ofPattern("MM-dd-yyyy");
slashLog(e, "FOR GUILD[" + guild.getId() + ":" + guild.getName() + "]");
// Build Embed
EmbedBuilder eb2 = new EmbedBuilder()
.setColor(EmbedUI.INFO)

View file

@ -12,6 +12,8 @@ import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
/**
* Helpful Invite Command
* @author Kay, oko
@ -31,6 +33,7 @@ public class InviteCommand extends CommandClass {
public void newCommand(String name, SlashCommandInteractionEvent e) {
if(e.getGuild() == null) { return; }
if ("invite".equals(name)) {
slashLog(e);
e.deferReply().queue();
EmbedBuilder eb = new EmbedBuilder()
.setColor(EmbedUI.INFO)

View file

@ -14,6 +14,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Random;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
/**
* Helpful Ping Command
* @author Kay
@ -33,6 +35,7 @@ public class PingCommand extends CommandClass {
@Override
public void newCommand(String name, SlashCommandInteractionEvent e) {
if ("ping".equals(name.toLowerCase(Locale.ROOT))) {
slashLog(e);
e.deferReply().queue();
long sentMs = e.getTimeCreated().toInstant().toEpochMilli();
long recMs = System.currentTimeMillis();

View file

@ -15,6 +15,9 @@ import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import static moe.oko.Kiafumi.util.EmbedUI.slashLog;
import static moe.oko.Kiafumi.util.EmbedUI.slashResponse;
/**
* Permits modification of server settings, critical class to functionality.
* @author Kay
@ -36,6 +39,7 @@ public class SettingCommand extends CommandClass {
Server server = Kiafumi.instance.getServerManager().getOrCreateServer(e.getGuild());
switch (name) {
case "settings" -> {
slashLog(e);
e.deferReply().queue();
//No options, just fire an embed off...
EmbedBuilder eb = new EmbedBuilder()
@ -49,7 +53,8 @@ public class SettingCommand extends CommandClass {
}
case "setting" -> {
//User is attempting a settings modification. Check if admin.
if (!e.getMember().hasPermission(Permission.ADMINISTRATOR) && !e.getMember().isOwner()) {
if(!e.getMember().hasPermission(Permission.ADMINISTRATOR) && !e.getMember().isOwner()) {
slashLog(e, "FAILED PERM CHECK");
e.deferReply(true).queue();
//Private reply, other people can't see this if ephemeral.
e.getHook().sendMessage("**You cannot run this command**").queue();
@ -59,6 +64,7 @@ public class SettingCommand extends CommandClass {
case "view" -> {
e.deferReply().queue();
String opt = e.getOption("name").getAsString();
slashLog(e, "SUBCOMMAND[VIEW] FOR " + opt);
EmbedBuilder eb1 = new EmbedBuilder()
.setColor(EmbedUI.INFO)
.setTitle(opt)
@ -72,7 +78,9 @@ public class SettingCommand extends CommandClass {
e.deferReply().queue();
String opt1 = e.getOption("name").getAsString();
String opt2 = e.getOption("value").getAsString();
slashLog(e, "SUBCOMMAND[SET] FOR " + opt1 + " TO " + opt2);
String response = server.setOptionByString(opt1, opt2);
slashResponse(e, response);
EmbedBuilder eb2 = new EmbedBuilder()
.setColor(EmbedUI.SUCCESS)
.setTitle(opt1)
@ -85,7 +93,9 @@ public class SettingCommand extends CommandClass {
case "clear" -> {
e.deferReply().queue();
String opt3 = e.getOption("name").getAsString();
slashLog(e, "SUBCOMMAND[CLEAR] FOR " + opt3);
String response1 = server.resetOptionByString(opt3);
slashResponse(e, response1);
EmbedBuilder eb3 = new EmbedBuilder()
.setColor(EmbedUI.SUCCESS)
.setTitle(opt3)

View file

@ -11,6 +11,7 @@ import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.user.update.GenericUserPresenceEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
@ -53,6 +54,14 @@ public class MainListener extends ListenerAdapter {
" Guilds Active: " + event.getGuildAvailableCount() + " Guilds Unavailable: " + event.getGuildUnavailableCount());
}
/**
* Slash Command Logging
*/
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
//soontm
}
/**
* Quick Response for if someone pings me.
*/

View file

@ -1,7 +1,12 @@
package moe.oko.Kiafumi.util;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import javax.annotation.Nullable;
import java.awt.Color;
import static moe.oko.Kiafumi.Kiafumi.info;
/**
* EmbedUI Class
* @author oko
@ -20,4 +25,20 @@ public abstract class EmbedUI {
public static final Color FAILURE = new Color(255,111,97);
public static final Color INFO = new Color(123,196,196);
/**
* Used for logging commands with ease to console via a static method.
* @param event - the event ran
* @param msg - Any message to append with this.
*/
public static void slashLog(SlashCommandInteractionEvent event, @Nullable String msg) {
info("User[" + event.getUser().getName() + ":" + event.getUser().getId() + "] RAN " + event.getName() + "." + (msg == null ? "" : msg));
}
public static void slashLog(SlashCommandInteractionEvent event) {
info("User[" + event.getUser().getName() + ":" + event.getUser().getId() + "] RAN " + event.getName() + ".");
}
public static void slashResponse(SlashCommandInteractionEvent event, String msg) {
info("User[" + event.getUser().getName() + ":" + event.getUser().getId() + "] CMD RESPONSE: " + msg);
}
}

View file

@ -11,6 +11,9 @@ import org.apache.http.util.EntityUtils;
* Such as, Steam, DDG, ProtonDB, CatApi, etc.
*/
public class ResponseHandlers {
/**
* @apiNote Returns GET response in a uniformed string. Use JSONArray to convert it into JSON.
**/
public static final ResponseHandler<String> STRING_RESPONSE_HANDLER = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {