diff --git a/src/main/java/sciwhiz12/janitor/msg/MessageBuilder.java b/src/main/java/sciwhiz12/janitor/msg/MessageBuilder.java deleted file mode 100644 index 1c4fb86..0000000 --- a/src/main/java/sciwhiz12/janitor/msg/MessageBuilder.java +++ /dev/null @@ -1,102 +0,0 @@ -package sciwhiz12.janitor.msg; - -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.MessageEmbed; -import sciwhiz12.janitor.JanitorBot; - -import java.util.HashMap; -import java.util.Map; -import java.util.function.Consumer; -import java.util.function.Supplier; -import java.util.function.UnaryOperator; - -import static sciwhiz12.janitor.msg.Substitutions.substitute; - -public class MessageBuilder { - private final EmbedBuilder embedBuilder; - private final Map> substitutions; - - public MessageBuilder(EmbedBuilder embedBuilder, Map> substitutions) { - this.embedBuilder = embedBuilder; - this.substitutions = substitutions; - } - - public MessageBuilder() { - this(new EmbedBuilder(), new HashMap<>()); - } - - public MessageBuilder(MessageBuilder copy) { - this(new EmbedBuilder(copy.embedBuilder), new HashMap<>(copy.substitutions)); - } - - public EmbedBuilder embed() { - return embedBuilder; - } - - public MessageBuilder embed(Consumer operator) { - operator.accept(embed()); - return this; - } - - public MessageBuilder apply(Consumer consumer) { - consumer.accept(this); - return this; - } - - public MessageBuilder with(final String argument, final Supplier value) { - substitutions.put(argument, value); - return this; - } - - public MessageBuilder field(final String head, final boolean inline) { - embedBuilder.addField(head + ".name", head + ".value", inline); - return this; - } - - public MessageBuilder blankField(final boolean inline) { - embedBuilder.addBlankField(inline); - return this; - } - - public MessageEmbed build(Translations translations, Substitutions substitutions) { - EmbedBuilder realEmbed = new EmbedBuilder(); - MessageEmbed tempEmbed = embed().build(); - final Map> replaceMap = substitutions.createDefaultedMap(this.substitutions); - final UnaryOperator replacer = str -> substitute(translations.translate(str), replaceMap); - - realEmbed.setColor(tempEmbed.getColorRaw()); - realEmbed.setTimestamp(tempEmbed.getTimestamp()); - if (tempEmbed.getTitle() != null) - realEmbed.setTitle(replacer.apply(tempEmbed.getTitle()), tempEmbed.getUrl()); - if (tempEmbed.getThumbnail() != null) - realEmbed.setThumbnail(tempEmbed.getThumbnail().getUrl()); - if (tempEmbed.getAuthor() != null) - realEmbed.setAuthor( - replacer.apply(tempEmbed.getAuthor().getName()), - tempEmbed.getAuthor().getUrl(), - tempEmbed.getAuthor().getIconUrl() - ); - if (tempEmbed.getFooter() != null) - realEmbed.setFooter( - replacer.apply(tempEmbed.getFooter().getText()), - tempEmbed.getFooter().getIconUrl() - ); - if (tempEmbed.getImage() != null) - realEmbed.setImage(tempEmbed.getImage().getUrl()); - if (tempEmbed.getDescription() != null) - realEmbed.setDescription(replacer.apply(tempEmbed.getDescription())); - - for (MessageEmbed.Field field : tempEmbed.getFields()) - realEmbed.addField( - replacer.apply(field.getName()), - replacer.apply(field.getValue()), - field.isInline() - ); - - return realEmbed.build(); - } - - public MessageEmbed build(JanitorBot bot) { - return build(bot.getTranslations(), bot.getSubstitutions()); - } -} diff --git a/src/main/java/sciwhiz12/janitor/msg/RegularMessageBuilder.java b/src/main/java/sciwhiz12/janitor/msg/RegularMessageBuilder.java new file mode 100644 index 0000000..76dab18 --- /dev/null +++ b/src/main/java/sciwhiz12/janitor/msg/RegularMessageBuilder.java @@ -0,0 +1,53 @@ +package sciwhiz12.janitor.msg; + +import net.dv8tion.jda.api.entities.MessageChannel; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.requests.restaction.MessageAction; +import sciwhiz12.janitor.JanitorBot; +import sciwhiz12.janitor.msg.json.RegularMessage; +import sciwhiz12.janitor.msg.substitution.SubstitutionMap; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; + +public class RegularMessageBuilder { + private final RegularMessage message; + private final Map> customSubstitutions; + + public RegularMessageBuilder(RegularMessage message, Map> customSubstitutions) { + this.message = message; + this.customSubstitutions = customSubstitutions; + } + + public RegularMessageBuilder(RegularMessage message) { + this(message, new HashMap<>()); + } + + public RegularMessageBuilder(RegularMessageBuilder copy) { + this(copy.message, new HashMap<>(copy.customSubstitutions)); + } + + public RegularMessageBuilder apply(Consumer consumer) { + consumer.accept(this); + return this; + } + + public RegularMessageBuilder with(final String argument, final Supplier value) { + customSubstitutions.put(argument, value); + return this; + } + + public MessageEmbed build(TranslationMap translations, SubstitutionMap substitutions) { + return message.create(translations, substitutions.with(customSubstitutions)).build(); + } + + public MessageEmbed build(JanitorBot bot) { + return build(bot.getTranslations(), bot.getSubstitutions()); + } + + public MessageAction send(JanitorBot bot, MessageChannel channel) { + return channel.sendMessage(build(bot)); + } +}