Skip to content

Commit bf97e76

Browse files
refactoring and added panel upon first startup to enter discord bot token
1 parent d342af3 commit bf97e76

File tree

5 files changed

+105
-19
lines changed

5 files changed

+105
-19
lines changed

build.gradle

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
plugins {
22
id 'java'
3+
id 'com.github.johnrengelman.shadow' version '4.0.2'
34
}
45

56
group 'org.example'
6-
version '1.0'
7+
version '1.3'
78

89
repositories {
910
mavenCentral()
@@ -16,11 +17,19 @@ repositories {
1617
dependencies {
1718
//testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
1819
//testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
19-
implementation("net.dv8tion:JDA:4.2.1_276")
20+
compile("net.dv8tion:JDA:4.3.0_298")
2021
compile group: 'commons-net', name: 'commons-net', version: '3.3'
2122
compile fileTree(include: ['*.jar'], dir: 'libs')
2223
}
2324

2425
test {
2526
//useJUnitPlatform()
27+
}
28+
29+
jar {
30+
manifest {
31+
attributes(
32+
'Main-Class' : "dain.Start",
33+
)
34+
}
2635
}

src/main/java/dain/Bot.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package dain;
22

3-
import dain.MinecraftChatBridge;
4-
import dain.Settings;
53
import dain.events.DiscordMessageReceiver;
6-
import net.dv8tion.jda.api.JDA;
7-
import net.dv8tion.jda.api.JDABuilder;
4+
import net.dv8tion.jda.api.*;
85
import net.dv8tion.jda.api.entities.Activity;
96
import net.dv8tion.jda.api.entities.TextChannel;
10-
117
import javax.security.auth.login.LoginException;
12-
import javax.swing.*;
13-
import java.awt.*;
14-
import java.awt.event.ActionEvent;
15-
import java.awt.event.ActionListener;
168

179
public class Bot {
1810

1911
private JDA jda;
12+
private TokenEntryFrame tokenEntryFrame = null;
2013

2114
public Bot() {
2215

2316
Settings.initialize();
2417

25-
try {
26-
jda = JDABuilder.createDefault(Settings.DISCORD_TOKEN).setActivity(Activity.listening(">help")).build();
27-
} catch (LoginException e) {
28-
e.printStackTrace();
18+
while (jda == null) {
19+
jda = loginJDAClient();
20+
try {
21+
Thread.sleep(5000);
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
2925
}
3026

3127
jda.addEventListener(new DiscordMessageReceiver());
@@ -73,4 +69,15 @@ public void startChatBridge() {
7369
public JDA getJda() {
7470
return jda;
7571
}
72+
73+
private JDA loginJDAClient() {
74+
try {
75+
return JDABuilder.createDefault(Settings.DISCORD_TOKEN).setActivity(Activity.listening(Settings.COMMAND_PREFIX + "help")).build();
76+
} catch (LoginException e) {
77+
if (tokenEntryFrame == null) {
78+
tokenEntryFrame = new TokenEntryFrame();
79+
}
80+
return null;
81+
}
82+
}
7683
}

src/main/java/dain/SettingsFrame.java

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

3-
import com.apple.laf.AquaButtonBorder;
4-
53
import javax.swing.*;
64
import javax.swing.border.LineBorder;
75
import java.awt.*;
@@ -91,7 +89,10 @@ public SettingsFrame() {
9189
saveButton.addActionListener(new ActionListener() {
9290
@Override
9391
public void actionPerformed(ActionEvent e) {
94-
if (e.getSource().equals(saveButton)) saveValues();
92+
if (e.getSource().equals(saveButton)) {
93+
saveValues();
94+
SettingsFrame.this.dispose();
95+
}
9596
}
9697
});
9798
bottomPanel.add(saveButton);

src/main/java/dain/Start.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public class Start {
44

55
public static void main(String[] args) {
66

7-
new Bot();//.startChatBridge();
7+
new Bot().startChatBridge();
88
}
99
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package dain;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
8+
public class TokenEntryFrame extends JFrame {
9+
10+
11+
public static final int WIDTH_FRAME = 800;
12+
public static final int HEIGHT_FRAME = 200;
13+
14+
public TokenEntryFrame() {
15+
16+
JLabel onlyLabel = new JLabel("Please enter the token for your Discord bot application:");
17+
onlyLabel.setBounds(20, 20, 360, 20);
18+
this.add(onlyLabel);
19+
20+
JTextField onlyTextField = new JTextField("");
21+
onlyTextField.setBounds(20, 40, 720, 20);
22+
this.add(onlyTextField);
23+
24+
JButton saveButton = new JButton("Save");
25+
saveButton.setBounds(20, 80, 80, 20);
26+
this.add(saveButton);
27+
saveButton.addActionListener(new ActionListener() {
28+
@Override
29+
public void actionPerformed(ActionEvent e) {
30+
if (e.getSource().equals(saveButton)) {
31+
Settings.DISCORD_TOKEN = onlyTextField.getText();
32+
Settings.writeToFile();
33+
34+
for (int i = 0; i < 8; i++) { /* we want to wait some time before closing the window
35+
because the main window doesn't pop up immediately
36+
and the user may become confused */
37+
sleap(250);
38+
TokenEntryFrame.this.setTitle("Please wait."); // give the user feedback in the title bar
39+
sleap(250);
40+
TokenEntryFrame.this.setTitle("Please wait.."); // so that they know their click had an effect
41+
sleap(250);
42+
TokenEntryFrame.this.setTitle("Please wait...");
43+
sleap(250);
44+
TokenEntryFrame.this.setTitle("Please wait");
45+
}
46+
47+
TokenEntryFrame.this.dispose();
48+
}
49+
}
50+
});
51+
52+
this.setLayout(null);
53+
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
54+
//this.setIconImage(logoIcon.getImage());
55+
this.setTitle("Discord Bot Token Entry");
56+
this.setType(Window.Type.POPUP);
57+
this.setLocation(320, 120);
58+
this.setSize(WIDTH_FRAME, HEIGHT_FRAME);
59+
this.setVisible(true);
60+
}
61+
62+
private void sleap(int ms) {
63+
try {
64+
Thread.sleep(ms);
65+
} catch (InterruptedException e) {
66+
e.printStackTrace();
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)