Add logging to discord bot
added 'drowned' event handler in discord bot too
This commit is contained in:
BIN
MCbot_bryce.jar
BIN
MCbot_bryce.jar
Binary file not shown.
7
pom.xml
7
pom.xml
@@ -67,6 +67,13 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- Logging-->
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.23.1</version> <!-- Use the latest STABLE version -->
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Add other dependencies as needed -->
|
||||
</dependencies>
|
||||
|
@@ -3,14 +3,20 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
||||
public class disMCbot extends ListenerAdapter {
|
||||
|
||||
private TextChannel updateChannel; // Define the channel where updates will be posted
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(disMCbot.class);
|
||||
|
||||
public static void main(String[] args) throws LoginException {
|
||||
logger.info("Starting Discord Comms...");
|
||||
JDABuilder builder = JDABuilder.createDefault("MTIyNjY1NTgxODU0NDMxNjQxNw.GALTlL.vPRJyKTEJNqN1Snfj5G-_2306lBKFdOYB1eDXE");
|
||||
builder.addEventListeners(new disMCbot());
|
||||
builder.build();
|
||||
@@ -23,16 +29,31 @@ public class disMCbot extends ListenerAdapter {
|
||||
String message = event.getMessage().getContentRaw();
|
||||
// Check for conditions or events triggering updates
|
||||
if (message.contains("Player1 died")) {
|
||||
// Log It
|
||||
logger.info("Player1 died - detected");
|
||||
// Post update to the specified channel
|
||||
if (updateChannel != null) {
|
||||
updateChannel.sendMessage("Player1 has died in Minecraft!").queue();
|
||||
// Log It
|
||||
logger.info("Message Posted in Discord.");
|
||||
}
|
||||
} else if (message.contains("Player1 Exp")) {
|
||||
// Log It
|
||||
logger.info("Player1 Exp Gained");
|
||||
// Post another type of update
|
||||
if (updateChannel != null) {
|
||||
updateChannel.sendMessage("Player1 gained experience in Minecraft!").queue();
|
||||
// Log It
|
||||
logger.info("Message sent to Discord.");
|
||||
}
|
||||
}
|
||||
} else if (message.contains("drowned")) {
|
||||
// Log It
|
||||
logger.info("Player1 has drowned");
|
||||
// Post another type of update
|
||||
if (updateChannel != null) {
|
||||
updateChannel.sendMessage("Player1 has drowned - they got thirsty!").queue();
|
||||
// Log It
|
||||
logger.info("Message sent to Discord.");
|
||||
}
|
||||
|
||||
public void setUpdateChannel(TextChannel channel) {
|
||||
|
13
src/main/resources/log4j2.xml
Normal file
13
src/main/resources/log4j2.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<File name="File" fileName="logs/MCbot.log">
|
||||
<PatternLayout pattern="%d{dd-MM-yyyy HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
|
||||
</File>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="File" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
13
target/classes/log4j2.xml
Normal file
13
target/classes/log4j2.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<File name="File" fileName="logs/MCbot.log">
|
||||
<PatternLayout pattern="%d{dd-MM-yyyy HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
|
||||
</File>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="File" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
Reference in New Issue
Block a user