add logback logging and compatability. Also test Ping command.

This commit is contained in:
Tueem 2024-12-23 01:25:01 +01:00
parent 3473657fbb
commit 2b3423c1c9
Signed by: tueem
GPG Key ID: F2CE0513D231AD7A
6 changed files with 121 additions and 38 deletions

View File

@ -14,6 +14,9 @@ plugins {
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
maven {
url = uri("https://git.tomatentum.net/api/packages/tueem/maven")
}
}
dependencies {
@ -23,16 +26,23 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// This dependency is used by the application.
implementation(libs.guava)
implementation(libs.javacord)
implementation(libs.dotenv)
implementation(libs.log4j)
implementation(libs.slf4j)
implementation(libs.logback)
implementation(libs.log4jtoslf4j)
implementation(libs.jultoslf4j)
implementation(libs.jline)
implementation(libs.marinaralib)
implementation(libs.marinarajavacord)
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(23)
}
}

View File

@ -1,54 +1,43 @@
package net.tomatentum.tomatenmusic3;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import java.io.IOException;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.intent.Intent;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import io.github.cdimascio.dotenv.Dotenv;
import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
import net.tomatentum.tomatenmusic3.command.PingCommand;
public class App {
public static void main(String[] args) {
public static void main(String[] args) {
new App().connect();
}
private static String LOGGERPATTERN = "%-22d{dd MMM yyyy HH:mm:ss} (%t) [%c{3}] %p: %m%n";
private Config config;
private DiscordApi client;
private Logger logger;
private Logger logger = LoggerFactory.getLogger(getClass());
private Terminal terminal;
private Marinara marinara;
private App() {
Dotenv env = Dotenv.configure().ignoreIfMissing().load();
this.config = new Config(env);
initLogger();
}
private void initLogger() {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
AppenderComponentBuilder cbuilder =
builder.newAppender("Console", "Console")
.add(builder
.newLayout("PatternLayout")
.addAttribute("pattern", LOGGERPATTERN)
);
RootLoggerComponentBuilder rlbuilder =
builder.newRootLogger(config.isDevelopment() ? Level.DEBUG : Level.INFO)
.add(builder.newAppenderRef("Console"));
BuiltConfiguration logconf = builder.add(cbuilder).add(rlbuilder).build();
Configurator.reconfigure(logconf);
logger = LogManager.getLogger(this);
LoggerContext loggerctx = (LoggerContext) LoggerFactory.getILoggerFactory();
if (config.isDevelopment())
loggerctx.getLogger("root").setLevel(Level.DEBUG);
initJline();
}
public void connect() {
@ -56,9 +45,24 @@ public class App {
.setToken(config.token())
.addIntents(Intent.GUILD_VOICE_STATES)
.login().join();
logger.log(Level.INFO, "connected as {}", client.getYourself().getName());
initMarinara();
logger.info("connected as {}", client.getYourself().getName());
}
private void initJline() {
try {
this.terminal = TerminalBuilder.terminal();
} catch (IOException e) {
e.printStackTrace();
}
JlineAppender.Terminal = this.terminal;
}
private void initMarinara() {
this.marinara = Marinara.load(new JavacordWrapper(client));
marinara.getRegistry().addInteractions(new PingCommand());
marinara.getRegistry().registerCommands();
}
}

View File

@ -0,0 +1,21 @@
package net.tomatentum.tomatenmusic3;
import java.io.PrintWriter;
import org.jline.terminal.Terminal;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
public class JlineAppender extends ConsoleAppender<ILoggingEvent> {
public static Terminal Terminal;
@Override
protected void append(ILoggingEvent event) {
PrintWriter terminalWriter = Terminal.writer();
terminalWriter.write(new String(super.encoder.encode(event)));
terminalWriter.flush();
}
}

View File

@ -0,0 +1,27 @@
package net.tomatentum.tomatenmusic3.command;
import java.time.Duration;
import java.time.Instant;
import org.javacord.api.entity.message.MessageFlag;
import org.javacord.api.interaction.SlashCommandInteraction;
import net.tomatentum.marinara.interaction.InteractionHandler;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
public class PingCommand implements InteractionHandler {
@SlashCommand(
name = "ping",
description = "Tests bot's connection."
)
public void execPing(SlashCommandInteraction interaction) {
Duration ping = Duration.between(interaction.getCreationTimestamp(), Instant.now());
interaction.createImmediateResponder()
.append("Pong! " + ping.toMillis() + "ms")
.setFlags(MessageFlag.EPHEMERAL)
.respond().join();
}
}

View File

@ -0,0 +1,11 @@
<configuration>
<appender name="jline" class="net.tomatentum.tomatenmusic3.JlineAppender">
<encoder>
<pattern>%d{STRICT} %-30.-30t %-56([%boldWhite(%logger{40})]) %highlight(%p): %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="jline" />
</root>
</configuration>

View File

@ -6,13 +6,23 @@ guava = "33.0.0-jre"
junit-jupiter = "5.10.2"
javacord = "3.8.0"
dotenv = "3.0.0"
log4j = "2.24.1"
logback = "1.5.15"
slf4j = "2.0.16"
log4jtoslf4j = "2.24.3"
jultoslf4j = "2.0.16"
jline = "3.26.3"
marinara = "1.0.0-RC1-9d88ca9"
[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
javacord = { module = "org.javacord:javacord", version.ref = "javacord" }
dotenv = { module = "io.github.cdimascio:dotenv-java", version.ref = "dotenv"}
log4j = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j"}
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback"}
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j"}
log4jtoslf4j = { module = "org.apache.logging.log4j:log4j-to-slf4j", version.ref = "log4jtoslf4j"}
jultoslf4j = { module = "org.slf4j:jul-to-slf4j", version.ref = "jultoslf4j"}
jline = { module = "org.jline:jline", version.ref = "jline"}
marinaralib = { module = "net.tomatentum.Marinara:lib-dev", version.ref = "marinara"}
marinarajavacord = { module = "net.tomatentum.Marinara:wrapper-javacord-dev", version.ref = "marinara"}