-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordConnection.java
200 lines (168 loc) · 7.3 KB
/
DiscordConnection.java
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.denizenscript.ddiscordbot;
import com.denizenscript.ddiscordbot.events.*;
import com.denizenscript.denizencore.flags.SavableMapFlagTracker;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent;
import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;
import net.dv8tion.jda.api.events.thread.ThreadHiddenEvent;
import net.dv8tion.jda.api.events.thread.ThreadRevealedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.bukkit.Bukkit;
import javax.annotation.Nonnull;
import java.util.function.Consumer;
public class DiscordConnection extends ListenerAdapter {
public String botID;
public JDA client;
public SavableMapFlagTracker flags;
public CacheHelper cache = new CacheHelper();
public Message getMessage(long channel, long message) {
Message result = cache.getMessage(channel, message);
if (result != null) {
return result;
}
if (!DenizenDiscordBot.allowMessageRetrieval) {
return null;
}
Channel chan = getChannel(channel);
if (!(chan instanceof MessageChannel)) {
return null;
}
return ((MessageChannel) chan).retrieveMessageById(message).complete();
}
public void registerHandlers() {
client.addEventListener(this);
}
public Channel getChannel(long id) {
Channel result = client.getGuildChannelById(id);
if (result != null) {
return result;
}
result = client.getPrivateChannelById(id);
if (result != null) {
return result;
}
return null;
}
@Override
public void onMessageReactionAdd(MessageReactionAddEvent event) {
autoHandle(event, DiscordMessageReactionAddScriptEvent.instance);
}
@Override
public void onMessageReactionRemove(MessageReactionRemoveEvent event) {
autoHandle(event, DiscordMessageReactionRemoveScriptEvent.instance);
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
cache.onMessageReceived(event);
autoHandle(event, DiscordMessageReceivedScriptEvent.instance);
}
@Override
public void onMessageUpdate(MessageUpdateEvent event) {
Message oldMessage = cache.getMessage(event.getChannel().getIdLong(), event.getMessageIdLong());
cache.onMessageUpdate(event);
autoHandle(event, DiscordMessageModifiedScriptEvent.instance, (e) -> {
e.oldMessage = oldMessage;
});
}
@Override
public void onMessageDelete(MessageDeleteEvent event) {
autoHandle(event, DiscordMessageDeletedScriptEvent.instance);
}
@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
autoHandle(event, DiscordUserJoinsScriptEvent.instance);
}
@Override
public void onGuildMemberRemove(GuildMemberRemoveEvent event) {
autoHandle(event, DiscordUserLeavesScriptEvent.instance);
}
@Override
public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event) {
autoHandle(event, DiscordUserRoleChangeScriptEvent.instance);
}
@Override
public void onGuildMemberRoleRemove(GuildMemberRoleRemoveEvent event) {
autoHandle(event, DiscordUserRoleChangeScriptEvent.instance);
}
@Override
public void onGuildMemberUpdateNickname(GuildMemberUpdateNicknameEvent event) {
autoHandle(event, DiscordUserNicknameChangeScriptEvent.instance);
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
autoHandle(event, DiscordApplicationCommandScriptEvent.instance);
}
@Override
public void onButtonInteraction(ButtonInteractionEvent event) {
autoHandle(event, DiscordButtonClickedScriptEvent.instance);
}
@Override
public void onMessageContextInteraction(MessageContextInteractionEvent event) {
autoHandle(event, DiscordApplicationCommandScriptEvent.instance);
}
@Override
public void onUserContextInteraction(UserContextInteractionEvent event) {
autoHandle(event, DiscordApplicationCommandScriptEvent.instance);
}
@Override
public void onModalInteraction(ModalInteractionEvent event) {
autoHandle(event, DiscordModalSubmittedScriptEvent.instance);
}
@Override
public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
autoHandle(event, DiscordSelectionUsedScriptEvent.instance);
}
@Override
public void onChannelCreate(@Nonnull ChannelCreateEvent event) {
autoHandle(event, DiscordChannelCreateScriptEvent.instance);
}
@Override
public void onChannelDelete(@Nonnull ChannelDeleteEvent event) {
autoHandle(event, DiscordChannelDeleteScriptEvent.instance);
}
@Override
public void onThreadRevealed(@Nonnull ThreadRevealedEvent event) {
autoHandle(event, DiscordThreadRevealedScriptEvent.instance);
}
@Override
public void onThreadHidden(@Nonnull ThreadHiddenEvent event) { // TODO: Is 'hidden' the same as 'archived'?
autoHandle(event, DiscordThreadArchivedScriptEvent.instance);
}
public void autoHandle(Event event, DiscordScriptEvent scriptEvent) {
autoHandle(event, scriptEvent, null);
}
public <T extends DiscordScriptEvent> void autoHandle(Event event, T scriptEvent, Consumer<T> configure) {
Bukkit.getScheduler().runTask(DenizenDiscordBot.instance, () -> {
if (!scriptEvent.enabled) {
return;
}
if (configure != null) {
configure.accept(scriptEvent);
}
scriptEvent.botID = botID;
scriptEvent.event = event;
scriptEvent.cancelled = false;
scriptEvent.fire();
});
}
}