blob: 6fdaf1ee617592c583cce23a7c987a4602f309e2 (
plain) (
blame)
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
|
import commands.CommandManager;
import commands.StatusHandler;
import common.OrgChannelTuple;
import fileutils.FileDataProcessor;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import javax.security.auth.login.LoginException;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main extends ListenerAdapter{
private int REFRESH_INTERVAL = 15;
private JDA jda;
private StatusHandler statusHandler;
private JDABuilder jdaBuilder;
private FileDataProcessor fileDataProcessor;
private CommandManager commandManager;
public void initializeBot(){
fileDataProcessor = new FileDataProcessor();
commandManager = new CommandManager(fileDataProcessor.readCredential("holodexAPIKey"));
jdaBuilder = JDABuilder.createDefault(fileDataProcessor.readCredential("discordToken"));
jdaBuilder.addEventListeners(commandManager);
jdaBuilder.addEventListeners(this);
try {
jda = jdaBuilder.build();
}
catch (LoginException e) {
System.out.println("Unable to login with the provided token. Please check your token and try again.");
throw new RuntimeException(e);
}
}
public void initializeAutoRefresh(){
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
ses.scheduleAtFixedRate(() -> {
try {
System.out.println("Refreshing upcoming channels");
List<OrgChannelTuple> refreshChannels = fileDataProcessor.getRefreshChannels();
if (refreshChannels.size() == 0) {
System.out.println("No channels to refresh");
return;
}
for (OrgChannelTuple orgChannelTuple : refreshChannels) {
System.out.println("Refreshing " + orgChannelTuple.getType() + " " + orgChannelTuple.getName());
jda.getTextChannelById(orgChannelTuple.getDiscordChannelId()).purgeMessages(
jda.getTextChannelById(orgChannelTuple.getDiscordChannelId()).getIterableHistory().complete());
List<MessageEmbed> messageEmbeds = commandManager.updateUpcomingChannel(orgChannelTuple.getName(), orgChannelTuple.getType());
if (messageEmbeds.size() == 0) {
continue;
}
for (MessageEmbed messageEmbed : messageEmbeds) {
jda.getTextChannelById(orgChannelTuple.getDiscordChannelId()).sendMessageEmbeds(messageEmbed).queue();
}
}
}
catch(NullPointerException ex){
System.out.println(ex);
System.out.println("Channel is empty. Skipping refresh there");
}
catch (Exception e) {
System.out.println("Error occurred while refreshing upcoming channels");
e.printStackTrace();
}
}, 0, REFRESH_INTERVAL, TimeUnit.MINUTES);
}
@Override
public void onMessageReceived(MessageReceivedEvent e) {
JDA jda = e.getJDA();
Message message = e.getMessage();
String msg = message.getContentDisplay();
}
@Override
public void onReady(net.dv8tion.jda.api.events.ReadyEvent event) {
System.out.println("Logged in as " + event.getJDA().getSelfUser().getAsTag());
statusHandler = new StatusHandler(jda);
statusHandler.updateSlashCommands();
initializeAutoRefresh();
System.out.println("Bot is ready!");
}
public static void main(String args[]) {
Main main = new Main();
main.initializeBot();
}
}
|