Hopefully finished help command

This commit is contained in:
Stijn 2022-03-09 22:37:44 +01:00
parent 9f1f62d593
commit ba55956bbf
6 changed files with 91 additions and 24 deletions

View File

@ -27,4 +27,11 @@ public class CommandManager extends ListenerAdapter {
String[] args = Arrays.copyOfRange(s, 1, s.length);
}
public List<DiscordCommand> getCommands() {
return commands;
}
public String getPrefix(long guildId) {
return commandPrefixes.getOrDefault(guildId, "!");
}
}

View File

@ -1,6 +1,7 @@
package com.alttd.commandManager;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import java.util.List;
@ -13,12 +14,16 @@ public abstract class DiscordCommand {
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 List<String> getAlias();
public String getExtendedHelpMessage() {
return getHelpMessage();
}
public abstract List<String> getAliases();
}

View File

@ -1,26 +1,72 @@
package com.alttd.commandManager.commands;
import com.alttd.AltitudeBot;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
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.PrivateChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import java.util.List;
import java.util.Optional;
public class CommandHelp extends DiscordCommand {
private final CommandManager commandManager;
public CommandHelp(CommandManager commandManager) {
this.commandManager = commandManager;
}
@Override
public String getName() {
return "help";
}
@Override
public String execute(String[] args, Member commandSource) {
return null;
public String execute(String[] args, Member commandSource, TextChannel textChannel) {
return execute(args, textChannel, commandSource.getIdLong(), textChannel.getGuild().getIdLong(), Util.getGroupIds(commandSource));
}
@Override
public String execute(String[] args, User commandSource) {
return null;
public String execute(String[] args, User commandSource, TextChannel textChannel) {
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
@ -29,7 +75,7 @@ public class CommandHelp extends DiscordCommand {
}
@Override
public List<String> getAlias() {
public List<String> getAliases() {
return null;
}
}

View File

@ -21,4 +21,10 @@ public class MessagesConfig extends AbstractConfig {
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() {
}
}

View File

@ -24,28 +24,16 @@ public class PermissionManager {
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();
if (textChannel instanceof PrivateChannel) {
if (isDisabled(privateEnabledCommands, permission))
return false;
return hasPermission(user.getIdLong(), null, permission);
} else {
Logger.warning("Using user for Guild channel % ", textChannel.getAsMention());
return false;
if (isDisabled(channelEnabledCommands.get(textChannel.getIdLong()), permission.toLowerCase()))
return false;
}
}
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);
return hasPermission(userId, groupIds, permission);
}
private boolean isDisabled(List<String> enabledCommandList, String permission) {

View 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());
}
}