Skip to content

Commit fdafa7c

Browse files
committed
Changed certain command permissions
1 parent b850944 commit fdafa7c

File tree

8 files changed

+57
-14
lines changed

8 files changed

+57
-14
lines changed

src/main/java/com/github/bustedearlobes/themis/commands/ClearCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ClearCommand extends Command {
1717
private static final int DEFAULT_CLEAR_NUMBER = 100;
1818

1919
public ClearCommand() {
20-
super("clear", REGEX);
20+
super("clear", REGEX, true);
2121
}
2222

2323
@Override

src/main/java/com/github/bustedearlobes/themis/commands/Command.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
public abstract class Command {
1313
private Pattern pattern;
1414
private String commandName;
15+
private boolean requiresOwner;
1516

16-
public Command(String commandName, String regexValidation) {
17+
public Command(String commandName, String regexValidation, boolean requiresOwner) {
1718
pattern = Pattern.compile(regexValidation);
19+
this.requiresOwner = requiresOwner;
1820
this.commandName = commandName;
1921
}
2022

@@ -69,6 +71,10 @@ public void printUsage(TextChannel textChannel) {
6971
textChannel.sendMessage("Usage:\n\n" + getFullCommandManual().toString()).complete();
7072
}
7173

74+
public boolean requiresOwner() {
75+
return requiresOwner;
76+
}
77+
7278
protected abstract void onCall(Matcher fullCommand, Message message, JDA jda, Themis themis);
7379

7480
}

src/main/java/com/github/bustedearlobes/themis/commands/CommandListener.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,28 @@ public List<Command> getCommands() {
3131

3232
@Override
3333
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
34-
if(event.getMember().isOwner()) {
35-
String messageContent = event.getMessage().getContent();
36-
if(messageContent.trim().startsWith(COMMAND_BASE)) {
37-
messageContent = messageContent.trim().replaceFirst(COMMAND_BASE, "");
38-
String commandName = messageContent.trim().split(" ")[0];
39-
for(Command command: commands) {
40-
if(commandName.equals(command.getCommandName())) {
34+
String messageContent = event.getMessage().getContent();
35+
if(messageContent.trim().startsWith(COMMAND_BASE)) {
36+
messageContent = messageContent.trim().replaceFirst(COMMAND_BASE, "");
37+
String commandName = messageContent.trim().split(" ")[0];
38+
for(Command command: commands) {
39+
if(commandName.equals(command.getCommandName())) {
40+
if(command.requiresOwner() && event.getMember().isOwner()) {
41+
if(command.validateCall(messageContent)) {
42+
try {
43+
command.onCall(command.parseCommand(messageContent),
44+
event.getMessage(),
45+
event.getJDA(),
46+
themis);
47+
} catch(Exception e) {
48+
LOG.log(Level.WARNING,
49+
"There was an error while executing a command.",
50+
e);
51+
}
52+
} else {
53+
command.printUsage(event.getMessage().getTextChannel());
54+
}
55+
} else if(!command.requiresOwner()) {
4156
if(command.validateCall(messageContent)) {
4257
try {
4358
command.onCall(command.parseCommand(messageContent),

src/main/java/com/github/bustedearlobes/themis/commands/HelpCommand.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class HelpCommand extends Command {
1111
private static final String REGEX = "^help( (\\w+))?$";
1212

1313
public HelpCommand() {
14-
super("help", REGEX);
14+
super("help", REGEX, false);
1515
}
1616

1717
@Override
@@ -36,6 +36,28 @@ public void onCall(Matcher fullCommand, Message message, JDA jda, Themis themis)
3636
}
3737
message.getTextChannel().sendMessage(sb.toString()).complete();
3838
}
39+
} else {
40+
if(fullCommand.group(1) != null) {
41+
for(Command command : themis.getCommandListener().getCommands()) {
42+
if(command.getCommandName().equals(fullCommand.group(2)) && !command.requiresOwner()) {
43+
command.printUsage(message.getTextChannel());
44+
}
45+
}
46+
} else {
47+
StringBuilder sb = new StringBuilder(4000);
48+
sb.append("Commands:\n\n");
49+
for(Command command : themis.getCommandListener().getCommands()) {
50+
if(!command.requiresOwner()) {
51+
sb.append(command.getCommandManual()).append("\n");
52+
}
53+
}
54+
String outString = sb.toString();
55+
while(outString.length() >= 2000) {
56+
message.getTextChannel().sendMessage(outString.substring(0, 2000)).complete();
57+
outString = outString.substring(1999);
58+
}
59+
message.getTextChannel().sendMessage(sb.toString()).complete();
60+
}
3961
}
4062
}
4163

src/main/java/com/github/bustedearlobes/themis/commands/MusicCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class MusicCommand extends Command {
2626
private static final int CAPTURE_GROUP_SKIP = 3;
2727

2828
public MusicCommand() {
29-
super("music", REGEX);
29+
super("music", REGEX, false);
3030
}
3131

3232
@Override

src/main/java/com/github/bustedearlobes/themis/commands/MuteCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MuteCommand extends Command {
1919

2020

2121
public MuteCommand() {
22-
super("mute", REGEX);
22+
super("mute", REGEX, true);
2323
}
2424

2525
@Override

src/main/java/com/github/bustedearlobes/themis/commands/ShutdownCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ShutdownCommand extends Command {
1313
private static final String REGEX = "^shutdown$";
1414

1515
public ShutdownCommand() {
16-
super("shutdown", REGEX);
16+
super("shutdown", REGEX, true);
1717
}
1818

1919
@Override

src/main/java/com/github/bustedearlobes/themis/commands/UnmuteCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class UnmuteCommand extends Command {
1818

1919

2020
public UnmuteCommand() {
21-
super("unmute", REGEX);
21+
super("unmute", REGEX, true);
2222
}
2323

2424
@Override

0 commit comments

Comments
 (0)