add logback logging and compatability. Also test Ping command.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
}
|
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
11
app/src/main/resources/logback.xml
Normal file
11
app/src/main/resources/logback.xml
Normal 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>
|
Reference in New Issue
Block a user