-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPapiBot.java
More file actions
113 lines (93 loc) · 3.75 KB
/
PapiBot.java
File metadata and controls
113 lines (93 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package at.helpch.papibot;
import at.helpch.papibot.core.framework.Command;
import at.helpch.papibot.core.handlers.EventHandler;
import at.helpch.papibot.core.handlers.GEvent;
import at.helpch.papibot.core.handlers.chat.CommandHandler;
import at.helpch.papibot.core.objects.enums.Registerables;
import at.helpch.papibot.core.objects.tasks.GRunnable;
import at.helpch.papibot.core.objects.tasks.Task;
import at.helpch.papibot.core.storage.file.GFile;
import at.helpch.papibot.core.storage.mysql.MySQLInitializer;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import lombok.Getter;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import org.reflections.Reflections;
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Stream;
import static at.helpch.papibot.core.objects.enums.Registerables.*;
// ------------------------------
// Copyright (c) PiggyPiglet 2020
// https://www.piggypiglet.me
// ------------------------------
@Singleton
public final class PapiBot {
@Getter private Injector injector;
@Getter private JDA jda;
private static final Reflections REFLECTIONS = new Reflections("at.helpch.papibot");
private static final BlockingQueue<GRunnable> QUEUE = new LinkedBlockingQueue<>();
@Inject private GFile gFile;
@Inject private MySQLInitializer mySQLInitializer;
@Inject private EventHandler eventHandler;
@Inject private CommandHandler commandHandler;
void start(Injector injector) throws Exception {
this.injector = injector;
Stream.of(
FILES, EVENTS, COMMANDS, BOT, MYSQL, CONSOLE
).forEach(PapiBot.this::register);
//noinspection InfiniteLoopStatement
while (true) QUEUE.take().run();
}
/**
* Initialize individual components (registerables) in a controllable; clean way.
* @param registerable The registerable to be registered.
*/
public void register(Registerables registerable) {
switch (registerable) {
case FILES:
Stream.of("config.json", "schema.sql").forEach(i -> gFile.make(i, "./" + i, "/" + i));
break;
case EVENTS:
REFLECTIONS.getSubTypesOf(GEvent.class).stream().map(injector::getInstance).forEach(eventHandler.getEvents()::add);
break;
case COMMANDS:
REFLECTIONS.getSubTypesOf(Command.class).stream().map(injector::getInstance).forEach(commandHandler.getCommands()::add);
break;
case BOT:
try {
jda = new JDABuilder(gFile.getFileConfiguration("config").getString("token"))
.setActivity(Activity.watching("the eCloud"))
.addEventListeners(eventHandler)
.build();
} catch (Exception e) {
e.printStackTrace();
}
break;
case MYSQL:
mySQLInitializer.connect();
break;
case CONSOLE:
Task.async(r -> {
Scanner input = new Scanner(System.in);
while (true) {
switch (input.nextLine().toLowerCase()) {
case "stop": System.exit(0); break;
}
}
}, "Console Command Monitor");
break;
}
}
/**
* Queue a GRunnable to run on the main thread.
* @param gRunnable GRunnable instance.
*/
public void queue(GRunnable gRunnable) {
QUEUE.add(gRunnable);
}
}