package cc.fascinated.bat.features.welcomer; import cc.fascinated.bat.common.ChannelUtils; import cc.fascinated.bat.common.Serializable; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import com.google.gson.Gson; import lombok.Getter; import lombok.Setter; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import org.bson.Document; /** * @author Fascinated (fascinated7) */ @Getter @Setter public class WelcomerProfile extends Serializable { /** * The welcomer message, null if we're using an embed */ private WelcomerMessage welcomerMessage; /** * The welcomer embed, null if we're using a message */ private WelcomerEmbed welcomerEmbed; /** * The channel to send the welcomer messages to */ private TextChannel channel; /** * Gets the welcomer message * * @return The welcomer message, false if we're using an embed */ public boolean isEmbed() { return welcomerEmbed != null; } /** * Gets the welcomer message * * @return The welcomer message, false if we're using an embed */ public boolean isMessage() { return welcomerMessage != null; } /** * Sets the welcomer message *

* This will disable the embed if it's enabled *

*/ public void setMessage(String message) { welcomerMessage = new WelcomerMessage(message); welcomerEmbed = null; } /** * Sets the welcomer embed *

* This will disable the message if it's enabled *

*/ public void setEmbed(String title, String description, String color, boolean pingBeforeSend) { welcomerEmbed = new WelcomerEmbed(title, description, color, pingBeforeSend); welcomerMessage = null; } /** * Sends the welcome message to the user * * @param guild The guild to send the message in * @param user The user to send the message to */ public void sendWelcomeMessage(BatGuild guild, BatUser user) { if (this.channel == null || (!this.isMessage() && !this.isEmbed())) { return; } if (welcomerEmbed != null) { if (welcomerEmbed.isPingBeforeSend()) { // Ping the user before sending the message this.channel.sendMessage(user.getDiscordUser().getAsMention()).queue(); } this.channel.sendMessageEmbeds(welcomerEmbed.buildEmbed(guild, user).build()).queue(); return; } if (welcomerMessage != null) { this.channel.sendMessage(welcomerMessage.buildMessage(guild, user)).queue(); } } @Override public void load(Document document, Gson gson) { Document welcomerMessageDocument = document.get("welcomerMessage", Document.class); if (welcomerMessageDocument != null) { welcomerMessage = new WelcomerMessage( welcomerMessageDocument.getString("message") ); } Document welcomerEmbedDocument = document.get("welcomerEmbed", Document.class); if (welcomerEmbedDocument != null) { welcomerEmbed = new WelcomerEmbed( welcomerEmbedDocument.getString("title"), welcomerEmbedDocument.getString("description"), welcomerEmbedDocument.getString("color"), welcomerEmbedDocument.getBoolean("pingBeforeSend") ); } String channelId = document.getString("channelId"); if (channelId != null) { TextChannel textChannel = ChannelUtils.getTextChannel(channelId); if (textChannel != null) { channel = textChannel; } } } @Override public Document serialize(Gson gson) { Document document = new Document(); if (welcomerMessage != null) { document.put("welcomerMessage", new Document("message", welcomerMessage.getMessage())); } if (welcomerEmbed != null) { document.put("welcomerEmbed", new Document() .append("title", welcomerEmbed.getTitle()) .append("description", welcomerEmbed.getDescription()) .append("color", welcomerEmbed.getColor()) .append("pingBeforeSend", welcomerEmbed.isPingBeforeSend()) ); } if (channel != null) { document.put("channelId", channel.getId()); } return document; } @Override public void reset() { welcomerMessage = null; welcomerEmbed = null; } }