MORE improvement + rename

This commit is contained in:
Chayapak 2023-07-16 14:58:30 +07:00
parent fbb6758cc8
commit 01c19ad4bd
6 changed files with 105 additions and 51 deletions

View file

@ -2,6 +2,7 @@ package land.chipmunk.chayapak.chomens_bot;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import land.chipmunk.chayapak.chomens_bot.plugins.ConsolePlugin;
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
@ -59,7 +60,7 @@ public class Main {
configWriter.write(defaultConfig);
configWriter.close();
System.out.println("config.yml file not found, so the default one was created");
LoggerUtilities.info("config.yml file not found, so the default one was created");
}
InputStream opt = new FileInputStream(file);
@ -85,7 +86,7 @@ public class Main {
jda = builder.build();
jda.awaitReady();
} catch (LoginException e) {
System.err.println("Failed to login to Discord, stacktrace:");
LoggerUtilities.error("Failed to login to Discord, stacktrace:");
e.printStackTrace();
System.exit(1);
} catch (InterruptedException ignored) {
@ -119,7 +120,7 @@ public class Main {
} catch (UnknownHostException ignored) {}
if (!reachable) {
System.err.println("No internet, exiting");
LoggerUtilities.error("No internet, exiting");
System.exit(1);
}

View file

@ -9,6 +9,7 @@ import land.chipmunk.chayapak.chomens_bot.command.DiscordCommandContext;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
import land.chipmunk.chayapak.chomens_bot.util.CodeBlockUtilities;
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
@ -312,7 +313,7 @@ public class DiscordPlugin {
final TextChannel logChannel = jda.getTextChannelById(channelId);
if (logChannel == null) {
System.out.println("Log channel for " + channelId + " is null");
LoggerUtilities.error("Log channel for " + channelId + " is null");
return;
}

View file

@ -3,13 +3,9 @@ package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.util.FileLoggerUtilities;
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LoggerPlugin extends ChatPlugin.Listener {
private final Bot bot;
@ -18,8 +14,6 @@ public class LoggerPlugin extends ChatPlugin.Listener {
public boolean logToConsole = true;
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
public LoggerPlugin(Bot bot) {
this.bot = bot;
@ -58,48 +52,23 @@ public class LoggerPlugin extends ChatPlugin.Listener {
});
}
// ported from chomens bot js
private String prefix (Component prefix, String _message) {
LocalDateTime dateTime = LocalDateTime.now();
final Component message = Component.translatable(
"[%s %s] [%s] [%s] %s",
Component.text(dateTime.format(dateTimeFormatter)).color(NamedTextColor.GRAY),
prefix,
Component.text(Thread.currentThread().getName()).color(NamedTextColor.GRAY),
Component.text(bot.options.serverName).color(NamedTextColor.GRAY),
Component.text(_message).color(NamedTextColor.WHITE)
).color(NamedTextColor.DARK_GRAY);
return ComponentUtilities.stringifyAnsi(message);
public void log (String message) {
LoggerUtilities.log(bot, message, true, logToConsole);
}
public void log (String message, boolean logToFile, boolean logToConsole) {
LoggerUtilities.log(bot, message, logToFile, logToConsole);
}
public void log (String message) { log(message, true, logToConsole); }
public void log (String _message, boolean logToFile, boolean logToConsole) {
final String message = prefix(Component.text("Log").color(NamedTextColor.GOLD), _message);
public void info (String message) {
if (!logToConsole) return;
if (logToConsole) bot.console.reader.printAbove(message);
else if (logToFile) {
final String formattedMessage = String.format(
"[%s] %s",
bot.host + ":" + bot.port,
_message
);
FileLoggerUtilities.log(formattedMessage);
}
LoggerUtilities.info(bot, message);
}
public void info (String _message) {
final String message = prefix(Component.text("Info").color(NamedTextColor.GREEN), _message);
public void custom (Component prefix, Component message) {
if (!logToConsole) return;
if (logToConsole) bot.console.reader.printAbove(message);
}
public void custom (Component prefix, Component _message) {
final String message = prefix(prefix, ComponentUtilities.stringifyAnsi(_message));
if (logToConsole) bot.console.reader.printAbove(message);
LoggerUtilities.custom(bot, prefix, message);
}
@Override

View file

@ -13,7 +13,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;
public class AES {
public class AESUtilities {
private static final SecureRandom RANDOM = new SecureRandom();
private static final String CIPHER = "AES/CBC/PKCS5Padding";

View file

@ -0,0 +1,83 @@
package land.chipmunk.chayapak.chomens_bot.util;
import land.chipmunk.chayapak.chomens_bot.Bot;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LoggerUtilities {
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
// function ported from chomens bot js (well, modified)
private static String prefix (Bot bot, Component prefix, String _message) {
final LocalDateTime dateTime = LocalDateTime.now();
Component message;
if (bot != null) {
message = Component.translatable(
"[%s %s] [%s] [%s] %s",
Component.text(dateTime.format(dateTimeFormatter)).color(NamedTextColor.GRAY),
prefix,
Component.text(Thread.currentThread().getName()).color(NamedTextColor.GRAY),
Component.text(bot.options.serverName).color(NamedTextColor.GRAY),
Component.text(_message).color(NamedTextColor.WHITE)
).color(NamedTextColor.DARK_GRAY);
} else {
message = Component.translatable(
"[%s %s] [%s] %s",
Component.text(dateTime.format(dateTimeFormatter)).color(NamedTextColor.GRAY),
prefix,
Component.text(Thread.currentThread().getName()).color(NamedTextColor.GRAY),
Component.text(_message).color(NamedTextColor.WHITE)
).color(NamedTextColor.DARK_GRAY);
}
return ComponentUtilities.stringifyAnsi(message);
}
public static void log (String message) { log(null, message, true, true); }
public static void log (Bot bot, String message) { log(bot, message, true, true); }
public static void log (Bot bot, String message, boolean logToFile, boolean logToConsole) {
final String component = prefix(bot, Component.text("Log").color(NamedTextColor.GOLD), message);
if (logToConsole && bot != null) bot.console.reader.printAbove(component);
else if (logToConsole) System.out.println(component);
if (logToFile) {
final String formattedMessage = bot == null ? "" :
String.format(
"[%s] %s",
bot.host + ":" + bot.port,
message
);
FileLoggerUtilities.log(formattedMessage);
}
}
public static void info (String message) { info(null, message); }
public static void info (Bot bot, String message) {
final String component = prefix(bot, Component.text("Info").color(NamedTextColor.GREEN), message);
if (bot != null) bot.console.reader.printAbove(component);
else System.out.println(component);
}
public static void error (String message) { error(null, message); }
public static void error (Bot bot, String message) {
final String component = prefix(bot, Component.text("Error").color(NamedTextColor.RED), message);
if (bot != null) bot.console.reader.printAbove(component);
else System.out.println(component);
}
public static void custom (Component prefix, Component message) { custom(null, prefix, message); }
public static void custom (Bot bot, Component prefix, Component _message) {
final String message = prefix(bot, prefix, ComponentUtilities.stringifyAnsi(_message));
if (bot != null) bot.console.reader.printAbove(message);
else System.out.println(message);
}
}

View file

@ -2,7 +2,7 @@ package land.chipmunk.chayapak.chomens_bot.voiceChat;
import io.netty.buffer.Unpooled;
import land.chipmunk.chayapak.chomens_bot.data.voiceChat.RawUdpPacket;
import land.chipmunk.chayapak.chomens_bot.util.AES;
import land.chipmunk.chayapak.chomens_bot.util.AESUtilities;
import land.chipmunk.chayapak.chomens_bot.util.FriendlyByteBuf;
import land.chipmunk.chayapak.chomens_bot.voiceChat.packets.*;
@ -64,7 +64,7 @@ public class NetworkMessage {
private static NetworkMessage readFromBytes(SocketAddress socketAddress, UUID secret, byte[] encryptedPayload, long timestamp) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
byte[] decrypt;
try {
decrypt = AES.decrypt(secret, encryptedPayload);
decrypt = AESUtilities.decrypt(secret, encryptedPayload);
} catch (Exception e) {
e.printStackTrace();
return null;
@ -117,6 +117,6 @@ public class NetworkMessage {
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
return AES.encrypt(secret, bytes);
return AESUtilities.encrypt(secret, bytes);
}
}