Skip to content

Commit 4cb1101

Browse files
author
raxixor
committed
Revamping stuff has begun.
1 parent feba61d commit 4cb1101

34 files changed

Lines changed: 1442 additions & 130 deletions

build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ repositories {
1717
jcenter()
1818
maven { url 'https://jitpack.io' }
1919
mavenCentral()
20-
flatDir {
21-
dirs 'deps/'
22-
}
2320
}
2421

2522
dependencies {
2623
testCompile 'junit:junit:4.12'
27-
compile 'net.dv8tion:JDA:3.3.1_286'
24+
compile 'net.dv8tion:JDA:3.3.1_304'
2825
compile 'org.slf4j:slf4j-simple:1.8.0-alpha2'
2926
compile 'org.apache.httpcomponents:httpcore:4.4.7'
3027
compile 'org.apache.httpcomponents:httpclient:4.5.3'
3128
compile 'com.google.code.gson:gson:2.8.2'
29+
compile 'com.google.guava:guava:23.2-jre'
3230
compile group: 'org.apache.commons', name: 'commons-text', version: '1.1'
3331
compile group: 'postgresql', name: 'postgresql', version: '9.1-901-1.jdbc4'
3432
compile group: 'org.jasypt', name: 'jasypt', version: '1.9.2'

src/main/java/com/raxixor/jdaddons/command/Command.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,86 @@
55
import com.sun.istack.internal.NotNull;
66
import net.dv8tion.jda.core.EmbedBuilder;
77
import net.dv8tion.jda.core.entities.*;
8-
import net.dv8tion.jda.core.exceptions.RateLimitedException;
98
import net.dv8tion.jda.core.requests.RestAction;
10-
import net.dv8tion.jda.core.utils.SimpleLog;
9+
import net.dv8tion.jda.core.utils.JDALogger;
10+
import org.slf4j.Logger;
1111

1212
import java.awt.*;
1313
import java.util.Arrays;
1414
import java.util.List;
1515

16-
public interface Command {
16+
public abstract class Command {
1717

18-
void execute(Message trig, List<String> args);
18+
public abstract void execute(Message trig, List<String> args);
1919

20-
default CommandDescription getDescription() {
20+
public CommandDescription getDescription() {
2121
return getClass()
2222
.getAnnotation(CommandDescription.class);
2323
}
2424

25-
default String getName() {
25+
public String getName() {
2626
return getDescription().name();
2727
}
2828

29-
default SimpleLog getLog() {
30-
return SimpleLog.getLog(getClass().getSimpleName());
29+
protected Logger getLog() {
30+
return JDALogger.getLog(getClass().getSimpleName());
3131
}
3232

33-
default String getAttributeValueFromKey(String key) {
33+
public String getAttributeValueFromKey(String key) {
3434
if (!hasAttribute(key)) return null;
3535
return Arrays.stream(getDescription().attributes())
3636
.filter(ca -> ca.key().equals(key)).findFirst().get().value();
3737
}
3838

39-
default boolean hasAttribute(String key) {
39+
public boolean hasAttribute(String key) {
4040
return Arrays.stream(getDescription().attributes())
4141
.anyMatch(ca -> ca.key().equals(key));
4242
}
4343

44-
default RestAction<Message> reply(@NotNull Message trig, String msg) {
44+
protected RestAction<Message> reply(@NotNull Message trig, String msg) {
4545
trig.getChannel().sendTyping().queue();
4646
return trig.getChannel().sendMessage(msg);
4747
}
4848

49-
default RestAction<Message> reply(@NotNull Message trig, String msg, EmoteLevel e) {
49+
protected RestAction<Message> reply(@NotNull Message trig, String msg, EmoteLevel e) {
5050
e = e != null ? e : EmoteLevel.INFO;
5151
return reply(trig, String.format("%s | %s", e, msg));
5252
}
5353

54-
default RestAction<Message> reply(@NotNull Message trig, MessageEmbed embed) {
54+
protected RestAction<Message> reply(@NotNull Message trig, MessageEmbed embed) {
5555
trig.getChannel().sendTyping().queue();
5656
return trig.getChannel().sendMessage(embed);
5757
}
5858

59-
default RestAction<Message> reply(@NotNull MessageChannel chan, String msg) {
59+
protected RestAction<Message> reply(@NotNull MessageChannel chan, String msg) {
6060
chan.sendTyping().queue();
6161
return chan.sendMessage(msg);
6262
}
6363

64-
default RestAction<Message> reply(@NotNull MessageChannel chan, String msg, EmoteLevel e) {
64+
protected RestAction<Message> reply(@NotNull MessageChannel chan, String msg, EmoteLevel e) {
6565
e = e != null ? e : EmoteLevel.INFO;
6666
return reply(chan, String.format("%s | %s", e, msg));
6767
}
6868

69-
default RestAction<Message> reply(@NotNull MessageChannel chan, MessageEmbed embed) {
69+
protected RestAction<Message> reply(@NotNull MessageChannel chan, MessageEmbed embed) {
7070
chan.sendTyping().queue();
7171
return chan.sendMessage(embed);
7272
}
7373

74-
default EmbedBuilder getBaseEmbed(@NotNull Color color, String footer) {
74+
protected EmbedBuilder getBaseEmbed(@NotNull Color color, String footer) {
7575
EmbedBuilder eb = new EmbedBuilder();
7676

7777
return eb.setColor(color).setFooter(footer, null);
7878
}
7979

80-
default EmbedBuilder getBaseEmbed(@NotNull Color color, String footer,
80+
protected EmbedBuilder getBaseEmbed(@NotNull Color color, String footer,
8181
User author) {
8282
String eff = author.getEffectiveAvatarUrl();
8383
return getBaseEmbed(color, footer).setAuthor(FormatUtil.formatUser(author),
8484
eff, eff);
8585
}
8686

87-
default EmbedBuilder getBaseEmbed(@NotNull Color color, String footer,
87+
protected EmbedBuilder getBaseEmbed(@NotNull Color color, String footer,
8888
Member author) {
8989
String eff = author.getUser().getEffectiveAvatarUrl();
9090
return getBaseEmbed(color, footer).setAuthor(FormatUtil.formatMember(author),

src/main/java/com/raxixor/jdaddons/command/CommandDescription.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
boolean respondToBots() default false;
1818

1919
CommandAttribute[] attributes() default {};
20+
21+
PermissionLevel level() default PermissionLevel.USER;
2022
}

src/main/java/com/raxixor/jdaddons/command/CommandHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
import com.raxixor.jdaddons.entities.EmoteLevel;
44
import com.raxixor.jdaddons.util.FormatUtil;
55
import net.dv8tion.jda.core.entities.Message;
6-
import net.dv8tion.jda.core.utils.SimpleLog;
6+
import net.dv8tion.jda.core.utils.JDALogger;
77
import org.apache.commons.text.StrSubstitutor;
88
import org.apache.commons.text.similarity.LevenshteinDistance;
9+
import org.slf4j.Logger;
910

1011
import java.util.*;
1112

1213
public class CommandHandler {
1314

14-
private final SimpleLog log = SimpleLog.getLog("CommandHandler");
15+
private final Logger log = JDALogger.getLog("CommandHandler");
1516

1617
private final Set<Command> commands = new HashSet<>();
1718

@@ -97,11 +98,11 @@ public void execute(Command cmd, Message msg, String args) {
9798
EmoteLevel.ERROR, sub.replace(NOT_ENOUGH_ARGS)).queue();
9899
} else {
99100
try {
100-
log.info(String.format("{%s %s} Executing %s.", FormatUtil.formatUser(msg.getAuthor()),
101+
log.info(String.format("{%s | %s} Executing %s.", FormatUtil.formatUser(msg.getAuthor()),
101102
FormatUtil.formatChannel(msg.getChannel()), cmd.getName()));
102103
cmd.execute(msg, argsList);
103104
} catch (Exception e) {
104-
log.warn(String.format("{%s %s} Could not execute %s.", FormatUtil.formatUser(msg.getAuthor()),
105+
log.warn(String.format("{%s | %s} Could not execute %s.", FormatUtil.formatUser(msg.getAuthor()),
105106
FormatUtil.formatChannel(msg.getChannel()), cmd.getName()));
106107
throw new ExecutionException(e);
107108
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.raxixor.jdaddons.command;
2+
3+
public enum PermissionLevel {
4+
5+
SELF((short) -1),
6+
OWNER((short) 0),
7+
CO_OWNER((short) 1),
8+
ADMIN((short) 2),
9+
MOD((short) 3),
10+
TRUSTED((short) 4),
11+
USER((short) 5),
12+
MUTED((short) 6);
13+
14+
private final short permLevel;
15+
PermissionLevel(short permLevel) {
16+
this.permLevel = permLevel;
17+
}
18+
19+
public static PermissionLevel getPermissionLevel(short lvl) {
20+
switch (lvl) {
21+
case -1:
22+
return SELF;
23+
case 0:
24+
return OWNER;
25+
case 1:
26+
return CO_OWNER;
27+
case 2:
28+
return ADMIN;
29+
case 3:
30+
return MOD;
31+
case 4:
32+
return TRUSTED;
33+
case 5:
34+
return USER;
35+
case 6:
36+
return MUTED;
37+
default:
38+
return USER;
39+
}
40+
}
41+
42+
public static PermissionLevel getPermissionLevel(long lvl) {
43+
return getPermissionLevel((short) lvl);
44+
}
45+
46+
public static boolean isHigherThan(PermissionLevel source, PermissionLevel target) {
47+
if (source == null && target == null)
48+
return false;
49+
if (source == null)
50+
source = USER;
51+
if (target == null)
52+
target = USER;
53+
54+
return source.permLevel < target.permLevel;
55+
}
56+
57+
public boolean isHigherThan(PermissionLevel target) {
58+
if (target == null)
59+
target = USER;
60+
61+
return this.permLevel < target.permLevel;
62+
}
63+
64+
public static boolean isHigherThanOrEqual(PermissionLevel source, PermissionLevel target) {
65+
if (source == null && target == null)
66+
return false;
67+
if (source == null)
68+
source = USER;
69+
if (target == null)
70+
target = USER;
71+
72+
return source.permLevel <= target.permLevel;
73+
}
74+
75+
public boolean isHigherThanOrEqual(PermissionLevel target) {
76+
if (target == null)
77+
target = USER;
78+
79+
return this.permLevel <= target.permLevel;
80+
}
81+
82+
public static boolean isLowerThan(PermissionLevel source, PermissionLevel target) {
83+
if (source == null && target == null)
84+
return false;
85+
if (source == null)
86+
source = USER;
87+
if (target == null)
88+
target = USER;
89+
90+
return target.permLevel < source.permLevel;
91+
}
92+
93+
public boolean isLowerThan(PermissionLevel target) {
94+
if (target == null)
95+
target = USER;
96+
97+
return target.permLevel < this.permLevel;
98+
}
99+
100+
public static boolean isLowerThanOrEqual(PermissionLevel source, PermissionLevel target) {
101+
if (source == null && target == null)
102+
return false;
103+
if (source == null)
104+
source = USER;
105+
if (target == null)
106+
target = USER;
107+
108+
return target.permLevel <= source.permLevel;
109+
}
110+
111+
public boolean isLowerThanOrEqual(PermissionLevel target) {
112+
if (target == null)
113+
target = USER;
114+
115+
return target.permLevel <= this.permLevel;
116+
}
117+
118+
@Override
119+
public String toString() {
120+
switch (this.permLevel) {
121+
case -1:
122+
return "Self (-1)";
123+
case 0:
124+
return "Owner (0)";
125+
case 1:
126+
return "Co-Owner (1)";
127+
case 2:
128+
return "Admin (2)";
129+
case 3:
130+
return "Moderator (3)";
131+
case 4:
132+
return "Trusted (4)";
133+
case 5:
134+
return "User (5)";
135+
case 6:
136+
return "Muted (6)";
137+
default:
138+
return "User (5)";
139+
}
140+
}
141+
142+
public short getPermLevel() {
143+
return permLevel;
144+
}
145+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.raxixor.jdaddons.command.group;
2+
3+
import com.raxixor.jdaddons.command.PermissionLevel;
4+
import net.dv8tion.jda.core.entities.Message;
5+
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
public abstract class CommandGroup {
10+
11+
public abstract void execute(SubCommand sCmd, Message trig, List<String> args);
12+
13+
public abstract Set<SubCommand> getSubCommands();
14+
15+
public SubCommand getSubCommand(String trigger) {
16+
for (SubCommand cmd : getSubCommands()) {
17+
if (cmd.getTriggers().length > 1) {
18+
for (String trig : cmd.getTriggers()) {
19+
if (trig.equals(trigger))
20+
return cmd;
21+
}
22+
} else {
23+
if (trigger.equals(cmd.getTriggers()[0]))
24+
return cmd;
25+
}
26+
}
27+
return null;
28+
}
29+
30+
public CommandGroupDescription getDescription() {
31+
return getClass().getAnnotation(CommandGroupDescription.class);
32+
}
33+
34+
public String getGroupName() {
35+
return getDescription().groupName();
36+
}
37+
38+
public String getPrefix() {
39+
return getDescription().prefix();
40+
}
41+
42+
public PermissionLevel getLevel() {
43+
return getDescription().level();
44+
}
45+
46+
public boolean isLevelOverride() {
47+
return getDescription().levelOverride();
48+
}
49+
50+
public boolean hasSubCommand(String trigger) {
51+
for (SubCommand cmd : getSubCommands()) {
52+
if (cmd.getTriggers().length > 1)
53+
for (String tr : cmd.getTriggers())
54+
if (tr.equals(trigger))
55+
return true;
56+
else
57+
if (trigger.equals(cmd.getTriggers()[0]))
58+
return true;
59+
}
60+
return false;
61+
}
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.raxixor.jdaddons.command.group;
2+
3+
import com.raxixor.jdaddons.command.PermissionLevel;
4+
5+
import java.lang.annotation.*;
6+
7+
@Retention(RetentionPolicy.RUNTIME)
8+
@Target(ElementType.TYPE)
9+
public @interface CommandGroupDescription {
10+
11+
String groupName();
12+
13+
String prefix();
14+
15+
PermissionLevel level() default PermissionLevel.USER;
16+
boolean levelOverride() default false;
17+
}

0 commit comments

Comments
 (0)