Compare commits

...

6 Commits

Author SHA1 Message Date
3473657fbb add Logger config and basic connect logic 2024-10-10 21:19:36 +02:00
373a6b4279 add Config Wrapper for .env 2024-10-10 21:18:29 +02:00
942719d19c add various ignored files 2024-10-10 21:18:10 +02:00
fd7fa1e078 add javacord, dotenv and log4j dependencies 2024-10-10 21:17:52 +02:00
5532dc1769 add .env.example 2024-10-10 21:16:49 +02:00
879ee55ac4 update used Library 2024-10-10 21:16:37 +02:00
7 changed files with 102 additions and 5 deletions

2
.env.example Normal file
View File

@ -0,0 +1,2 @@
ENVIRONMENT=Production
TOKEN=<YOUR BOT TOKEN>

2
.gitignore vendored
View File

@ -3,3 +3,5 @@
# Ignore Gradle build output directory # Ignore Gradle build output directory
build build
.vscode
.env

View File

@ -1,3 +1,3 @@
# TomatenMusic3 # TomatenMusic3
A simple Discord Music Bot written in Java with Discord4J A simple Discord Music Bot written in Java with Javacord

View File

@ -8,6 +8,7 @@
plugins { plugins {
// Apply the application plugin to add support for building a CLI application in Java. // Apply the application plugin to add support for building a CLI application in Java.
application application
id("com.gradleup.shadow") version "8.3.3"
} }
repositories { repositories {
@ -23,6 +24,9 @@ dependencies {
// This dependency is used by the application. // This dependency is used by the application.
implementation(libs.guava) implementation(libs.guava)
implementation(libs.javacord)
implementation(libs.dotenv)
implementation(libs.log4j)
} }
// Apply a specific Java toolchain to ease working on different environments. // Apply a specific Java toolchain to ease working on different environments.
@ -37,6 +41,13 @@ application {
mainClass = "net.tomatentum.tomatenmusic3.App" mainClass = "net.tomatentum.tomatenmusic3.App"
} }
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = application.mainClass
}
}
tasks.named<Test>("test") { tasks.named<Test>("test") {
// Use JUnit Platform for unit tests. // Use JUnit Platform for unit tests.
useJUnitPlatform() useJUnitPlatform()

View File

@ -1,10 +1,64 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package net.tomatentum.tomatenmusic3; 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 org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.intent.Intent;
import io.github.cdimascio.dotenv.Dotenv;
public class App { 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 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);
}
public void connect() {
client = new DiscordApiBuilder()
.setToken(config.token())
.addIntents(Intent.GUILD_VOICE_STATES)
.login().join();
logger.log(Level.INFO, "connected as {}", client.getYourself().getName());
}
} }

View File

@ -0,0 +1,20 @@
package net.tomatentum.tomatenmusic3;
import io.github.cdimascio.dotenv.Dotenv;
public class Config {
private Dotenv env;
public Config(Dotenv env) {
this.env = env;
}
public String token() {
return env.get("TOKEN");
}
public boolean isDevelopment() {
return env.get("ENVIRONMENT").equals("Development");
}
}

View File

@ -4,7 +4,15 @@
[versions] [versions]
guava = "33.0.0-jre" guava = "33.0.0-jre"
junit-jupiter = "5.10.2" junit-jupiter = "5.10.2"
javacord = "3.8.0"
dotenv = "3.0.0"
log4j = "2.24.1"
[libraries] [libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" } guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } 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"}