/
DenizenDiscordBot.java
351 lines (327 loc) · 18.7 KB
/
DenizenDiscordBot.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package com.denizenscript.ddiscordbot;
import com.denizenscript.ddiscordbot.commands.*;
import com.denizenscript.ddiscordbot.events.*;
import com.denizenscript.ddiscordbot.objects.*;
import com.denizenscript.ddiscordbot.properties.DiscordTimeTagProperties;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.TimeTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.utilities.debugging.*;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.tags.TagManager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
public class DenizenDiscordBot extends JavaPlugin {
public static Warning oldMessageContexts = new SlowWarning("oldMessageContexts", "dDiscordBot contexts relating to message data are now provided by DiscordMessageTag.");
public static Warning oldMessageCommand = new SlowWarning("oldMessageCommand", "dDiscordBot's 'discord message' sub-command has been moved to a base 'discordmessage' command.");
public static Warning oldConnectCommand = new FutureWarning("oldConnectCommand", "dDiscordBot's 'discord connect' sub-command has been moved to a base 'discordconnect' command.");
public static Warning oldStopTyping = new FutureWarning("oldStopTyping", "dDiscordBot's 'discord stop_typing' sub-command is deprecated as it does nothing.");
public static Warning oldDeleteMessage = new FutureWarning("oldDeleteMessage", "dDiscordBot's 'discord delete_message' sub-command is deprecated in favor of 'adjust <[message]> delete'.");
public static Warning oldEditMessage = new SlowWarning("oldEditMessage", "dDiscordBot's 'discord edit_message' sub-command has been moved to the 'discordmessage' command.");
public static Warning oldTokenFile = new SlowWarning("oldTokenFile", "dDiscordBot used to recommend 'tokenfile' for 'discordconnect', however it is now recommended that you use a SecretTag and the 'secrets.secret' file for the token.");
public static Warning oldCommandPermissions = new StrongWarning("oldCommandPermissions", "dDiscordBot's 'discordcommand' command's 'enabled', 'enable_for', 'disable_for' arguments and its 'perms' instruction no longer function due to API changes; use the 'Integrations' panel in your server settings instead.");
public static DenizenDiscordBot instance;
public HashMap<String, DiscordConnection> connections = new HashMap<>();
public static boolean allowMessageRetrieval = true;
public static int messageCacheSize = 128;
@Override
public void onEnable() {
Debug.log("dDiscordBot loaded!");
instance = this;
saveDefaultConfig();
FileConfiguration config = getConfig();
if (config != null) {
allowMessageRetrieval = config.getBoolean("Allow message lookup", true);
messageCacheSize = config.getInt("Message cache size", 128);
}
try {
// Commands
DenizenCore.commandRegistry.registerCommand(DiscordBanCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordCommandCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordConnectCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordCreateChannelCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordCreateThreadCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordInteractionCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordMessageCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordModalCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordReactCommand.class);
DenizenCore.commandRegistry.registerCommand(DiscordTimeoutCommand.class);
// Events
ScriptEvent.registerScriptEvent(DiscordApplicationCommandScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordButtonClickedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordChannelCreateScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordChannelDeleteScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordCommandAutocompleteScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageDeletedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageModifiedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageReactionAddScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageReactionRemoveScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageReceivedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordModalSubmittedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordSelectionUsedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordThreadArchivedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordThreadRevealedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordUserJoinsScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordUserLeavesScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordUserNicknameChangeScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordUserRoleChangeScriptEvent.class);
// Objects
ObjectFetcher.registerWithObjectFetcher(DiscordBotTag.class, DiscordBotTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordButtonTag.class, DiscordButtonTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordChannelTag.class, DiscordChannelTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordCommandTag.class, DiscordCommandTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordEmbedTag.class, DiscordEmbedTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordGroupTag.class, DiscordGroupTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordInteractionTag.class, DiscordInteractionTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordMessageTag.class, DiscordMessageTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordReactionTag.class, DiscordReactionTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordRoleTag.class, DiscordRoleTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordSelectionTag.class, DiscordSelectionTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordTextInputTag.class, DiscordTextInputTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordUserTag.class, DiscordUserTag.tagProcessor);
// Extension properties
PropertyParser.registerProperty(DiscordTimeTagProperties.class, TimeTag.class);
// <--[tag]
// @attribute <discord_bots>
// @returns ListTag(DiscordBotTag)
// @plugin dDiscordBot
// @description
// Returns a list of all Discord bots currently loaded in dDiscordBot.
// -->
TagManager.registerTagHandler(ListTag.class, "discord_bots", (attribute) -> {
ListTag bots = new ListTag();
for (String bot : connections.keySet()) {
bots.addObject(new DiscordBotTag(bot));
}
return bots;
});
// <--[tag]
// @attribute <discord[<bot-id>]>
// @returns DiscordBotTag
// @plugin dDiscordBot
// @description
// Returns the Discord bot for the given bot ID.
// -->
TagManager.registerTagHandler(DiscordBotTag.class, "discord", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord tag base must have input.");
return null;
}
return DiscordBotTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_button[(<button>)]>
// @returns DiscordButtonTag
// @plugin dDiscordBot
// @description
// Returns a blank DiscordButtonTag instance, to be filled with data via the with.as tag.
// Or, if given an input, returns a Discord Button object constructed from the input value.
// Refer to <@link objecttype DiscordButtonTag>.
// -->
TagManager.registerTagHandler(DiscordButtonTag.class, "discord_button", (attribute) -> {
if (!attribute.hasParam()) {
return new DiscordButtonTag();
}
return DiscordButtonTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_channel[<channel>]>
// @returns DiscordChannelTag
// @plugin dDiscordBot
// @description
// Returns a Discord Channel object constructed from the input value.
// Refer to <@link objecttype DiscordChannelTag>.
// -->
TagManager.registerTagHandler(DiscordChannelTag.class, "discord_channel", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord channel tag base must have input.");
return null;
}
return DiscordChannelTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_command[<command>]>
// @returns DiscordCommandTag
// @plugin dDiscordBot
// @description
// Returns a Discord Command object constructed from the input value.
// Refer to <@link objecttype DiscordCommandTag>.
// -->
TagManager.registerTagHandler(DiscordCommandTag.class, "discord_command", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord command tag base must have input.");
return null;
}
return DiscordCommandTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_embed[(<embed>)]>
// @returns DiscordEmbedTag
// @plugin dDiscordBot
// @description
// Returns a blank DiscordEmbedTag instance, to be filled with data via the with.as tag.
// Or, if given an input, returns a Discord Embed object constructed from the input value.
// Refer to <@link objecttype DiscordEmbedTag>.
// -->
TagManager.registerTagHandler(DiscordEmbedTag.class, "discord_embed", (attribute) -> {
if (!attribute.hasParam()) {
return new DiscordEmbedTag();
}
return DiscordEmbedTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_group[<group>]>
// @returns DiscordGroupTag
// @plugin dDiscordBot
// @description
// Returns a Discord Group object constructed from the input value.
// Refer to <@link objecttype DiscordGroupTag>.
// -->
TagManager.registerTagHandler(DiscordGroupTag.class, "discord_group", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord group tag base must have input.");
return null;
}
return DiscordGroupTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_interaction[<interaction>]>
// @returns DiscordInteractionTag
// @plugin dDiscordBot
// @description
// Returns a Discord Interaction object constructed from the input value.
// Refer to <@link objecttype DiscordInteractionTag>.
// -->
TagManager.registerTagHandler(DiscordInteractionTag.class, "discord_interaction", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord interaction tag base must have input.");
return null;
}
return DiscordInteractionTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_message[<message>]>
// @returns DiscordMessageTag
// @plugin dDiscordBot
// @description
// Returns a Discord Message object constructed from the input value.
// Refer to <@link objecttype DiscordMessageTag>.
// -->
TagManager.registerTagHandler(DiscordMessageTag.class, "discord_message", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord message tag base must have input.");
return null;
}
return DiscordMessageTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_reaction[<reaction>]>
// @returns DiscordReactionTag
// @plugin dDiscordBot
// @description
// Returns a Discord Reaction object constructed from the input value.
// Refer to <@link objecttype DiscordReactionTag>.
// -->
TagManager.registerTagHandler(DiscordReactionTag.class, "discord_reaction", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord reaction tag base must have input.");
return null;
}
return DiscordReactionTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_role[<role>]>
// @returns DiscordRoleTag
// @plugin dDiscordBot
// @description
// Returns a Discord Role object constructed from the input value.
// Refer to <@link objecttype DiscordRoleTag>.
// -->
TagManager.registerTagHandler(DiscordRoleTag.class, "discord_role", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord role tag base must have input.");
return null;
}
return DiscordRoleTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_selection[(<menu>)]>
// @returns DiscordSelectionTag
// @plugin dDiscordBot
// @description
// Returns a blank DiscordSelectionTag instance, to be filled with data via the with.as tag.
// Or, if given an input, returns a Discord Selection object constructed from the input value.
// Refer to <@link objecttype DiscordSelectionTag>.
// -->
TagManager.registerTagHandler(DiscordSelectionTag.class, "discord_selection", (attribute) -> {
if (!attribute.hasParam()) {
return new DiscordSelectionTag();
}
return DiscordSelectionTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_text_input[(<button>)]>
// @returns DiscordTextInputTag
// @plugin dDiscordBot
// @description
// Returns a blank DiscordTextInputTag instance, to be filled with data via the with.as tag.
// Or, if given an input, returns a Discord TextInput object constructed from the input value.
// Refer to <@link objecttype DiscordTextInputTag>.
// -->
TagManager.registerTagHandler(DiscordTextInputTag.class, "discord_text_input", (attribute) -> {
if (!attribute.hasParam()) {
return new DiscordTextInputTag();
}
return DiscordTextInputTag.valueOf(attribute.getParam(), attribute.context);
});
// <--[tag]
// @attribute <discord_user[<user>]>
// @returns DiscordUserTag
// @plugin dDiscordBot
// @description
// Returns a Discord User object constructed from the input value.
// Refer to <@link objecttype DiscordUserTag>.
// -->
TagManager.registerTagHandler(DiscordUserTag.class, "discord_user", (attribute) -> {
if (!attribute.hasParam()) {
attribute.echoError("Discord user tag base must have input.");
return null;
}
return DiscordUserTag.valueOf(attribute.getParam(), attribute.context);
});
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
@Override
public void onDisable() {
for (Map.Entry<String, DiscordConnection> connection : connections.entrySet()) {
try {
if (connection.getValue().client != null) {
if (connection.getValue().flags.modified) {
connection.getValue().flags.saveToFile(DiscordConnectCommand.flagFilePathFor(connection.getKey()));
}
connection.getValue().client.shutdownNow();
}
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
connections.clear();
DiscordInteractionTag.interactionCache.clear();
Bukkit.getServer().getScheduler().cancelTasks(this);
HandlerList.unregisterAll(this);
// To prevent issue with onDisable order, tell Denizen to shutdown NOW (it won't repeat itself).
Denizen.getInstance().onDisable();
}
}