-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaBotMain.java
More file actions
123 lines (105 loc) · 6.14 KB
/
JavaBotMain.java
File metadata and controls
123 lines (105 loc) · 6.14 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
114
115
116
117
118
119
120
121
122
123
package dev.jacrispys.JavaBot;
import com.github.topi314.lavasrc.applemusic.AppleMusicSourceManager;
import com.github.topi314.lavasrc.spotify.SpotifySourceManager;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager;
import dev.jacrispys.JavaBot.api.analytics.utils.ListenTimeTracker;
import dev.jacrispys.JavaBot.api.libs.utils.JavalinManager;
import dev.jacrispys.JavaBot.audio.AudioPlayerButtons;
import dev.jacrispys.JavaBot.audio.GenerateGenrePlaylist;
import dev.jacrispys.JavaBot.audio.InactivityTimer;
import dev.jacrispys.JavaBot.commands.ComplaintCommand;
import dev.jacrispys.JavaBot.commands.EmbedCLI;
import dev.jacrispys.JavaBot.commands.RegisterGuildCommand;
import dev.jacrispys.JavaBot.commands.UnclassifiedSlashCommands;
import dev.jacrispys.JavaBot.commands.audio.GenericMusicCommands;
import dev.jacrispys.JavaBot.commands.audio.SlashMusicCommands;
import dev.jacrispys.JavaBot.commands.debug.GenericDebugCommands;
import dev.jacrispys.JavaBot.commands.message.DefaultPrivateMessageResponse;
import dev.jacrispys.JavaBot.events.BotStartup;
import dev.jacrispys.JavaBot.utils.SecretData;
import dev.jacrispys.JavaBot.utils.SpotifyManager;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.naming.ConfigurationException;
import java.io.IOException;
/**
* The core class that establishes a connection with {@link JDA} and discord.
* <br> Also initializes all other utility/necessary classes.
*/
public class JavaBotMain {
// TODO: 10/6/2023 Add unit testing to most classes
private static final Logger logger = LoggerFactory.getLogger(JavaBotMain.class);
private static final String className = JavaBotMain.class.getSimpleName();
public static AudioPlayerManager audioManager;
/**
* The main method of the application.
* <br>
* <br> Starts by initializing ENV login data via {@link SecretData#initLoginInfo()}
* <br> Then creates a connection to {@link JDA} through the {@link JDABuilder}
* <br> It next registers Audio source managers for multi-platform song searching through {@link AudioSourceManagers}
* <br> Finally we add all event listeners and register a {@link io.javalin.Javalin} server to handle API requests (WIP)
* <br> <br>
* @throws ConfigurationException if any of the token fields are left blank in the config file
* @throws IOException if any errors occur whilst obtaining data from the YAML file
*/
public static void main(String[] args) throws ConfigurationException, IOException {
logger.info("{} - Installing & loading data Files.", className);
SecretData.initLoginInfo();
String devToken = SecretData.getToken(false);
String botToken = SecretData.getToken();
if (devToken == null || botToken == null) {
throw new ConfigurationException("Config file MUST contain VALID values for all fields!");
}
logger.info("{} - Logging into bot & discord servers...", className);
JDA jda = JDABuilder.createDefault(devToken)
.setChunkingFilter(ChunkingFilter.ALL)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES, GatewayIntent.MESSAGE_CONTENT)
.enableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE)
.build();
logger.info("{} - Login Successful!", className);
logger.info("{} - Connecting to spotify source manager...", className);
audioManager = new DefaultAudioPlayerManager();
AudioSourceManagers.registerLocalSource(audioManager);
String clientId = SecretData.getSpotifyId();
String clientSecret = SecretData.getSpotifySecret();
String countryCode = "US";
YoutubeAudioSourceManager ytSource = new YoutubeAudioSourceManager(true, SecretData.getYtEmail(), SecretData.getYtPass());
audioManager.registerSourceManager(ytSource);
audioManager.registerSourceManager(new SpotifySourceManager(null, clientId, clientSecret, countryCode, audioManager));
audioManager.registerSourceManager(new AppleMusicSourceManager(null, SecretData.getAppleToken(), "us", audioManager));
AudioSourceManagers.registerRemoteSources(audioManager);
logger.info("{} - Successfully connected to spotify!", className);
logger.info("{} - Connecting to personal spotify API...", className);
SpotifyManager.getInstance();
logger.info("{} - Connected to personal API!", className);
String version = JavaBotMain.class.getPackage().getImplementationVersion();
jda.getPresence().setActivity(Activity.streaming(version + " Woo!", "https://www.twitch.tv/jacrispyslive"));
logger.info("{} - Starting event listeners...", className);
jda.addEventListener(new SlashMusicCommands());
jda.addEventListener(new DefaultPrivateMessageResponse());
jda.addEventListener(new ComplaintCommand());
jda.addEventListener(new RegisterGuildCommand());
jda.addEventListener(new BotStartup());
jda.addEventListener(new GenericMusicCommands());
jda.addEventListener(new AudioPlayerButtons());
jda.addEventListener(new InactivityTimer());
jda.addEventListener(new GenericDebugCommands());
jda.addEventListener(new UnclassifiedSlashCommands(jda));
jda.addEventListener(EmbedCLI.getInstance());
jda.addEventListener(new GenerateGenrePlaylist());
jda.addEventListener(new ListenTimeTracker(jda));
logger.info("{} - Successfully added [" + jda.getRegisteredListeners().size() + "] event listeners!", className);
new JavalinManager(7070);
}
}