Skip to content

Commit 7965621

Browse files
committed
Updated to jda 5.0.0-beta.13, refactored code, removed mongodb
1 parent 50fe02c commit 7965621

21 files changed

Lines changed: 315 additions & 801 deletions

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
# Nyjava
22
Nyjava is a discord bot base that supports 🎉 Discord Slash Commands! 🎉
3-
### I'll see where I go with this, I might update it, I might not. Time will tell.
4-
For now though, it's free to use for everyone, and if you find a bug, feel free to open an issue on the GitHub [issue page](https://github.com/Nyvil/Nyjava/issues).
3+
### I'll update it when major changes to JDA are made, probably.
4+
When using the bot, please be mindful of the [License](https://github.com/Nyvil/Nyjava/blob/stable/LICENSE).
5+
If you happen to find any bugs, please report them on the [issue page](https://github.com/Nyvil/Nyjava/issues).
56

67
# Getting started
7-
## Nyjava requires a MongoDB database to work. [MongoDB docs](https://docs.mongodb.com/manual/tutorial/getting-started/)
8-
9-
After compiling and running the jar for the first time, it will ask you for your bots token and it will create a folder named "Bot" located in the same path</br> as the jar.
10-
Open said folder, click on MongoDB.json and enter your database credentials. Afterwards restart the bot.
11-
12-
## Important:
13-
if you created the MongoDB user not in the admin database, go to the DatabaseConnection class and change the authSource string.
8+
When running the bot, pass in the token of your bot as an argument.
9+
```
10+
java -jar Nyjava.jar <token>
11+
```
1412

1513
# Bugs
1614
-/-

pom.xml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<groupId>org.apache.maven.plugins</groupId>
1010
<artifactId>maven-compiler-plugin</artifactId>
1111
<configuration>
12-
<source>9</source>
13-
<target>9</target>
12+
<source>16</source>
13+
<target>16</target>
1414
</configuration>
1515
</plugin>
1616
</plugins>
@@ -35,7 +35,7 @@
3535
<dependency>
3636
<groupId>net.dv8tion</groupId>
3737
<artifactId>JDA</artifactId>
38-
<version>4.3.0_277</version>
38+
<version>5.0.0-beta.13</version>
3939
</dependency>
4040
<!-- https://mvnrepository.com/artifact/com.github.oshi/oshi-core -->
4141
<dependency>
@@ -46,7 +46,7 @@
4646
<dependency>
4747
<groupId>com.google.code.gson</groupId>
4848
<artifactId>gson</artifactId>
49-
<version>2.8.9</version>
49+
<version>2.10.1</version>
5050
</dependency>
5151
<dependency>
5252
<groupId>org.mongodb</groupId>
@@ -60,11 +60,6 @@
6060
<version>3.12.0</version>
6161
</dependency>
6262
<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-async -->
63-
<dependency>
64-
<groupId>org.mongodb</groupId>
65-
<artifactId>mongodb-driver-async</artifactId>
66-
<version>3.12.8</version>
67-
</dependency>
6863
<dependency>
6964
<groupId>org.slf4j</groupId>
7065
<artifactId>slf4j-api</artifactId>
@@ -75,12 +70,6 @@
7570
<artifactId>slf4j-simple</artifactId>
7671
<version>LATEST</version>
7772
</dependency>
78-
<dependency>
79-
<groupId>org.projectlombok</groupId>
80-
<artifactId>lombok</artifactId>
81-
<version>LATEST</version>
82-
<scope>provided</scope>
83-
</dependency>
8473
</dependencies>
8574
<properties>
8675
<maven.compiler.source>8</maven.compiler.source>

src/main/java/uk/co/nyvil/Bot.java

Lines changed: 15 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,39 @@
11
package uk.co.nyvil;
22

3-
import com.google.gson.JsonObject;
4-
import com.mongodb.BasicDBObject;
5-
import com.mongodb.DBCollection;
6-
import com.mongodb.DBObject;
7-
import lombok.Getter;
8-
import lombok.Setter;
9-
import net.dv8tion.jda.api.EmbedBuilder;
10-
import net.dv8tion.jda.api.MessageBuilder;
11-
import net.dv8tion.jda.api.Permission;
12-
import net.dv8tion.jda.api.entities.*;
13-
import uk.co.nyvil.bot.BotConnection;
14-
import uk.co.nyvil.bot.commands.manager.CommandHandler;
15-
import uk.co.nyvil.database.DatabaseConnection;
16-
import uk.co.nyvil.database.utils.DatabaseUtils;
17-
import uk.co.nyvil.utils.FileUtils;
18-
import uk.co.nyvil.utils.MessageUtils;
19-
import uk.co.nyvil.utils.PermissionUtils;
20-
21-
import java.io.File;
22-
import java.net.URISyntaxException;
23-
import java.nio.file.Path;
24-
import java.text.DateFormat;
25-
import java.text.ParseException;
26-
import java.time.Instant;
27-
import java.time.LocalDate;
28-
import java.time.ZoneId;
29-
import java.util.ArrayList;
30-
import java.util.Date;
31-
import java.util.List;
32-
import java.util.Scanner;
33-
import java.util.concurrent.*;
3+
import uk.co.nyvil.bot.JdaService;
4+
import uk.co.nyvil.bot.commands.manager.CommandRegistry;
345

356
/**
367
* Created by Nyvil on 11/06/2021 at 21:16
378
* in project Discord Base
389
*/
3910

40-
@Getter
41-
@Setter
4211
public class Bot {
4312

44-
@Getter
45-
private static List<String> log = new ArrayList<>();
46-
@Getter
47-
private CommandHandler commandHandler;
48-
@Getter
49-
private DatabaseConnection databaseConnection;
50-
@Getter
51-
private BotConnection botConnection;
52-
@Getter
53-
private DatabaseUtils databaseUtils;
54-
@Getter
55-
private String db, dbUser, dbPwd, dbPort, host, token;
56-
@Getter
57-
private boolean dbStart;
58-
@Getter
5913
private static final Bot instance = new Bot();
14+
private static JdaService jdaService;
15+
private static CommandRegistry commandRegistry;
6016

6117
public static void main(String[] args) {
62-
try {
63-
getInstance().createDir();
64-
FileUtils.getToken();
65-
FileUtils.startup();
66-
} catch (URISyntaxException e) {
67-
e.printStackTrace();
18+
if(args.length < 1) {
19+
System.out.println("Enter your bot's token!");
20+
System.exit(1);
6821
}
6922

70-
getInstance().start();
71-
getInstance().setStaff();
72-
}
23+
jdaService = new JdaService(args[0]);
24+
commandRegistry = new CommandRegistry();
7325

74-
/**
75-
* Method to initialise the instance of the bot and the instances for the handlers
76-
*/
77-
private void start() {
78-
if (this.getToken() == null) {
79-
try (Scanner scanner = new Scanner(System.in)) {
80-
System.out.println("Please enter your bot's token:");
81-
String token = scanner.nextLine();
82-
try {
83-
FileUtils.setToken(token);
84-
botConnection = new BotConnection();
85-
commandHandler = new CommandHandler();
86-
if (getInstance().dbStart) {
87-
databaseConnection = new DatabaseConnection();
88-
} else {
89-
System.out.println("Couldn't connect to database, review the database file and restart.");
90-
}
91-
} catch (URISyntaxException e) {
92-
e.printStackTrace();
93-
}
94-
}
95-
} else {
96-
botConnection = new BotConnection();
97-
commandHandler = new CommandHandler();
98-
if (getInstance().dbStart) {
99-
databaseConnection = new DatabaseConnection();
100-
} else {
101-
System.out.println("Couldn't connect to database, review the database file and restart.");
102-
}
103-
}
10426
}
10527

106-
/**
107-
* File to get the path of the jar
108-
*
109-
* @return the path where the jar is located
110-
* @throws URISyntaxException if URI can't be parsed, so basically if the directory isn't found
111-
*/
112-
public Path getDirPath() throws URISyntaxException {
113-
return new File(Bot.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile().toPath();
28+
public static Bot getInstance() {
29+
return instance;
11430
}
11531

116-
/**
117-
* Creates the root dir of the bot
118-
*
119-
* @throws URISyntaxException {@link #getDirPath()} for reason
120-
*/
121-
private void createDir() throws URISyntaxException {
122-
final File rootDir = new File(getDirPath() + File.separator + "Bot");
123-
if (!rootDir.exists()) {
124-
rootDir.mkdir();
125-
}
32+
public JdaService getJdaService() {
33+
return jdaService;
12634
}
12735

128-
private void setStaff() {
129-
DatabaseUtils.getCollection(DatabaseUtils.getDatabase(db), "staff_team").find().forEach(p -> {
130-
JsonObject obj = DatabaseUtils.getAsJsonObject("_id", (String) p.get("_id"), DatabaseUtils.getCollection(DatabaseUtils.getDatabase(db), "staff_team"));
131-
PermissionUtils.staff.add(obj != null ? obj.get("_id").getAsLong() : 0);
132-
User user = BotConnection.getJda().retrieveUserById(obj.get("_id").getAsString()).complete();
133-
System.out.println("Added " + obj.get("_id").getAsString() + "(" + user.getName() + ") as staff!");
134-
});
36+
public CommandRegistry getCommandRegistry() {
37+
return commandRegistry;
13538
}
13639
}

src/main/java/uk/co/nyvil/bot/BotConnection.java

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package uk.co.nyvil.bot;
2+
3+
import net.dv8tion.jda.api.JDA;
4+
import net.dv8tion.jda.api.JDABuilder;
5+
import net.dv8tion.jda.api.requests.GatewayIntent;
6+
import uk.co.nyvil.bot.commands.manager.SlashCommandHandler;
7+
import uk.co.nyvil.bot.listeners.ReadyListener;
8+
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import java.util.Optional;
13+
14+
public class JdaService {
15+
16+
17+
private final String token;
18+
19+
private final List<GatewayIntent> gatewayIntents = new ArrayList<>();
20+
private static JDA jda;
21+
22+
public JdaService(String token) {
23+
this.token = token;
24+
25+
start(Collections.emptyList());
26+
}
27+
28+
29+
/**
30+
* @param gatewayIntents the gatewayIntents to set - https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/requests/GatewayIntent.html. If you want none, just pass an empty List -> Collections.emptyList()
31+
*
32+
*/
33+
private void start(List<GatewayIntent> gatewayIntents) {
34+
try {
35+
jda = JDABuilder.createDefault(token)
36+
.enableIntents(gatewayIntents)
37+
.addEventListeners(new SlashCommandHandler(), new ReadyListener())
38+
.build();
39+
40+
jda.awaitReady();
41+
} catch (InterruptedException e) {
42+
e.printStackTrace();
43+
}
44+
45+
}
46+
47+
public Optional<JDA> getJda() {
48+
return Optional.ofNullable(jda);
49+
}
50+
51+
52+
53+
}

0 commit comments

Comments
 (0)