Skip to content

Commit 527e02e

Browse files
streamlined logging & updated readme
1 parent bc5540f commit 527e02e

File tree

9 files changed

+45
-41
lines changed

9 files changed

+45
-41
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# What is this?
22
This is a Discord bot written in Java that can link the chat of Minecraft servers with Discord channels.
3-
<img width="1440" alt="Screenshot of the program running on a blank desktop" src="https://user-images.githubusercontent.com/53956237/136717891-a6233941-3776-4acb-9a66-50c6ec680d4d.png">
43

54
# Why should I use this one instead of countless others?
65
Here's what makes this implementation unique:

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'org.example'
7-
version '1.7'
7+
version '1.7.1'
88

99
repositories {
1010
mavenCentral()

src/main/java/dain/Bot.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package dain;
22

3-
import dain.events.DiscordMessageReceiver;
3+
import dain.events.*;
44
import net.dv8tion.jda.api.*;
5-
import net.dv8tion.jda.api.entities.Activity;
6-
import net.dv8tion.jda.api.entities.TextChannel;
7-
import javax.security.auth.login.LoginException;
8-
import java.io.BufferedReader;
9-
import java.io.IOException;
10-
import java.io.InputStreamReader;
11-
import java.util.Locale;
12-
import java.util.Scanner;
5+
import net.dv8tion.jda.api.entities.*;
6+
7+
import javax.security.auth.login.*;
8+
import java.util.*;
139

1410
public class Bot {
1511

@@ -45,7 +41,7 @@ public void startChatBridge() {
4541
TextChannel [] channels = new TextChannel[Settings.DISCORD_CHANNEL_IDS.length];
4642
for(int i = 0; i < Settings.DISCORD_CHANNEL_IDS.length; i++) {
4743
channels[i] = jda.getTextChannelById(Settings.DISCORD_CHANNEL_IDS[i]);
48-
Logger.log("Chat Bridge active.", Logger.LoggingLevel.INFO);
44+
Logger.info("Chat Bridge active.");
4945
channels[i].sendMessage("Chat bridge active.").queue();
5046
}
5147

src/main/java/dain/DiscordBotFrame.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import javax.swing.*;
44
import java.awt.*;
5-
import java.awt.event.ActionListener;
6-
import java.awt.event.ActionEvent;
7-
import java.util.Objects;
5+
import java.util.*;
86

97
public class DiscordBotFrame extends JFrame {
108

@@ -26,11 +24,9 @@ public DiscordBotFrame() {
2624
addComponent(settingsLabel, layout, 1, 0, 1, 1, GridBagConstraints.NONE);
2725

2826
JButton settingsButton = new JButton(); // no text on settings button just gear icon
29-
settingsButton.addActionListener(new ActionListener() {
30-
public void actionPerformed(ActionEvent event) {
31-
if(event.getSource() == settingsButton) {
32-
new SettingsFrame();
33-
}
27+
settingsButton.addActionListener(event -> {
28+
if(event.getSource() == settingsButton) {
29+
new SettingsFrame();
3430
}
3531
});
3632
ImageIcon settingsIcon = new ImageIcon(Objects.requireNonNull(getClass().getClassLoader().getResource("settings-icon.png")));
@@ -47,8 +43,8 @@ public void actionPerformed(ActionEvent event) {
4743
logTextArea.setEnabled(false);
4844
JScrollPane logScrollPane = new JScrollPane(logTextArea);
4945
logScrollPane.setWheelScrollingEnabled(false); // do not allow scrolling with mouse scroll wheel
50-
logScrollPane.setHorizontalScrollBarPolicy(31); // 31 = never show horizontal scroll bar
51-
logScrollPane.setVerticalScrollBarPolicy(21); // 21 = never show vertical scroll bar
46+
logScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
47+
logScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
5248
addComponent(logScrollPane, layout, 0, 3, 2, 1, GridBagConstraints.NONE);
5349
Logger.setTextArea(logTextArea);
5450

@@ -71,7 +67,7 @@ private static void sleap(int ms) {
7167
try {
7268
Thread.sleep(ms);
7369
} catch(InterruptedException exception) {
74-
Logger.log("thread interrupted exception", Logger.LoggingLevel.ERROR);
70+
Logger.error("thread interrupted exception");
7571
}
7672
}
7773

src/main/java/dain/FTPHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void grabFile(String file, int serverId) {
3939
ftpClient.disconnect();
4040

4141
} catch (IOException e) {
42-
Logger.log("I/O Exception during FTP handling", Logger.LoggingLevel.ERROR);
42+
Logger.error("I/O Exception during FTP handling");
4343
}
4444
}
4545
}

src/main/java/dain/Logger.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ public class Logger {
66

77
private static JTextArea textArea;
88

9-
public enum LoggingLevel {
9+
private enum LoggingLevel {
1010
INFO, WARN, ERROR, FATAL
1111
}
1212

1313
public static void setTextArea(JTextArea textAreaIn) {
1414
textArea = textAreaIn;
1515
}
1616

17-
public static void log(String message, LoggingLevel level) {
17+
private static void log(String message, LoggingLevel level) {
1818
String string = "[" + level;
1919
string = string + " @ " + java.time.LocalTime.now() + "]: ";
2020
string = string + message;
@@ -27,4 +27,20 @@ public static void log(String message, LoggingLevel level) {
2727

2828
System.out.print(string);
2929
}
30+
31+
public static void info(String message) {
32+
log(message, LoggingLevel.INFO);
33+
}
34+
35+
public static void warn(String message) {
36+
log(message, LoggingLevel.WARN);
37+
}
38+
39+
public static void error(String message) {
40+
log(message, LoggingLevel.ERROR);
41+
}
42+
43+
public static void fatal(String message) {
44+
log(message, LoggingLevel.FATAL);
45+
}
3046
}

src/main/java/dain/MinecraftChatBridge.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import net.dv8tion.jda.api.entities.TextChannel;
66
import net.kronos.rkon.core.Rcon;
77
import net.kronos.rkon.core.ex.AuthenticationException;
8-
98
import java.io.*;
109
import java.util.*;
1110

@@ -40,7 +39,7 @@ public void run() {
4039
try {
4140
Thread.sleep(100);
4241
} catch (InterruptedException e) {
43-
Logger.log("thread interrupted while sleeping", Logger.LoggingLevel.FATAL);
42+
Logger.fatal("thread interrupted while sleeping");
4443
System.exit(-1);
4544
}
4645
}
@@ -61,7 +60,7 @@ public boolean sendNewMessage() {
6160

6261
scanner.close();
6362
} catch (FileNotFoundException e) {
64-
Logger.log("File not found. Where is latest.log?", Logger.LoggingLevel.ERROR);
63+
Logger.error("File not found. Where is latest.log?");
6564
e.printStackTrace();
6665
}
6766

@@ -79,13 +78,13 @@ public boolean sendNewMessage() {
7978
if (line.matches("Done \\(\\d+\\.\\d*s\\)!.*")) serverInitIsDone = true;
8079
if (!serverInitIsDone) return true;
8180

82-
Logger.log("Message received from Minecraft: " + line, Logger.LoggingLevel.INFO);
81+
Logger.info("Message received from Minecraft: " + line);
8382

8483
String processedLine = processLine(line);
8584

8685
sendMessageAllChannels(processedLine);
8786

88-
Logger.log("Message sent to Discord: " + processedLine, Logger.LoggingLevel.INFO);
87+
Logger.info("Message sent to Discord: " + processedLine);
8988

9089
return true;
9190
}
@@ -195,7 +194,7 @@ public static void sendMessageToMc(Message message) {
195194
for (int id = 0; id < Settings.SERVER_IPS.length; id++) {
196195
sendCommandToServer(command, id);
197196
}
198-
Logger.log("Message sent to Minecraft: " + message, Logger.LoggingLevel.INFO);
197+
Logger.info("Message sent to Minecraft: " + message);
199198
}
200199

201200
public static void sendMessageToServer(String author, String message, int serverId) {

src/main/java/dain/Settings.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package dain;
22

33
import java.io.*;
4-
import java.nio.channels.SeekableByteChannel;
5-
import java.util.Scanner;
4+
import java.util.*;
65

76
public class Settings {
87

@@ -50,7 +49,7 @@ public static void initialize() {
5049
}
5150

5251
if (isLocal) {
53-
Logger.log("Bot is running in local mode. Server IPs & FTP login details will be overridden.", Logger.LoggingLevel.WARN);
52+
Logger.warn("Bot is running in local mode. Server IPs & FTP login details will be overridden.");
5453
SERVER_IPS = new String[] {"127.0.0.1"};
5554
FTP_USERNAMES = new String[0];
5655
FTP_PASSWORDS = new String[0];

src/main/java/dain/events/DiscordMessageReceiver.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99

1010
public class DiscordMessageReceiver extends ListenerAdapter {
1111

12-
private enum Commands { // todo maybe better as an enum?
12+
private enum Commands {
1313
KILL("kill"),
1414
SCORE("score"),
1515
HELP("help");
1616

17-
private String value;
17+
private final String value;
1818

19-
private Commands(String valueIn) {
19+
Commands(String valueIn) {
2020
this.value = valueIn;
2121
}
22-
};
22+
}
2323

2424
@Override
2525
public void onMessageReceived(MessageReceivedEvent event) {
@@ -44,10 +44,9 @@ public void onMessageReceived(MessageReceivedEvent event) {
4444

4545
for (int id = 0; id < Settings.DISCORD_CHANNEL_IDS.length; id++) { // for all Discord channels ids in config
4646
if (event.getChannel() == event.getJDA().getTextChannelById(Settings.DISCORD_CHANNEL_IDS[id])) { // if the channel id from the config matches one of the channel id that the message was sent from //todo need to optimize this
47-
Logger.log("Message received from Discord: " + messageSent, Logger.LoggingLevel.INFO);
47+
Logger.info("Message received from Discord: " + messageSent);
4848
MinecraftChatBridge.sendMessageToMc(event.getMessage()); // send message to mc
4949
}
5050
}
51-
return;
5251
}
5352
}

0 commit comments

Comments
 (0)