package net.nevet5gi.buzzbot.commands.utils; 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 net.nevet5gi.buzzbot.BuzzBot; import net.nevet5gi.buzzbot.Config; import net.nevet5gi.buzzbot.commands.*; import net.nevet5gi.buzzbot.database.SqlDB; import net.nevet5gi.buzzbot.objects.GuildData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; public class CommandManager { private static final Logger LOGGER = LoggerFactory.getLogger(CommandManager.class); protected final List commands = new ArrayList<>(); protected static List slashList = new ArrayList<>(); public CommandManager() { //Add to this list in alphabetical order addCommand(new BanCmd()); addCommand(new BeeCmd()); addCommand(new HelpCmd(this)); addCommand(new MuteCmd()); addCommand(new PingCmd()); addCommand(new SetRoleCmd()); addCommand(new TestCmd()); addCommand(new UnbanCmd()); addCommand(new UnmuteCmd()); addCommand(new UnwarnCmd()); addCommand(new WarnCmd()); //addCommand(new CommandClass()); } private void addCommand(ICmdGeneric cmd) { boolean nameFound = commands.stream().anyMatch(it -> it.getName().equalsIgnoreCase(cmd.getName())); if (nameFound) { throw new IllegalArgumentException("A command with this name is already present"); } if (cmd instanceof ICommand) { commands.add((ICommand) cmd); } if (cmd instanceof ISlashCommand) { slashList.add(((ISlashCommand) cmd)); } } public static void registerSlashCommands() { if (slashList != null) { List cmdDataList = new ArrayList<>(); for (ISlashCommand slashCmd : slashList) { cmdDataList.add(slashCmd.getCommandData()); } BuzzBot.jda.updateCommands().addCommands(cmdDataList).queue(); } } public List getCommands() { return commands; } public List getSlashCommands() { return slashList; } @Nullable public ICommand getCommand(String search) { String searchLower = search.toLowerCase(); for (ICommand cmd : commands) { if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) { return cmd; } } return null; } public boolean hasPermission(Member member, ICmdGeneric cmd, GuildData guild) { List roles = member.getRoles(); int permLevel = 0; for (Role role : roles) { switch (role.getName().toLowerCase()) { case "helper": if (permLevel < 1) permLevel = 1; continue; case "moderator", "mod": if (permLevel < 2) permLevel = 2; continue; case "administrator", "admin": if (permLevel < 3) permLevel = 3; continue; case "owner": if (permLevel < 4) permLevel = 4; } if (role.getIdLong() == guild.getHelperId() && permLevel < 1) permLevel = 1; if (role.getIdLong() == guild.getModeratorId() && permLevel < 2) permLevel = 2; if (role.getIdLong() == guild.getAdministratorId() && permLevel < 3) permLevel = 3; if (role.getIdLong() == guild.getOwnerId() && permLevel < 4) permLevel = 4; if (member.isOwner() && permLevel < 4) permLevel = 4; } return permLevel >= cmd.getPermissionLevel(); } @Nullable public ISlashCommand getSlashCommand(String search) { String searchLower = search.toLowerCase(); for (ISlashCommand scmd : slashList) { if (scmd.getName().equals(searchLower) || scmd.getAliases().contains(searchLower)) { return scmd; } } return null; } public void handle(GuildMessageReceivedEvent event) { String[] split = event.getMessage().getContentRaw() .replaceFirst("(?i)" + Pattern.quote(Config.getConfig().getString("bot.prefix")), "") .split("\\s+"); String invoke = split[0].toLowerCase(); ICommand cmd = getCommand(invoke); SqlDB sql = new SqlDB(); GuildData guild = sql.getGuildData(event.getGuild().getIdLong()); sql.close(); if (!hasPermission(event.getMember(), cmd, guild)) { event.getMessage().reply("You do not have permission to use this command.").queue(); return; } if (cmd != null) { event.getChannel().sendTyping().queue(); List 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(); SqlDB sql = new SqlDB(); GuildData guild = sql.getGuildData(event.getGuild().getIdLong()); sql.close(); if (!hasPermission(event.getMember(), cmd, guild)) { event.getHook().sendMessage("You do not have permission to use this command.").queue(); return; } List args = new ArrayList<>(); CommandContext ctx = new CommandContext(event, args); cmd.handleSlash(ctx); } } }