package org.royaldev.thehumanity.commands.impl;
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.NotNull;
import org.kitteh.irc.client.library.element.User;
import org.kitteh.irc.client.library.event.helper.ActorEvent;
import org.royaldev.thehumanity.TheHumanity;
import xyz.cardstock.cardstock.commands.BaseCommand;
import xyz.cardstock.cardstock.commands.CallInfo;
import xyz.cardstock.cardstock.commands.Command;
import xyz.cardstock.cardstock.configuration.Server;
import java.util.List;
import java.util.stream.Collectors;
@Command(
name = "help",
description = "Gets the help for all commands."
)
public class HelpCommand extends BaseCommand {
private static final String GIST_ID = "thehumanity:help";
private final TheHumanity humanity;
public HelpCommand(final TheHumanity instance) {
this.humanity = instance;
}
/**
* Gets the names of all commands, in alphabetical order, concatenated together with no delimiter. Used for a cache
* String by the {@link org.royaldev.thehumanity.TheHumanity#cachedGist TheHumanity#gist()} method.
*
* @return String, as specified above
*/
private String getNames() {
return this.humanity.getCommandRegistrar().all().stream().map(BaseCommand::getName).sorted().collect(Collectors.joining());
}
@Override
public void run(@NotNull ActorEvent<User> event, @NotNull CallInfo callInfo, @NotNull List<String> arguments) {
final User u = event.getActor();
final StringBuilder sb = new StringBuilder();
final Server server = this.humanity.getClientServerMap().get(event.getClient());
Preconditions.checkNotNull(server);
for (final BaseCommand ic : this.humanity.getCommandRegistrar().all()) {
sb.append("## ").append(server.getPrefix()).append(ic.getName()).append("\n");
sb.append("*").append(ic.getDescription()).append("* \n");
if (!"<command>".equalsIgnoreCase(ic.getUsage())) {
sb.append("**Usage:** ").append(ic.getUsage().replace("<command>", ic.getName())).append(" \n");
}
if (ic.getAliases().length > 0) {
sb.append("**Aliases:** ").append(String.join(", ", ic.getAliases())).append("\n");
}
}
u.sendNotice(this.humanity.cachedGist(HelpCommand.GIST_ID, this.getNames(), "help.md", sb.toString()));
}
}