Hopefully finished help command
This commit is contained in:
parent
9f1f62d593
commit
ba55956bbf
|
|
@ -27,4 +27,11 @@ public class CommandManager extends ListenerAdapter {
|
||||||
String[] args = Arrays.copyOfRange(s, 1, s.length);
|
String[] args = Arrays.copyOfRange(s, 1, s.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<DiscordCommand> getCommands() {
|
||||||
|
return commands;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrefix(long guildId) {
|
||||||
|
return commandPrefixes.getOrDefault(guildId, "!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.alttd.commandManager;
|
package com.alttd.commandManager;
|
||||||
|
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.TextChannel;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -13,12 +14,16 @@ public abstract class DiscordCommand {
|
||||||
return "command." + getName();
|
return "command." + getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract String execute(String[] args, Member commandSource);
|
public abstract String execute(String[] args, Member commandSource, TextChannel textChannel);
|
||||||
|
|
||||||
public abstract String execute(String[] args, User commandSource);
|
public abstract String execute(String[] args, User commandSource, TextChannel textChannel);
|
||||||
|
|
||||||
public abstract String getHelpMessage();
|
public abstract String getHelpMessage();
|
||||||
|
|
||||||
public abstract List<String> getAlias();
|
public String getExtendedHelpMessage() {
|
||||||
|
return getHelpMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract List<String> getAliases();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,72 @@
|
||||||
package com.alttd.commandManager.commands;
|
package com.alttd.commandManager.commands;
|
||||||
|
|
||||||
|
import com.alttd.AltitudeBot;
|
||||||
|
import com.alttd.commandManager.CommandManager;
|
||||||
import com.alttd.commandManager.DiscordCommand;
|
import com.alttd.commandManager.DiscordCommand;
|
||||||
import com.alttd.config.MessagesConfig;
|
import com.alttd.config.MessagesConfig;
|
||||||
|
import com.alttd.permissions.PermissionManager;
|
||||||
|
import com.alttd.templates.Parser;
|
||||||
|
import com.alttd.templates.Template;
|
||||||
|
import com.alttd.util.Logger;
|
||||||
|
import com.alttd.util.Util;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.PrivateChannel;
|
||||||
|
import net.dv8tion.jda.api.entities.TextChannel;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class CommandHelp extends DiscordCommand {
|
public class CommandHelp extends DiscordCommand {
|
||||||
|
|
||||||
|
private final CommandManager commandManager;
|
||||||
|
|
||||||
|
public CommandHelp(CommandManager commandManager) {
|
||||||
|
this.commandManager = commandManager;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "help";
|
return "help";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String execute(String[] args, Member commandSource) {
|
public String execute(String[] args, Member commandSource, TextChannel textChannel) {
|
||||||
return null;
|
return execute(args, textChannel, commandSource.getIdLong(), textChannel.getGuild().getIdLong(), Util.getGroupIds(commandSource));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String execute(String[] args, User commandSource) {
|
public String execute(String[] args, User commandSource, TextChannel textChannel) {
|
||||||
return null;
|
if (!(textChannel instanceof PrivateChannel))
|
||||||
|
Logger.warning("Using User when executing command on Member: % Command: %", commandSource.getAsMention(), getName());
|
||||||
|
return execute(args, textChannel, commandSource.getIdLong(), 0, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String execute(String[] args, TextChannel textChannel, long userId, long guildId, List<Long> groupIds) {
|
||||||
|
PermissionManager permissionManager = AltitudeBot.getInstance().getPermissionManager();
|
||||||
|
StringBuilder helpMessage = new StringBuilder();
|
||||||
|
if (args.length == 0) {
|
||||||
|
commandManager.getCommands().stream()
|
||||||
|
.filter(command -> permissionManager.hasPermission(
|
||||||
|
textChannel,
|
||||||
|
userId,
|
||||||
|
groupIds,
|
||||||
|
command.getPermission()))
|
||||||
|
.forEach(command -> helpMessage.append(command.getHelpMessage()));
|
||||||
|
} else {
|
||||||
|
String arg = args[0].toLowerCase();
|
||||||
|
Optional<DiscordCommand> first = commandManager.getCommands().stream()
|
||||||
|
.filter(command -> command.getName().equals(arg)
|
||||||
|
|| command.getAliases().contains(arg)).findFirst();
|
||||||
|
if (first.isEmpty())
|
||||||
|
return Parser.parse(MessagesConfig.INVALID_COMMAND_ARGS,
|
||||||
|
Template.of("args", arg),
|
||||||
|
Template.of("command", getName()),
|
||||||
|
Template.of("prefix", commandManager.getPrefix(guildId)));
|
||||||
|
DiscordCommand discordCommand = first.get();
|
||||||
|
helpMessage.append(discordCommand.getExtendedHelpMessage());
|
||||||
|
}
|
||||||
|
return Parser.parse(MessagesConfig.HELP_MESSAGE_TEMPLATE, Template.of("commands", helpMessage.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -29,7 +75,7 @@ public class CommandHelp extends DiscordCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getAlias() {
|
public List<String> getAliases() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,4 +21,10 @@ public class MessagesConfig extends AbstractConfig {
|
||||||
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
|
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String INVALID_COMMAND = "<command> is not a valid command.";
|
||||||
|
public static String INVALID_COMMAND_ARGS = "`<args>` is/are not valid argument(s) for `<command>`.\nFor more info see <prefix>help <command>";
|
||||||
|
private static void loadInvalidCommands() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,28 +24,16 @@ public class PermissionManager {
|
||||||
this.privateEnabledCommands = privateEnabledCommands;
|
this.privateEnabledCommands = privateEnabledCommands;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasPermission(TextChannel textChannel, User user, String permission) {
|
public boolean hasPermission(TextChannel textChannel, long userId, List<Long> groupIds, String permission) {
|
||||||
permission = permission.toLowerCase();
|
permission = permission.toLowerCase();
|
||||||
if (textChannel instanceof PrivateChannel) {
|
if (textChannel instanceof PrivateChannel) {
|
||||||
if (isDisabled(privateEnabledCommands, permission))
|
if (isDisabled(privateEnabledCommands, permission))
|
||||||
return false;
|
return false;
|
||||||
return hasPermission(user.getIdLong(), null, permission);
|
|
||||||
} else {
|
} else {
|
||||||
Logger.warning("Using user for Guild channel % ", textChannel.getAsMention());
|
if (isDisabled(channelEnabledCommands.get(textChannel.getIdLong()), permission.toLowerCase()))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
return hasPermission(userId, groupIds, permission);
|
||||||
|
|
||||||
public boolean hasPermission(TextChannel textChannel, Member member, String permission) {
|
|
||||||
permission = permission.toLowerCase();
|
|
||||||
if (isDisabled(channelEnabledCommands.get(textChannel.getIdLong()), permission.toLowerCase()))
|
|
||||||
return false;
|
|
||||||
return hasPermission(
|
|
||||||
member.getIdLong(),
|
|
||||||
member.getRoles().stream()
|
|
||||||
.map(Role::getIdLong)
|
|
||||||
.collect(Collectors.toList()),
|
|
||||||
permission);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isDisabled(List<String> enabledCommandList, String permission) {
|
private boolean isDisabled(List<String> enabledCommandList, String permission) {
|
||||||
|
|
|
||||||
15
src/main/java/com/alttd/util/Util.java
Normal file
15
src/main/java/com/alttd/util/Util.java
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.alttd.util;
|
||||||
|
|
||||||
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.Role;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class Util {
|
||||||
|
public static List<Long> getGroupIds(Member member) {
|
||||||
|
return member.getRoles().stream()
|
||||||
|
.map(Role::getIdLong)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user