package cc.fascinated.bat.features.welcomer; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import lombok.AllArgsConstructor; import lombok.Getter; import java.util.Date; /** * @author Fascinated (fascinated7) */ @AllArgsConstructor @Getter public enum WelcomerPlaceholders { USER_MENTION("{user_mention}", BatUser.class) { @Override public String replacePlaceholder(Object object) { return ((BatUser) object).getDiscordUser().getAsMention(); } }, USER_NAME("{user_name}", BatUser.class) { @Override public String replacePlaceholder(Object object) { return ((BatUser) object).getName(); } }, GUILD_NAME("{guild_name}", BatGuild.class) { @Override public String replacePlaceholder(Object object) { return ((BatGuild) object).getName(); } }, JOIN_DATE("{join_date}", null) { @Override public String replacePlaceholder(Object object) { return "".formatted(new Date().toInstant().getEpochSecond()); } }; /** * The placeholder string that will get replaced */ private final String placeholder; /** * The class that the placeholder is associated with */ private final Class clazz; /** * Replaces the placeholder with the string based on the overridden method * * @param object The object to replace the placeholder with * @return The string with the placeholder replaced */ public String replacePlaceholder(Object object) { if (clazz != null && !clazz.isInstance(object)) { throw new IllegalArgumentException("Object is not an instance of " + clazz.getName()); } return null; } /** * Replaces all placeholders in a message with the objects provided * * @param message The message to replace the placeholders in * @param objects The objects to replace the placeholders with * @return The message with the placeholders replaced */ public static String replaceAllPlaceholders(String message, Object... objects) { for (WelcomerPlaceholders placeholder : values()) { for (Object object : objects) { if (placeholder.getClazz() != null && !placeholder.getClazz().isInstance(object)) { continue; } message = message.replace(placeholder.getPlaceholder(), placeholder.replacePlaceholder(object)); } } return message; } }