ModBot/src/main/java/tech/nevets/modbot/api/commands/CommandRegistry.java
2022-09-20 12:36:54 -04:00

154 lines
5.0 KiB
Java

package tech.nevets.modbot.api.commands;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.nevets.modbot.CoreBot;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class CommandRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandRegistry.class);
private final List<ICommand> commands = new ArrayList<>();
private final List<IPrefixCommand> prefixCommands = new ArrayList<>();
private final List<ISlashCommand> slashCommands = new ArrayList<>();
public CommandRegistry() {}
public void addCommand(ICommand cmd) {
boolean nameFound = commands.stream().anyMatch(it -> it.getName().equalsIgnoreCase(cmd.getName()));
if (nameFound) {
throw new IllegalArgumentException("A command with that name is already present!");
}
commands.add(cmd);
if (cmd instanceof IPrefixCommand) {
prefixCommands.add((IPrefixCommand) cmd);
}
if (cmd instanceof ISlashCommand) {
slashCommands.add((ISlashCommand) cmd);
}
}
public void registerSlashCommands() {
if (slashCommands.size() > 0) {
List<CommandData> cmdDataList = new ArrayList<>();
for (ISlashCommand slashCmd : slashCommands) {
cmdDataList.add(slashCmd.getCommandData());
}
CoreBot.jda.updateCommands().addCommands(cmdDataList).queue();
}
}
@Nullable
public IPrefixCommand getPrefixCommand(String search) {
String searchLower = search.toLowerCase();
for (IPrefixCommand cmd : prefixCommands) {
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
return cmd;
}
}
LOGGER.warn("Returned null for command search: " + searchLower);
return null;
}
@Nullable
public ISlashCommand getSlashCommand(String search) {
String searchLower = search.toLowerCase();
for (ISlashCommand cmd : slashCommands) {
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
return cmd;
}
}
return null;
}
public List<ICommand> getCommands() {
return commands;
}
public List<IPrefixCommand> getPrefixCommands() {
return prefixCommands;
}
public List<ISlashCommand> getSlashCommands() {
return slashCommands;
}
public boolean hasPermission(Member member, ICommand cmd) {
List<Role> memberRoles = member.getRoles();
int permLevel = 0;
for (Role memberRole : memberRoles) {
switch (memberRole.getName().toLowerCase()) {
case "helper", "1":
if (permLevel < 1) permLevel = 1;
continue;
case "moderator", "mod", "2":
if (permLevel < 2) permLevel = 2;
continue;
case "administrator", "admin", "3":
if (permLevel < 3) permLevel = 3;
continue;
case "owner", "4":
if (permLevel < 4) permLevel = 4;
}
}
return permLevel >= cmd.getPermissionLevel();
}
public void handle(GuildMessageReceivedEvent event) {
String[] split = event.getMessage().getContentRaw()
.replaceFirst("(?i)" + Pattern.quote(CoreBot.CORE_CONFIG.getConfig().getString("bot.prefix")), "")
.split("\\s+");
String invoke = split[0].toLowerCase();
IPrefixCommand cmd = getPrefixCommand(invoke);
assert event.getMember() != null;
assert cmd != null;
if (!hasPermission(event.getMember(), cmd)) {
event.getMessage().reply("You do not have permission to use this command.").queue();
return;
}
event.getChannel().sendTyping().queue();
List<String> args = Arrays.asList(split).subList(1, split.length);
CommandContext ctx = new CommandContext(event, args);
cmd.handle(ctx);
}
public void handle(SlashCommandEvent event) {
String invoke = event.getName();
ISlashCommand cmd = getSlashCommand(invoke);
if (cmd != null) {
event.deferReply().queue();
assert event.getMember() != null;
if (!hasPermission(event.getMember(), cmd)) {
event.getHook().sendMessage("You do not have permission to use this command.").queue();
return;
}
CommandContext ctx = new CommandContext(event);
cmd.handleSlash(ctx);
}
}
}