forked from JDA-Applications/JDA-Utilities
-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
CommandClientImpl.java
956 lines (854 loc) · 34.1 KB
/
CommandClientImpl.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
/*
* Copyright 2016-2018 John Grosh (jagrosh) & Kaidan Gustave (TheMonitorLizard)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jagrosh.jdautilities.command.impl;
import com.jagrosh.jdautilities.command.*;
import com.jagrosh.jdautilities.command.Command.Category;
import com.jagrosh.jdautilities.commons.utils.FixedSizeCache;
import com.jagrosh.jdautilities.commons.utils.SafeIdUtil;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.ShutdownEvent;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege;
import net.dv8tion.jda.internal.utils.Checks;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.Reader;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* An implementation of {@link com.jagrosh.jdautilities.command.CommandClient CommandClient} to be used by a bot.
*
* <p>This is a listener usable with {@link net.dv8tion.jda.api.JDA JDA}, as it implements
* {@link net.dv8tion.jda.api.hooks.EventListener EventListener} in order to catch and use different kinds of
* {@link net.dv8tion.jda.api.events.Event Event}s. The primary usage of this is where the CommandClient implementation
* takes {@link net.dv8tion.jda.api.events.message.MessageReceivedEvent MessageReceivedEvent}s, and automatically
* processes arguments, and provide them to a {@link com.jagrosh.jdautilities.command.Command Command} for
* running and execution.
*
* @author John Grosh (jagrosh)
*/
public class CommandClientImpl implements CommandClient, EventListener
{
private static final Logger LOG = LoggerFactory.getLogger(CommandClient.class);
private static final String DEFAULT_PREFIX = "@mention";
private final OffsetDateTime start;
private final Activity activity;
private final OnlineStatus status;
private final String ownerId;
private final String[] coOwnerIds;
private final String prefix;
private final String altprefix;
private final String[] prefixes;
private final Function<MessageReceivedEvent, String> prefixFunction;
private final Function<MessageReceivedEvent, Boolean> commandPreProcessFunction;
private final String serverInvite;
private final HashMap<String, Integer> commandIndex;
private final HashMap<String, Integer> slashCommandIndex;
private final ArrayList<Command> commands;
private final ArrayList<SlashCommand> slashCommands;
private final ArrayList<String> slashCommandIds;
private final String forcedGuildId;
private final boolean manualUpsert;
private final String success;
private final String warning;
private final String error;
private final String botsKey, carbonKey;
private final HashMap<String,OffsetDateTime> cooldowns;
private final HashMap<String,Integer> uses;
private final FixedSizeCache<Long, Set<Message>> linkMap;
private final boolean useHelp;
private final boolean shutdownAutomatically;
private final Consumer<CommandEvent> helpConsumer;
private final String helpWord;
private final ScheduledExecutorService executor;
private final AnnotatedModuleCompiler compiler;
private final GuildSettingsManager manager;
private String textPrefix;
private CommandListener listener = null;
private int totalGuilds;
public CommandClientImpl(String ownerId, String[] coOwnerIds, String prefix, String altprefix, String[] prefixes, Function<MessageReceivedEvent, String> prefixFunction, Function<MessageReceivedEvent, Boolean> commandPreProcessFunction, Activity activity, OnlineStatus status, String serverInvite,
String success, String warning, String error, String carbonKey, String botsKey, ArrayList<Command> commands, ArrayList<SlashCommand> slashCommands, String forcedGuildId, boolean manualUpsert,
boolean useHelp, boolean shutdownAutomatically, Consumer<CommandEvent> helpConsumer, String helpWord, ScheduledExecutorService executor,
int linkedCacheSize, AnnotatedModuleCompiler compiler, GuildSettingsManager manager)
{
Checks.check(ownerId != null, "Owner ID was set null or not set! Please provide an User ID to register as the owner!");
if(!SafeIdUtil.checkId(ownerId))
LOG.warn(String.format("The provided Owner ID (%s) was found unsafe! Make sure ID is a non-negative long!", ownerId));
if(coOwnerIds!=null)
{
for(String coOwnerId : coOwnerIds)
{
if(!SafeIdUtil.checkId(coOwnerId))
LOG.warn(String.format("The provided CoOwner ID (%s) was found unsafe! Make sure ID is a non-negative long!", coOwnerId));
}
}
this.start = OffsetDateTime.now();
this.ownerId = ownerId;
this.coOwnerIds = coOwnerIds;
this.prefix = prefix==null || prefix.isEmpty() ? DEFAULT_PREFIX : prefix;
this.altprefix = altprefix==null || altprefix.isEmpty() ? null : altprefix;
this.prefixes = prefixes==null || prefixes.length == 0 ? null : prefixes;
if (this.prefixes != null) {
Arrays.sort(this.prefixes, Comparator.reverseOrder());
}
this.prefixFunction = prefixFunction;
this.commandPreProcessFunction = commandPreProcessFunction==null ? event -> true : commandPreProcessFunction;
this.textPrefix = prefix;
this.activity = activity;
this.status = status;
this.serverInvite = serverInvite;
this.success = success==null ? "": success;
this.warning = warning==null ? "": warning;
this.error = error==null ? "": error;
this.carbonKey = carbonKey;
this.botsKey = botsKey;
this.commandIndex = new HashMap<>();
this.slashCommandIndex = new HashMap<>();
this.commands = new ArrayList<>();
this.slashCommands = new ArrayList<>();
this.slashCommandIds = new ArrayList<>();
this.forcedGuildId = forcedGuildId;
this.manualUpsert = manualUpsert;
this.cooldowns = new HashMap<>();
this.uses = new HashMap<>();
this.linkMap = linkedCacheSize>0 ? new FixedSizeCache<>(linkedCacheSize) : null;
this.useHelp = useHelp;
this.shutdownAutomatically = shutdownAutomatically;
this.helpWord = helpWord==null ? "help" : helpWord;
this.executor = executor==null ? Executors.newSingleThreadScheduledExecutor() : executor;
this.compiler = compiler;
this.manager = manager;
this.helpConsumer = helpConsumer==null ? (event) -> {
StringBuilder builder = new StringBuilder("**"+event.getSelfUser().getName()+"** commands:\n");
Category category = null;
for(Command command : commands)
{
if(!command.isHidden() && (!command.isOwnerCommand() || event.isOwner()))
{
if(!Objects.equals(category, command.getCategory()))
{
category = command.getCategory();
builder.append("\n\n __").append(category==null ? "No Category" : category.getName()).append("__:\n");
}
builder.append("\n`").append(textPrefix).append(prefix==null?" ":"").append(command.getName())
.append(command.getArguments()==null ? "`" : " "+command.getArguments()+"`")
.append(" - ").append(command.getHelp());
}
}
User owner = event.getJDA().getUserById(ownerId);
if(owner!=null)
{
builder.append("\n\nFor additional help, contact **").append(owner.getName()).append("**#").append(owner.getDiscriminator());
if(serverInvite!=null)
builder.append(" or join ").append(serverInvite);
}
event.replyInDm(builder.toString(), unused ->
{
if(event.isFromType(ChannelType.TEXT))
event.reactSuccess();
}, t -> event.replyWarning("Help cannot be sent because you are blocking Direct Messages."));
} : helpConsumer;
// Load commands
for(Command command : commands)
{
addCommand(command);
}
// Load slash commands
for(SlashCommand command : slashCommands)
{
addSlashCommand(command);
}
}
@Override
public void setListener(CommandListener listener)
{
this.listener = listener;
}
@Override
public CommandListener getListener()
{
return listener;
}
@Override
public List<Command> getCommands()
{
return commands;
}
@Override
public List<SlashCommand> getSlashCommands()
{
return slashCommands;
}
@Override
public boolean isManualUpsert()
{
return manualUpsert;
}
@Override
public String forcedGuildId()
{
return forcedGuildId;
}
@Override
public OffsetDateTime getStartTime()
{
return start;
}
@Override
public OffsetDateTime getCooldown(String name)
{
return cooldowns.get(name);
}
@Override
public int getRemainingCooldown(String name)
{
if(cooldowns.containsKey(name))
{
int time = (int) Math.ceil(OffsetDateTime.now().until(cooldowns.get(name), ChronoUnit.MILLIS) / 1000D);
if(time<=0)
{
cooldowns.remove(name);
return 0;
}
return time;
}
return 0;
}
@Override
public void applyCooldown(String name, int seconds)
{
cooldowns.put(name, OffsetDateTime.now().plusSeconds(seconds));
}
@Override
public void cleanCooldowns()
{
OffsetDateTime now = OffsetDateTime.now();
cooldowns.keySet().stream().filter((str) -> (cooldowns.get(str).isBefore(now)))
.collect(Collectors.toList()).forEach(cooldowns::remove);
}
@Override
public int getCommandUses(Command command)
{
return getCommandUses(command.getName());
}
@Override
public int getCommandUses(String name)
{
return uses.getOrDefault(name, 0);
}
@Override
public void addCommand(Command command)
{
addCommand(command, commands.size());
}
@Override
public void addCommand(Command command, int index)
{
if(index>commands.size() || index<0)
throw new ArrayIndexOutOfBoundsException("Index specified is invalid: ["+index+"/"+commands.size()+"]");
synchronized(commandIndex)
{
String name = command.getName().toLowerCase();
//check for collision
if(commandIndex.containsKey(name))
throw new IllegalArgumentException("Command added has a name or alias that has already been indexed: \""+name+"\"!");
for(String alias : command.getAliases())
{
if(commandIndex.containsKey(alias.toLowerCase()))
throw new IllegalArgumentException("Command added has a name or alias that has already been indexed: \""+alias+"\"!");
}
//shift if not append
if(index<commands.size())
{
commandIndex.entrySet().stream().filter(entry -> entry.getValue()>=index).collect(Collectors.toList())
.forEach(entry -> commandIndex.put(entry.getKey(), entry.getValue()+1));
}
//add
commandIndex.put(name, index);
for(String alias : command.getAliases())
commandIndex.put(alias.toLowerCase(), index);
}
commands.add(index,command);
}
@Override
public void addSlashCommand(SlashCommand command)
{
addSlashCommand(command, slashCommands.size());
}
@Override
public void addSlashCommand(SlashCommand command, int index)
{
if(index>slashCommands.size() || index<0)
throw new ArrayIndexOutOfBoundsException("Index specified is invalid: ["+index+"/"+slashCommands.size()+"]");
synchronized(slashCommandIndex)
{
String name = command.getName().toLowerCase();
//check for collision
if(slashCommandIndex.containsKey(name))
throw new IllegalArgumentException("Command added has a name that has already been indexed: \""+name+"\"!");
//shift if not append
if(index<slashCommands.size())
{
slashCommandIndex.entrySet().stream().filter(entry -> entry.getValue()>=index).collect(Collectors.toList())
.forEach(entry -> slashCommandIndex.put(entry.getKey(), entry.getValue()+1));
}
//add
slashCommandIndex.put(name, index);
}
slashCommands.add(index,command);
}
@Override
public void removeCommand(String name)
{
synchronized(commandIndex)
{
if(!commandIndex.containsKey(name.toLowerCase()))
throw new IllegalArgumentException("Name provided is not indexed: \"" + name + "\"!");
int targetIndex = commandIndex.remove(name.toLowerCase());
Command removedCommand = commands.remove(targetIndex);
for(String alias : removedCommand.getAliases())
{
commandIndex.remove(alias.toLowerCase());
}
commandIndex.entrySet().stream().filter(entry -> entry.getValue()>targetIndex).collect(Collectors.toList())
.forEach(entry -> commandIndex.put(entry.getKey(), entry.getValue()-1));
}
}
@Override
public void addAnnotatedModule(Object module)
{
compiler.compile(module).forEach(this::addCommand);
}
@Override
public void addAnnotatedModule(Object module, Function<Command, Integer> mapFunction)
{
compiler.compile(module).forEach(command -> addCommand(command, mapFunction.apply(command)));
}
@Override
public String getOwnerId()
{
return ownerId;
}
@Override
public long getOwnerIdLong()
{
return Long.parseLong(ownerId);
}
@Override
public String[] getCoOwnerIds()
{
return coOwnerIds;
}
@Override
public long[] getCoOwnerIdsLong()
{
// Thought about using java.util.Arrays#setAll(T[], IntFunction<T>)
// here, but as it turns out it's actually the same thing as this but
// it throws an error if null. Go figure.
if(coOwnerIds==null)
return null;
long[] ids = new long[coOwnerIds.length];
for(int i = 0; i<ids.length; i++)
ids[i] = Long.parseLong(coOwnerIds[i]);
return ids;
}
@Override
public String getSuccess()
{
return success;
}
@Override
public String getWarning()
{
return warning;
}
@Override
public String getError()
{
return error;
}
@Override
public ScheduledExecutorService getScheduleExecutor()
{
return executor;
}
@Override
public String getServerInvite()
{
return serverInvite;
}
@Override
public String getPrefix()
{
return prefix;
}
@Override
public String[] getPrefixes() {
return prefixes;
}
@Override
public Function<MessageReceivedEvent, String> getPrefixFunction()
{
return prefixFunction;
}
@Override
public String getAltPrefix()
{
return altprefix;
}
@Override
public String getTextualPrefix()
{
return textPrefix;
}
@Override
public int getTotalGuilds()
{
return totalGuilds;
}
@Override
public String getHelpWord()
{
return helpWord;
}
@Override
public boolean usesLinkedDeletion() {
return linkMap != null;
}
@SuppressWarnings("unchecked")
@Override
public <S> S getSettingsFor(Guild guild)
{
if (manager==null)
return null;
return (S) manager.getSettings(guild);
}
@SuppressWarnings("unchecked")
@Override
public <M extends GuildSettingsManager> M getSettingsManager()
{
return (M) manager;
}
@Override
public void shutdown()
{
GuildSettingsManager<?> manager = getSettingsManager();
if(manager != null)
manager.shutdown();
executor.shutdown();
}
@Override
public void onEvent(GenericEvent event)
{
if(event instanceof MessageReceivedEvent)
onMessageReceived((MessageReceivedEvent)event);
else if(event instanceof SlashCommandEvent)
onSlashCommand((SlashCommandEvent)event);
else if(event instanceof GuildMessageDeleteEvent && usesLinkedDeletion())
onMessageDelete((GuildMessageDeleteEvent) event);
else if(event instanceof GuildJoinEvent)
{
if(((GuildJoinEvent)event).getGuild().getSelfMember().getTimeJoined()
.plusMinutes(10).isAfter(OffsetDateTime.now()))
sendStats(event.getJDA());
}
else if(event instanceof GuildLeaveEvent)
sendStats(event.getJDA());
else if(event instanceof ReadyEvent)
onReady((ReadyEvent)event);
else if(event instanceof ShutdownEvent)
{
if(shutdownAutomatically)
shutdown();
}
}
private void onReady(ReadyEvent event)
{
if(!event.getJDA().getSelfUser().isBot())
{
LOG.error("JDA-Utilities does not support CLIENT accounts.");
event.getJDA().shutdown();
return;
}
textPrefix = prefix.equals(DEFAULT_PREFIX) ? "@"+event.getJDA().getSelfUser().getName()+" " : prefix;
if(activity != null)
event.getJDA().getPresence().setPresence(status==null ? OnlineStatus.ONLINE : status,
"default".equals(activity.getName()) ? Activity.playing("Type "+textPrefix+helpWord) : activity);
// Start SettingsManager if necessary
GuildSettingsManager<?> manager = getSettingsManager();
if(manager != null)
manager.init();
// Upsert slash commands, if not manual
if (!manualUpsert)
{
for (SlashCommand command : slashCommands)
{
CommandData data = command.buildCommandData();
if (forcedGuildId != null || (command.isGuildOnly() && command.getGuildId() != null)) {
String guildId = forcedGuildId != null ? forcedGuildId : command.getGuildId();
Guild guild = event.getJDA().getGuildById(guildId);
if (guild == null) {
LOG.error("Could not find guild with specified ID: " + forcedGuildId + ". Not going to upsert.");
continue;
}
List<CommandPrivilege> privileges = command.buildPrivileges(this);
guild.upsertCommand(data).queue(command1 -> {
slashCommandIds.add(command1.getId());
if (!privileges.isEmpty())
command1.updatePrivileges(guild, privileges).queue();
});
} else {
event.getJDA().upsertCommand(data).queue(command1 -> slashCommandIds.add(command1.getId()));
}
}
}
sendStats(event.getJDA());
}
private void onMessageReceived(MessageReceivedEvent event)
{
// Return if it's a bot
if(event.getAuthor().isBot())
return;
final MessageParts parts = getParts(event);
if(parts!=null) //starts with valid prefix
{
if(useHelp && parts.command.equalsIgnoreCase(helpWord))
{
CommandEvent cevent = new CommandEvent(event, parts.prefixUsed, parts.args, this);
if(listener!=null)
listener.onCommand(cevent, null);
helpConsumer.accept(cevent); // Fire help consumer
if(listener!=null)
listener.onCompletedCommand(cevent, null);
return; // Help Consumer is done
}
else if(event.isFromType(ChannelType.PRIVATE) || event.getTextChannel().canTalk())
{
String name = parts.command;
String args = parts.args;
final Command command; // this will be null if it's not a command
synchronized(commandIndex)
{
int i = commandIndex.getOrDefault(name.toLowerCase(), -1);
command = i != -1? commands.get(i) : null;
}
if(command != null)
{
CommandEvent cevent = new CommandEvent(event, parts.prefixUsed, args, this);
if(listener != null)
listener.onCommand(cevent, command);
uses.put(command.getName(), uses.getOrDefault(command.getName(), 0) + 1);
if(commandPreProcessFunction.apply(event))
{
command.run(cevent);
}
return; // Command is done
}
}
}
if(listener != null)
listener.onNonCommandMessage(event);
}
@Nullable
private MessageParts getParts(MessageReceivedEvent event) {
String rawContent = event.getMessage().getContentRaw();
GuildSettingsProvider settings = event.isFromType(ChannelType.TEXT)? provideSettings(event.getGuild()) : null;
// Check for prefix or alternate prefix (@mention cases)
if(prefix.equals(DEFAULT_PREFIX) || (altprefix != null && altprefix.equals(DEFAULT_PREFIX))) {
if(rawContent.startsWith("<@"+ event.getJDA().getSelfUser().getId()+">") ||
rawContent.startsWith("<@!"+ event.getJDA().getSelfUser().getId()+">")) {
final int prefixLength = rawContent.indexOf('>') + 1;
return makeMessageParts(rawContent, prefixLength);
}
}
// Check for prefix
// Run Function check if there is one, then fallback to normal prefixes
if (prefixFunction != null) {
String prefix = prefixFunction.apply(event);
// Don't lowercase, up to Function to handle this
if (prefix != null && rawContent.startsWith(prefix)) {
final int prefixLength = prefix.length();
return makeMessageParts(rawContent, prefixLength);
}
}
final String lowerCaseContent = rawContent.toLowerCase();
// Check for default prefix
if (lowerCaseContent.startsWith(prefix.toLowerCase())) {
final int prefixLength = prefix.length();
return makeMessageParts(rawContent, prefixLength);
}
// Check for alternate prefix
if(altprefix != null && lowerCaseContent.startsWith(altprefix.toLowerCase())) {
final int prefixLength = altprefix.length();
return makeMessageParts(rawContent, prefixLength);
}
// Check for prefixes
if (prefixes != null) {
for (String pre : prefixes) {
if (lowerCaseContent.startsWith(pre.toLowerCase())) {
final int prefixLength = pre.length();
return makeMessageParts(rawContent, prefixLength);
}
}
}
// Check for guild specific prefixes
if(settings != null) {
Collection<String> prefixes = settings.getPrefixes();
if(prefixes != null) {
for(String prefix : prefixes) {
if(lowerCaseContent.startsWith(prefix.toLowerCase())) {
final int prefixLength = prefix.length();
return makeMessageParts(rawContent, prefixLength);
}
}
}
}
return null;
}
/**
* Processes the message raw content and returns the "parts" of the message
* <br>These parts include:
* <ul>
* <li>Used prefix</li>
* <li>Command name</li>
* <li>Arguments</li>
* </ul>
*
* @param rawContent
* The raw content of the incoming message
* @param prefixLength
* The length of the prefix that has been successfully detected before calling this method
* @return A MessageParts objects with all the parts cited above
*/
@NotNull
private CommandClientImpl.MessageParts makeMessageParts(String rawContent, int prefixLength) {
//Replacement method below
// final String[] split = rawContent.substring(prefixLength).trim().split("\\s+", 2);
//What we do is search for the first whitespace after the prefix, this gets us the command name
// To then get the arguments, we find the first occurrence of a character other than a whitespace, after the command index, and take the string from that index
String cmd = null;
for (int i = prefixLength; i < rawContent.length(); i++) {
if (Character.isWhitespace(rawContent.charAt(i))) { //If a whitespace assume we found the end of the command name
cmd = rawContent.substring(prefixLength, i);
break;
}
}
String args = "";
if (cmd == null) { //Assume there are no args since there were absolutely no whitespace
cmd = rawContent.substring(prefixLength);
} else {
for (int i = prefixLength + cmd.length(); i < rawContent.length(); i++) {
if (!Character.isWhitespace(rawContent.charAt(i))) { //If not a whitespace assume we found the start of the arguments
args = rawContent.substring(i);
break;
}
}
}
//Just in case something fucked up
LOG.trace("Received command named '{}' with args '{}'", cmd, args);
return new MessageParts(
rawContent.substring(0, prefixLength),
cmd,
args
);
}
private void onSlashCommand(SlashCommandEvent event)
{
final SlashCommand command; // this will be null if it's not a command
synchronized(slashCommandIndex)
{
int i = slashCommandIndex.getOrDefault(event.getName().toLowerCase(), -1);
command = i != -1? slashCommands.get(i) : null;
}
if(command != null)
{
if(listener != null)
listener.onSlashCommand(event, command);
uses.put(command.getName(), uses.getOrDefault(command.getName(), 0) + 1);
command.run(event, this);
// Command is done
}
}
private void sendStats(JDA jda)
{
OkHttpClient client = jda.getHttpClient();
if(carbonKey != null)
{
FormBody.Builder bodyBuilder = new FormBody.Builder()
.add("key", carbonKey)
.add("servercount", Integer.toString(jda.getGuilds().size()));
if(jda.getShardInfo() != null)
{
bodyBuilder.add("shard_id", Integer.toString(jda.getShardInfo().getShardId()))
.add("shard_count", Integer.toString(jda.getShardInfo().getShardTotal()));
}
Request.Builder builder = new Request.Builder()
.post(bodyBuilder.build())
.url("https://www.carbonitex.net/discord/data/botdata.php");
client.newCall(builder.build()).enqueue(new Callback()
{
@Override
public void onResponse(Call call, Response response)
{
LOG.info("Successfully send information to carbonitex.net");
response.close();
}
@Override
public void onFailure(Call call, IOException e)
{
LOG.error("Failed to send information to carbonitex.net ", e);
}
});
}
if(botsKey != null)
{
JSONObject body = new JSONObject().put("guildCount", jda.getGuilds().size());
if(jda.getShardInfo() != null)
{
body.put("shardId", jda.getShardInfo().getShardId())
.put("shardCount", jda.getShardInfo().getShardTotal());
}
Request.Builder builder = new Request.Builder()
.post(RequestBody.create(MediaType.parse("application/json"), body.toString()))
.url("https://discord.bots.gg/api/v1/bots/" + jda.getSelfUser().getId() + "/stats")
.header("Authorization", botsKey)
.header("Content-Type", "application/json");
client.newCall(builder.build()).enqueue(new Callback()
{
@Override
public void onResponse(Call call, Response response) throws IOException
{
if(response.isSuccessful())
{
LOG.info("Successfully sent information to discord.bots.gg");
try(Reader reader = response.body().charStream())
{
totalGuilds = new JSONObject(new JSONTokener(reader)).getInt("guildCount");
}
catch(Exception ex)
{
LOG.error("Failed to retrieve bot shard information from discord.bots.gg ", ex);
}
}
else
LOG.error("Failed to send information to discord.bots.gg: "+response.body().string());
response.close();
}
@Override
public void onFailure(Call call, IOException e)
{
LOG.error("Failed to send information to discord.bots.gg ", e);
}
});
}
else if (jda.getShardManager() != null)
{
totalGuilds = (int) jda.getShardManager().getGuildCache().size();
}
else
{
totalGuilds = (int) jda.getGuildCache().size();
}
}
private void onMessageDelete(GuildMessageDeleteEvent event)
{
// We don't need to cover whether or not this client usesLinkedDeletion() because
// that is checked in onEvent(Event) before this is even called.
synchronized(linkMap)
{
if(linkMap.contains(event.getMessageIdLong()))
{
Set<Message> messages = linkMap.get(event.getMessageIdLong());
if(messages.size()>1 && event.getGuild().getSelfMember()
.hasPermission(event.getChannel(), Permission.MESSAGE_MANAGE))
event.getChannel().deleteMessages(messages).queue(unused -> {}, ignored -> {});
else if(messages.size()>0)
messages.forEach(m -> m.delete().queue(unused -> {}, ignored -> {}));
}
}
}
private GuildSettingsProvider provideSettings(Guild guild)
{
Object settings = getSettingsFor(guild);
if(settings instanceof GuildSettingsProvider) //implicit null check
return (GuildSettingsProvider)settings;
else
return null;
}
/**
* <b>DO NOT USE THIS!</b>
*
* <p>This is a method necessary for linking a bot's response messages
* to their corresponding call message ID.
* <br><b>Using this anywhere in your code can and will break your bot.</b>
*
* @param callId
* The ID of the call Message
* @param message
* The Message to link to the ID
*/
public void linkIds(long callId, Message message)
{
// We don't use linked deletion, so we don't do anything.
if(!usesLinkedDeletion())
return;
synchronized(linkMap)
{
Set<Message> stored = linkMap.get(callId);
if(stored != null)
stored.add(message);
else
{
stored = new HashSet<>();
stored.add(message);
linkMap.add(callId, stored);
}
}
}
private static class MessageParts {
private final String prefixUsed;
private final String command;
private final String args;
private MessageParts(String prefixUsed, String command, String args) {
this.prefixUsed = prefixUsed;
this.command = command;
this.args = args;
}
}
}