|
1 | 1 | package uk.co.nyvil; |
2 | 2 |
|
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; |
34 | 5 |
|
35 | 6 | /** |
36 | 7 | * Created by Nyvil on 11/06/2021 at 21:16 |
37 | 8 | * in project Discord Base |
38 | 9 | */ |
39 | 10 |
|
40 | | -@Getter |
41 | | -@Setter |
42 | 11 | public class Bot { |
43 | 12 |
|
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 |
59 | 13 | private static final Bot instance = new Bot(); |
| 14 | + private static JdaService jdaService; |
| 15 | + private static CommandRegistry commandRegistry; |
60 | 16 |
|
61 | 17 | 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); |
68 | 21 | } |
69 | 22 |
|
70 | | - getInstance().start(); |
71 | | - getInstance().setStaff(); |
72 | | - } |
| 23 | + jdaService = new JdaService(args[0]); |
| 24 | + commandRegistry = new CommandRegistry(); |
73 | 25 |
|
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 | | - } |
104 | 26 | } |
105 | 27 |
|
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; |
114 | 30 | } |
115 | 31 |
|
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; |
126 | 34 | } |
127 | 35 |
|
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; |
135 | 38 | } |
136 | 39 | } |
0 commit comments