Search in sources :

Example 1 with MessageFormat

use of github.scarsz.discordsrv.objects.MessageFormat in project DiscordSRV by Scarsz.

the class MessageFormatResolver method getMessageFromConfiguration.

public static MessageFormat getMessageFromConfiguration(DynamicConfig config, String key) {
    if (!config.getOptional(key).isPresent()) {
        return null;
    }
    Optional<Boolean> enabled = config.getOptionalBoolean(key + ".Enabled");
    if (enabled.isPresent() && !enabled.get()) {
        return null;
    }
    MessageFormat messageFormat = new MessageFormat();
    if (config.getOptional(key + ".Embed").isPresent() && config.getOptionalBoolean(key + ".Embed.Enabled").orElse(true)) {
        Optional<String> hexColor = config.getOptionalString(key + ".Embed.Color");
        if (hexColor.isPresent()) {
            String hex = hexColor.get().trim();
            if (!hex.startsWith("#"))
                hex = "#" + hex;
            if (hex.length() == 7) {
                messageFormat.setColor(new Color(Integer.valueOf(hex.substring(1, 3), 16), Integer.valueOf(hex.substring(3, 5), 16), Integer.valueOf(hex.substring(5, 7), 16)));
            } else {
                DiscordSRV.debug("Invalid color hex: " + hex + " (in " + key + ".Embed.Color)");
            }
        } else {
            config.getOptionalInt(key + ".Embed.Color").map(Color::new).ifPresent(messageFormat::setColor);
        }
        if (config.getOptional(key + ".Embed.Author").isPresent()) {
            config.getOptionalString(key + ".Embed.Author.Name").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setAuthorName);
            config.getOptionalString(key + ".Embed.Author.Url").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setAuthorUrl);
            config.getOptionalString(key + ".Embed.Author.ImageUrl").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setAuthorImageUrl);
        }
        config.getOptionalString(key + ".Embed.ThumbnailUrl").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setThumbnailUrl);
        config.getOptionalString(key + ".Embed.Title.Text").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setTitle);
        config.getOptionalString(key + ".Embed.Title.Url").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setTitleUrl);
        config.getOptionalString(key + ".Embed.Description").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setDescription);
        Optional<List<String>> fieldsOptional = config.getOptionalStringList(key + ".Embed.Fields");
        if (fieldsOptional.isPresent()) {
            List<MessageEmbed.Field> fields = new ArrayList<>();
            for (String s : fieldsOptional.get()) {
                if (s.contains(";")) {
                    String[] parts = s.split(";");
                    if (parts.length < 2) {
                        continue;
                    }
                    boolean inline = parts.length < 3 || Boolean.parseBoolean(parts[2]);
                    fields.add(new MessageEmbed.Field(parts[0], parts[1], inline, true));
                } else {
                    boolean inline = Boolean.parseBoolean(s);
                    fields.add(new MessageEmbed.Field("\u200e", "\u200e", inline, true));
                }
            }
            messageFormat.setFields(fields);
        }
        config.getOptionalString(key + ".Embed.ImageUrl").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setImageUrl);
        if (config.getOptional(key + ".Embed.Footer").isPresent()) {
            config.getOptionalString(key + ".Embed.Footer.Text").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setFooterText);
            config.getOptionalString(key + ".Embed.Footer.IconUrl").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setFooterIconUrl);
        }
        Optional<Boolean> timestampOptional = config.getOptionalBoolean(key + ".Embed.Timestamp");
        if (timestampOptional.isPresent()) {
            if (timestampOptional.get()) {
                messageFormat.setTimestamp(new Date().toInstant());
            }
        } else {
            Optional<Long> epochOptional = config.getOptionalLong(key + ".Embed.Timestamp");
            epochOptional.ifPresent(timestamp -> messageFormat.setTimestamp(new Date(timestamp).toInstant()));
        }
    }
    if (config.getOptional(key + ".Webhook").isPresent() && config.getOptionalBoolean(key + ".Webhook.Enable").orElse(false)) {
        messageFormat.setUseWebhooks(true);
        config.getOptionalString(key + ".Webhook.AvatarUrl").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setWebhookAvatarUrl);
        config.getOptionalString(key + ".Webhook.Name").filter(StringUtils::isNotBlank).ifPresent(messageFormat::setWebhookName);
    }
    Optional<String> content = config.getOptionalString(key + ".Content");
    if (content.isPresent() && StringUtils.isNotBlank(content.get())) {
        messageFormat.setContent(content.get());
    }
    return messageFormat.isAnyContent() ? messageFormat : null;
}
Also used : MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) Color(java.awt.Color) ChatColor(org.bukkit.ChatColor) ArrayList(java.util.ArrayList) Date(java.util.Date) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with MessageFormat

use of github.scarsz.discordsrv.objects.MessageFormat in project DiscordSRV by Scarsz.

the class DiscordSRV method sendLeaveMessage.

/**
 * Triggers a leave message for the given player to be sent to Discord. Useful for fake leave messages.
 *
 * @param player the player
 * @param quitMessage the leave/quit message (that is usually provided by Bukkit's {@link PlayerQuitEvent#getQuitMessage()})
 * @see #sendJoinMessage(Player, String)
 */
public void sendLeaveMessage(Player player, String quitMessage) {
    if (player == null)
        throw new IllegalArgumentException("player cannot be null");
    MessageFormat messageFormat = getMessageFromConfiguration("MinecraftPlayerLeaveMessage");
    if (messageFormat == null || !messageFormat.isAnyContent()) {
        debug("Not sending leave message due to it being disabled");
        return;
    }
    TextChannel textChannel = getOptionalTextChannel("leave");
    if (textChannel == null) {
        DiscordSRV.debug("Not sending quit message, text channel is null");
        return;
    }
    final String displayName = StringUtils.isNotBlank(player.getDisplayName()) ? MessageUtil.strip(player.getDisplayName()) : "";
    final String message = StringUtils.isNotBlank(quitMessage) ? quitMessage : "";
    final String name = player.getName();
    String avatarUrl = getAvatarUrl(player);
    String botAvatarUrl = DiscordUtil.getJda().getSelfUser().getEffectiveAvatarUrl();
    String botName = getMainGuild() != null ? getMainGuild().getSelfMember().getEffectiveName() : DiscordUtil.getJda().getSelfUser().getName();
    BiFunction<String, Boolean, String> translator = (content, needsEscape) -> {
        if (content == null)
            return null;
        content = content.replaceAll("%time%|%date%", TimeUtil.timeStamp()).replace("%message%", MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(message) : message)).replace("%username%", MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(name) : name)).replace("%displayname%", needsEscape ? DiscordUtil.escapeMarkdown(displayName) : displayName).replace("%usernamenoescapes%", name).replace("%displaynamenoescapes%", displayName).replace("%embedavatarurl%", avatarUrl).replace("%botavatarurl%", botAvatarUrl).replace("%botname%", botName);
        content = DiscordUtil.translateEmotes(content, textChannel.getGuild());
        content = PlaceholderUtil.replacePlaceholdersToDiscord(content, player);
        return content;
    };
    Message discordMessage = translateMessage(messageFormat, translator);
    if (discordMessage == null)
        return;
    String webhookName = translator.apply(messageFormat.getWebhookName(), false);
    String webhookAvatarUrl = translator.apply(messageFormat.getWebhookAvatarUrl(), false);
    if (messageFormat.isUseWebhooks()) {
        WebhookUtil.deliverMessage(textChannel, webhookName, webhookAvatarUrl, discordMessage.getContentRaw(), discordMessage.getEmbeds().stream().findFirst().orElse(null));
    } else {
        DiscordUtil.queueMessage(textChannel, discordMessage, true);
    }
}
Also used : SSLContext(javax.net.ssl.SSLContext) LoggerContext(org.apache.logging.log4j.core.LoggerContext) IOUtil(net.dv8tion.jda.internal.utils.IOUtil) Level(org.apache.logging.log4j.Level) DualStackMode(com.neovisionaries.ws.client.DualStackMode) StringUtils(org.apache.commons.lang3.StringUtils) GsonBuilder(com.google.gson.GsonBuilder) GatewayIntent(net.dv8tion.jda.api.requests.GatewayIntent) ChatHook(github.scarsz.discordsrv.hooks.chat.ChatHook) AdvancedPie(org.bstats.charts.AdvancedPie) GroupSynchronizationManager(github.scarsz.discordsrv.objects.managers.GroupSynchronizationManager) CommandSender(org.bukkit.command.CommandSender) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) github.scarsz.discordsrv.objects.threads(github.scarsz.discordsrv.objects.threads) AccountLinkManager(github.scarsz.discordsrv.objects.managers.AccountLinkManager) PlayerQuitEvent(org.bukkit.event.player.PlayerQuitEvent) WebSocketFactory(com.neovisionaries.ws.client.WebSocketFactory) net.dv8tion.jda.api(net.dv8tion.jda.api) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) HandlerList(org.bukkit.event.HandlerList) PluginLoader(org.bukkit.plugin.PluginLoader) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) java.util(java.util) VaultHook(github.scarsz.discordsrv.hooks.VaultHook) CloseCode(net.dv8tion.jda.api.requests.CloseCode) Supplier(java.util.function.Supplier) SQLException(java.sql.SQLException) DrilldownPie(org.bstats.charts.DrilldownPie) CacheFlag(net.dv8tion.jda.api.utils.cache.CacheFlag) ApiManager(github.scarsz.discordsrv.api.ApiManager) MemberCachePolicy(net.dv8tion.jda.api.utils.MemberCachePolicy) PluginHook(github.scarsz.discordsrv.hooks.PluginHook) PluginBase(org.bukkit.plugin.PluginBase) ConsoleAppender(github.scarsz.discordsrv.objects.log4j.ConsoleAppender) FileUtils(org.apache.commons.io.FileUtils) Field(java.lang.reflect.Field) RequireLinkModule(github.scarsz.discordsrv.modules.requirelink.RequireLinkModule) CheckReturnValue(javax.annotation.CheckReturnValue) OkHttpClient(okhttp3.OkHttpClient) CommandManager(github.scarsz.discordsrv.objects.managers.CommandManager) Record(org.minidns.record.Record) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) DynamicConfig(github.scarsz.configuralize.DynamicConfig) Plugin(org.bukkit.plugin.Plugin) LoginException(javax.security.auth.login.LoginException) Dynamic(alexh.weak.Dynamic) AlertListener(github.scarsz.discordsrv.modules.alerts.AlertListener) ShutdownEvent(net.dv8tion.jda.api.events.ShutdownEvent) BiFunction(java.util.function.BiFunction) Player(org.bukkit.entity.Player) SimplePie(org.bstats.charts.SimplePie) ChunkGenerator(org.bukkit.generator.ChunkGenerator) org.bukkit(org.bukkit) DnsMessage(org.minidns.dnsmessage.DnsMessage) java.net(java.net) Language(github.scarsz.configuralize.Language) Gson(com.google.gson.Gson) RateLimitedException(net.dv8tion.jda.api.exceptions.RateLimitedException) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction) HierarchyException(net.dv8tion.jda.api.exceptions.HierarchyException) JdbcAccountLinkManager(github.scarsz.discordsrv.objects.managers.link.JdbcAccountLinkManager) Method(java.lang.reflect.Method) github.scarsz.discordsrv.api.events(github.scarsz.discordsrv.api.events) PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) Lag(github.scarsz.discordsrv.objects.Lag) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) PatternSyntaxException(java.util.regex.PatternSyntaxException) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) java.util.concurrent(java.util.concurrent) PermissionDefault(org.bukkit.permissions.PermissionDefault) VanishHook(github.scarsz.discordsrv.hooks.vanish.VanishHook) Metrics(org.bstats.bukkit.Metrics) SingleLineChart(org.bstats.charts.SingleLineChart) Logger(java.util.logging.Logger) PluginCommand(org.bukkit.command.PluginCommand) Collectors(java.util.stream.Collectors) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) Nullable(org.jetbrains.annotations.Nullable) MetadataValue(org.bukkit.metadata.MetadataValue) Command(org.bukkit.command.Command) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) BukkitWorker(org.bukkit.scheduler.BukkitWorker) CancellationDetector(github.scarsz.discordsrv.objects.CancellationDetector) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Getter(lombok.Getter) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) ImmutableList(com.google.common.collect.ImmutableList) Component(net.kyori.adventure.text.Component) ParseException(github.scarsz.configuralize.ParseException) github.scarsz.discordsrv.listeners(github.scarsz.discordsrv.listeners) JdaFilter(github.scarsz.discordsrv.objects.log4j.JdaFilter) AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) OkHostnameVerifier(okhttp3.internal.tls.OkHostnameVerifier) github.scarsz.discordsrv.util(github.scarsz.discordsrv.util) ConsoleMessage(github.scarsz.discordsrv.objects.ConsoleMessage) RestAction(net.dv8tion.jda.api.requests.RestAction) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) Dns(okhttp3.Dns) FileAccountLinkManager(github.scarsz.discordsrv.objects.managers.link.FileAccountLinkManager) Consumer(java.util.function.Consumer) java.io(java.io) DnsClient(org.minidns.DnsClient) VoiceModule(github.scarsz.discordsrv.modules.voice.VoiceModule) MultiverseCoreHook(github.scarsz.discordsrv.hooks.world.MultiverseCoreHook) DigestUtils(org.apache.commons.codec.digest.DigestUtils) LogManager(org.apache.logging.log4j.LogManager) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) DnsMessage(org.minidns.dnsmessage.DnsMessage) ConsoleMessage(github.scarsz.discordsrv.objects.ConsoleMessage)

Example 3 with MessageFormat

use of github.scarsz.discordsrv.objects.MessageFormat in project DiscordSRV by Scarsz.

the class DiscordSRV method sendJoinMessage.

/**
 * Triggers a join message for the given player to be sent to Discord. Useful for fake join messages.
 *
 * @param player the player
 * @param joinMessage the join message (that is usually provided by Bukkit's {@link PlayerJoinEvent#getJoinMessage()})
 * @see #sendLeaveMessage(Player, String)
 */
public void sendJoinMessage(Player player, String joinMessage) {
    if (player == null)
        throw new IllegalArgumentException("player cannot be null");
    MessageFormat messageFormat = player.hasPlayedBefore() ? getMessageFromConfiguration("MinecraftPlayerJoinMessage") : getMessageFromConfiguration("MinecraftPlayerFirstJoinMessage");
    if (messageFormat == null || !messageFormat.isAnyContent()) {
        debug("Not sending join message due to it being disabled");
        return;
    }
    TextChannel textChannel = getOptionalTextChannel("join");
    if (textChannel == null) {
        DiscordSRV.debug("Not sending join message, text channel is null");
        return;
    }
    final String displayName = StringUtils.isNotBlank(player.getDisplayName()) ? MessageUtil.strip(player.getDisplayName()) : "";
    final String message = StringUtils.isNotBlank(joinMessage) ? joinMessage : "";
    final String name = player.getName();
    final String avatarUrl = getAvatarUrl(player);
    final String botAvatarUrl = DiscordUtil.getJda().getSelfUser().getEffectiveAvatarUrl();
    String botName = getMainGuild() != null ? getMainGuild().getSelfMember().getEffectiveName() : DiscordUtil.getJda().getSelfUser().getName();
    BiFunction<String, Boolean, String> translator = (content, needsEscape) -> {
        if (content == null)
            return null;
        content = content.replaceAll("%time%|%date%", TimeUtil.timeStamp()).replace("%message%", MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(message) : message)).replace("%username%", needsEscape ? DiscordUtil.escapeMarkdown(name) : name).replace("%displayname%", needsEscape ? DiscordUtil.escapeMarkdown(displayName) : displayName).replace("%usernamenoescapes%", name).replace("%displaynamenoescapes%", displayName).replace("%embedavatarurl%", avatarUrl).replace("%botavatarurl%", botAvatarUrl).replace("%botname%", botName);
        content = DiscordUtil.translateEmotes(content, textChannel.getGuild());
        content = PlaceholderUtil.replacePlaceholdersToDiscord(content, player);
        return content;
    };
    Message discordMessage = translateMessage(messageFormat, translator);
    if (discordMessage == null)
        return;
    String webhookName = translator.apply(messageFormat.getWebhookName(), false);
    String webhookAvatarUrl = translator.apply(messageFormat.getWebhookAvatarUrl(), false);
    if (messageFormat.isUseWebhooks()) {
        WebhookUtil.deliverMessage(textChannel, webhookName, webhookAvatarUrl, discordMessage.getContentRaw(), discordMessage.getEmbeds().stream().findFirst().orElse(null));
    } else {
        DiscordUtil.queueMessage(textChannel, discordMessage, true);
    }
}
Also used : SSLContext(javax.net.ssl.SSLContext) LoggerContext(org.apache.logging.log4j.core.LoggerContext) IOUtil(net.dv8tion.jda.internal.utils.IOUtil) Level(org.apache.logging.log4j.Level) DualStackMode(com.neovisionaries.ws.client.DualStackMode) StringUtils(org.apache.commons.lang3.StringUtils) GsonBuilder(com.google.gson.GsonBuilder) GatewayIntent(net.dv8tion.jda.api.requests.GatewayIntent) ChatHook(github.scarsz.discordsrv.hooks.chat.ChatHook) AdvancedPie(org.bstats.charts.AdvancedPie) GroupSynchronizationManager(github.scarsz.discordsrv.objects.managers.GroupSynchronizationManager) CommandSender(org.bukkit.command.CommandSender) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) github.scarsz.discordsrv.objects.threads(github.scarsz.discordsrv.objects.threads) AccountLinkManager(github.scarsz.discordsrv.objects.managers.AccountLinkManager) PlayerQuitEvent(org.bukkit.event.player.PlayerQuitEvent) WebSocketFactory(com.neovisionaries.ws.client.WebSocketFactory) net.dv8tion.jda.api(net.dv8tion.jda.api) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) HandlerList(org.bukkit.event.HandlerList) PluginLoader(org.bukkit.plugin.PluginLoader) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) java.util(java.util) VaultHook(github.scarsz.discordsrv.hooks.VaultHook) CloseCode(net.dv8tion.jda.api.requests.CloseCode) Supplier(java.util.function.Supplier) SQLException(java.sql.SQLException) DrilldownPie(org.bstats.charts.DrilldownPie) CacheFlag(net.dv8tion.jda.api.utils.cache.CacheFlag) ApiManager(github.scarsz.discordsrv.api.ApiManager) MemberCachePolicy(net.dv8tion.jda.api.utils.MemberCachePolicy) PluginHook(github.scarsz.discordsrv.hooks.PluginHook) PluginBase(org.bukkit.plugin.PluginBase) ConsoleAppender(github.scarsz.discordsrv.objects.log4j.ConsoleAppender) FileUtils(org.apache.commons.io.FileUtils) Field(java.lang.reflect.Field) RequireLinkModule(github.scarsz.discordsrv.modules.requirelink.RequireLinkModule) CheckReturnValue(javax.annotation.CheckReturnValue) OkHttpClient(okhttp3.OkHttpClient) CommandManager(github.scarsz.discordsrv.objects.managers.CommandManager) Record(org.minidns.record.Record) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) DynamicConfig(github.scarsz.configuralize.DynamicConfig) Plugin(org.bukkit.plugin.Plugin) LoginException(javax.security.auth.login.LoginException) Dynamic(alexh.weak.Dynamic) AlertListener(github.scarsz.discordsrv.modules.alerts.AlertListener) ShutdownEvent(net.dv8tion.jda.api.events.ShutdownEvent) BiFunction(java.util.function.BiFunction) Player(org.bukkit.entity.Player) SimplePie(org.bstats.charts.SimplePie) ChunkGenerator(org.bukkit.generator.ChunkGenerator) org.bukkit(org.bukkit) DnsMessage(org.minidns.dnsmessage.DnsMessage) java.net(java.net) Language(github.scarsz.configuralize.Language) Gson(com.google.gson.Gson) RateLimitedException(net.dv8tion.jda.api.exceptions.RateLimitedException) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction) HierarchyException(net.dv8tion.jda.api.exceptions.HierarchyException) JdbcAccountLinkManager(github.scarsz.discordsrv.objects.managers.link.JdbcAccountLinkManager) Method(java.lang.reflect.Method) github.scarsz.discordsrv.api.events(github.scarsz.discordsrv.api.events) PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) Lag(github.scarsz.discordsrv.objects.Lag) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) PatternSyntaxException(java.util.regex.PatternSyntaxException) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) java.util.concurrent(java.util.concurrent) PermissionDefault(org.bukkit.permissions.PermissionDefault) VanishHook(github.scarsz.discordsrv.hooks.vanish.VanishHook) Metrics(org.bstats.bukkit.Metrics) SingleLineChart(org.bstats.charts.SingleLineChart) Logger(java.util.logging.Logger) PluginCommand(org.bukkit.command.PluginCommand) Collectors(java.util.stream.Collectors) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) Nullable(org.jetbrains.annotations.Nullable) MetadataValue(org.bukkit.metadata.MetadataValue) Command(org.bukkit.command.Command) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) BukkitWorker(org.bukkit.scheduler.BukkitWorker) CancellationDetector(github.scarsz.discordsrv.objects.CancellationDetector) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Getter(lombok.Getter) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) ImmutableList(com.google.common.collect.ImmutableList) Component(net.kyori.adventure.text.Component) ParseException(github.scarsz.configuralize.ParseException) github.scarsz.discordsrv.listeners(github.scarsz.discordsrv.listeners) JdaFilter(github.scarsz.discordsrv.objects.log4j.JdaFilter) AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) OkHostnameVerifier(okhttp3.internal.tls.OkHostnameVerifier) github.scarsz.discordsrv.util(github.scarsz.discordsrv.util) ConsoleMessage(github.scarsz.discordsrv.objects.ConsoleMessage) RestAction(net.dv8tion.jda.api.requests.RestAction) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) Dns(okhttp3.Dns) FileAccountLinkManager(github.scarsz.discordsrv.objects.managers.link.FileAccountLinkManager) Consumer(java.util.function.Consumer) java.io(java.io) DnsClient(org.minidns.DnsClient) VoiceModule(github.scarsz.discordsrv.modules.voice.VoiceModule) MultiverseCoreHook(github.scarsz.discordsrv.hooks.world.MultiverseCoreHook) DigestUtils(org.apache.commons.codec.digest.DigestUtils) LogManager(org.apache.logging.log4j.LogManager) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) DnsMessage(org.minidns.dnsmessage.DnsMessage) ConsoleMessage(github.scarsz.discordsrv.objects.ConsoleMessage)

Example 4 with MessageFormat

use of github.scarsz.discordsrv.objects.MessageFormat in project DiscordSRV by Scarsz.

the class AlertListener method process.

private void process(Object event, Dynamic alert, Set<String> triggers, int alertIndex) {
    Player player = event instanceof PlayerEvent ? ((PlayerEvent) event).getPlayer() : null;
    if (player == null) {
        // this will check to see if a #getPlayer() method exists on events coming through
        try {
            Method getPlayerMethod = event.getClass().getMethod("getPlayer");
            if (getPlayerMethod.getReturnType().equals(Player.class)) {
                player = (Player) getPlayerMethod.invoke(event);
            }
        } catch (Exception ignored) {
        // we tried ¯\_(ツ)_/¯
        }
    }
    CommandSender sender = null;
    String command = null;
    List<String> args = new LinkedList<>();
    if (event instanceof PlayerCommandPreprocessEvent) {
        sender = player;
        command = ((PlayerCommandPreprocessEvent) event).getMessage().substring(1);
    } else if (event instanceof ServerCommandEvent) {
        sender = ((ServerCommandEvent) event).getSender();
        command = ((ServerCommandEvent) event).getCommand();
    }
    if (StringUtils.isNotBlank(command)) {
        String[] split = command.split(" ", 2);
        String commandBase = split[0];
        if (split.length == 2)
            args.addAll(Arrays.asList(split[1].split(" ")));
        // transform "discordsrv:discord" to just "discord" for example
        if (commandBase.contains(":"))
            commandBase = commandBase.substring(commandBase.lastIndexOf(":") + 1);
        command = commandBase + (split.length == 2 ? (" " + split[1]) : "");
    }
    MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("Alerts." + alertIndex);
    for (String trigger : triggers) {
        String eventName = getEventName(event);
        if (trigger.startsWith("/")) {
            if (StringUtils.isBlank(command) || !command.toLowerCase().split("\\s+|$", 2)[0].equals(trigger.substring(1)))
                continue;
        } else {
            // make sure the called event matches what this alert is supposed to trigger on
            if (!eventName.equalsIgnoreCase(trigger))
                continue;
        }
        // make sure alert should run even if event is cancelled
        if (event instanceof Cancellable && ((Cancellable) event).isCancelled()) {
            Dynamic ignoreCancelledDynamic = alert.get("IgnoreCancelled");
            boolean ignoreCancelled = ignoreCancelledDynamic.isPresent() ? ignoreCancelledDynamic.as(Boolean.class) : true;
            if (ignoreCancelled) {
                DiscordSRV.debug(Debug.ALERTS, "Not running alert for event " + eventName + ": event was cancelled");
                return;
            }
        }
        Dynamic textChannelsDynamic = alert.get("Channel");
        if (textChannelsDynamic == null) {
            DiscordSRV.debug(Debug.ALERTS, "Not running alert for trigger " + trigger + ": no target channel was defined");
            return;
        }
        Set<String> channels = new HashSet<>();
        if (textChannelsDynamic.isList()) {
            textChannelsDynamic.children().map(Weak::asString).filter(Objects::nonNull).forEach(channels::add);
        } else if (textChannelsDynamic.isString()) {
            channels.add(textChannelsDynamic.asString());
        }
        Function<Function<String, Collection<TextChannel>>, Set<TextChannel>> channelResolver = converter -> {
            Set<TextChannel> textChannels = new HashSet<>();
            channels.forEach(channel -> textChannels.addAll(converter.apply(channel)));
            textChannels.removeIf(Objects::isNull);
            return textChannels;
        };
        Set<TextChannel> textChannels = channelResolver.apply(s -> {
            TextChannel target = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(s);
            return Collections.singleton(target);
        });
        if (textChannels.isEmpty()) {
            textChannels.addAll(channelResolver.apply(s -> DiscordUtil.getJda().getTextChannelsByName(s, false)));
        }
        if (textChannels.isEmpty()) {
            textChannels.addAll(channelResolver.apply(s -> NumberUtils.isDigits(s) ? Collections.singleton(DiscordUtil.getJda().getTextChannelById(s)) : Collections.emptyList()));
        }
        if (textChannels.size() == 0) {
            DiscordSRV.debug(Debug.ALERTS, "Not running alert for trigger " + trigger + ": no target channel was defined/found (channels: " + channels + ")");
            return;
        }
        for (TextChannel textChannel : textChannels) {
            // check alert conditions
            boolean allConditionsMet = true;
            Dynamic conditionsDynamic = alert.dget("Conditions");
            if (conditionsDynamic.isPresent()) {
                Iterator<Dynamic> conditions = conditionsDynamic.children().iterator();
                while (conditions.hasNext()) {
                    Dynamic dynamic = conditions.next();
                    String expression = dynamic.convert().intoString();
                    try {
                        Boolean value = new SpELExpressionBuilder(expression).withPluginVariables().withVariable("event", event).withVariable("server", Bukkit.getServer()).withVariable("discordsrv", DiscordSRV.getPlugin()).withVariable("player", player).withVariable("sender", sender).withVariable("command", command).withVariable("args", args).withVariable("allArgs", String.join(" ", args)).withVariable("channel", textChannel).withVariable("jda", DiscordUtil.getJda()).evaluate(event, Boolean.class);
                        DiscordSRV.debug(Debug.ALERTS, "Condition \"" + expression + "\" -> " + value);
                        if (value != null && !value) {
                            allConditionsMet = false;
                            break;
                        }
                    } catch (ParseException e) {
                        DiscordSRV.error("Error while parsing expression \"" + expression + "\" for trigger \"" + trigger + "\" -> " + e.getMessage());
                    } catch (SpelEvaluationException e) {
                        DiscordSRV.error("Error while evaluating expression \"" + expression + "\" for trigger \"" + trigger + "\" -> " + e.getMessage());
                    }
                }
                if (!allConditionsMet)
                    continue;
            }
            CommandSender finalSender = sender;
            String finalCommand = command;
            Player finalPlayer = player;
            BiFunction<String, Boolean, String> translator = (content, needsEscape) -> {
                if (content == null)
                    return null;
                // evaluate any SpEL expressions
                Map<String, Object> variables = new HashMap<>();
                variables.put("event", event);
                variables.put("server", Bukkit.getServer());
                variables.put("discordsrv", DiscordSRV.getPlugin());
                variables.put("player", finalPlayer);
                variables.put("sender", finalSender);
                variables.put("command", finalCommand);
                variables.put("args", args);
                variables.put("allArgs", String.join(" ", args));
                variables.put("channel", textChannel);
                variables.put("jda", DiscordUtil.getJda());
                content = NamedValueFormatter.formatExpressions(content, event, variables);
                // replace any normal placeholders
                content = NamedValueFormatter.format(content, key -> {
                    switch(key) {
                        case "tps":
                            return Lag.getTPSString();
                        case "time":
                        case "date":
                            return TimeUtil.timeStamp();
                        case "ping":
                            return finalPlayer != null ? PlayerUtil.getPing(finalPlayer) : "-1";
                        case "name":
                        case "username":
                            return finalPlayer != null ? finalPlayer.getName() : "";
                        case "displayname":
                            return finalPlayer != null ? MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(finalPlayer.getDisplayName()) : finalPlayer.getDisplayName()) : "";
                        case "world":
                            return finalPlayer != null ? finalPlayer.getWorld().getName() : "";
                        case "embedavatarurl":
                            return finalPlayer != null ? DiscordSRV.getAvatarUrl(finalPlayer) : DiscordUtil.getJda().getSelfUser().getEffectiveAvatarUrl();
                        case "botavatarurl":
                            return DiscordUtil.getJda().getSelfUser().getEffectiveAvatarUrl();
                        case "botname":
                            return DiscordSRV.getPlugin().getMainGuild() != null ? DiscordSRV.getPlugin().getMainGuild().getSelfMember().getEffectiveName() : DiscordUtil.getJda().getSelfUser().getName();
                        default:
                            return "{" + key + "}";
                    }
                });
                content = DiscordUtil.translateEmotes(content, textChannel.getGuild());
                content = PlaceholderUtil.replacePlaceholdersToDiscord(content, finalPlayer);
                return content;
            };
            Message message = DiscordSRV.translateMessage(messageFormat, translator);
            if (message == null) {
                DiscordSRV.debug(Debug.ALERTS, "Not sending alert because it is configured to have no message content");
                return;
            }
            if (messageFormat.isUseWebhooks()) {
                WebhookUtil.deliverMessage(textChannel, translator.apply(messageFormat.getWebhookName(), false), translator.apply(messageFormat.getWebhookAvatarUrl(), false), message.getContentRaw(), message.getEmbeds().stream().findFirst().orElse(null));
            } else {
                DiscordUtil.queueMessage(textChannel, message);
            }
        }
    }
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) Dynamic(alexh.weak.Dynamic) java.util(java.util) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) BiFunction(java.util.function.BiFunction) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Player(org.bukkit.entity.Player) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) ServerCommandEvent(org.bukkit.event.server.ServerCommandEvent) Matcher(java.util.regex.Matcher) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) org.bukkit.event(org.bukkit.event) PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Weak(alexh.weak.Weak) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) github.scarsz.discordsrv.util(github.scarsz.discordsrv.util) Method(java.lang.reflect.Method) ParseException(org.springframework.expression.ParseException) Bukkit(org.bukkit.Bukkit) Message(net.dv8tion.jda.api.entities.Message) Lag(github.scarsz.discordsrv.objects.Lag) CommandSender(org.bukkit.command.CommandSender) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Debug(github.scarsz.discordsrv.Debug) Subscribe(github.scarsz.discordsrv.api.Subscribe) RegisteredListener(org.bukkit.plugin.RegisteredListener) TimeUnit(java.util.concurrent.TimeUnit) EventListener(net.dv8tion.jda.api.hooks.EventListener) Modifier(java.lang.reflect.Modifier) NumberUtils(org.apache.commons.lang3.math.NumberUtils) PlayerEvent(org.bukkit.event.player.PlayerEvent) ExpiringDualHashBidiMap(github.scarsz.discordsrv.objects.ExpiringDualHashBidiMap) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Dynamic(alexh.weak.Dynamic) Message(net.dv8tion.jda.api.entities.Message) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Player(org.bukkit.entity.Player) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) PlayerEvent(org.bukkit.event.player.PlayerEvent) Method(java.lang.reflect.Method) ServerCommandEvent(org.bukkit.event.server.ServerCommandEvent) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ParseException(org.springframework.expression.ParseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Weak(alexh.weak.Weak) CommandSender(org.bukkit.command.CommandSender) ParseException(org.springframework.expression.ParseException) ExpiringDualHashBidiMap(github.scarsz.discordsrv.objects.ExpiringDualHashBidiMap)

Example 5 with MessageFormat

use of github.scarsz.discordsrv.objects.MessageFormat in project DiscordSRV by Scarsz.

the class PlayerJoinLeaveListener method onPlayerJoin.

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
    final Player player = event.getPlayer();
    // if player is OP & update is available tell them
    if (GamePermissionUtil.hasPermission(player, "discordsrv.updatenotification") && DiscordSRV.updateIsAvailable) {
        MessageUtil.sendMessage(player, DiscordSRV.getPlugin().getDescription().getVersion().endsWith("-SNAPSHOT") ? ChatColor.GRAY + "There is a newer development build of DiscordSRV available. Download it at https://snapshot.discordsrv.com/" : ChatColor.AQUA + "An update to DiscordSRV is available. Download it at https://www.spigotmc.org/resources/discordsrv.18494/ or https://get.discordsrv.com");
    }
    if (DiscordSRV.getPlugin().isGroupRoleSynchronizationEnabled()) {
        // trigger a synchronization for the player
        Bukkit.getScheduler().runTaskAsynchronously(DiscordSRV.getPlugin(), () -> DiscordSRV.getPlugin().getGroupSynchronizationManager().resync(player, GroupSynchronizationManager.SyncDirection.AUTHORITATIVE, true, GroupSynchronizationManager.SyncCause.PLAYER_JOIN));
    }
    if (PlayerUtil.isVanished(player)) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Not sending a join message for " + event.getPlayer().getName() + " because a vanish plugin reported them as vanished");
        return;
    }
    MessageFormat messageFormat = event.getPlayer().hasPlayedBefore() ? DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerJoinMessage") : DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerFirstJoinMessage");
    // make sure join messages enabled
    if (messageFormat == null)
        return;
    final String name = player.getName();
    // check if player has permission to not have join messages
    if (GamePermissionUtil.hasPermission(event.getPlayer(), "discordsrv.silentjoin")) {
        DiscordSRV.info(LangUtil.InternalMessage.SILENT_JOIN.toString().replace("{player}", name));
        return;
    }
    // player doesn't have silent join permission, send join message
    // schedule command to run in a second to be able to capture display name
    Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordSRV.getPlugin(), () -> DiscordSRV.getPlugin().sendJoinMessage(event.getPlayer(), event.getJoinMessage()), 20);
    // if enabled, set the player's discord nickname as their ign
    if (DiscordSRV.config().getBoolean("NicknameSynchronizationEnabled")) {
        Bukkit.getScheduler().runTaskAsynchronously(DiscordSRV.getPlugin(), () -> {
            final String discordId = DiscordSRV.getPlugin().getAccountLinkManager().getDiscordId(player.getUniqueId());
            DiscordSRV.getPlugin().getNicknameUpdater().setNickname(DiscordUtil.getMemberById(discordId), player);
        });
    }
}
Also used : Player(org.bukkit.entity.Player) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) EventHandler(org.bukkit.event.EventHandler)

Aggregations

MessageFormat (github.scarsz.discordsrv.objects.MessageFormat)13 Player (org.bukkit.entity.Player)10 github.scarsz.discordsrv.util (github.scarsz.discordsrv.util)6 BiFunction (java.util.function.BiFunction)6 StringUtils (org.apache.commons.lang3.StringUtils)6 EventHandler (org.bukkit.event.EventHandler)6 Debug (github.scarsz.discordsrv.Debug)4 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)4 Field (java.lang.reflect.Field)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Collectors (java.util.stream.Collectors)4 Message (net.dv8tion.jda.api.entities.Message)4 TextChannel (net.dv8tion.jda.api.entities.TextChannel)4 Bukkit (org.bukkit.Bukkit)4 Dynamic (alexh.weak.Dynamic)3 Lag (github.scarsz.discordsrv.objects.Lag)3 Method (java.lang.reflect.Method)3 java.util (java.util)3 Pattern (java.util.regex.Pattern)3 CommandSender (org.bukkit.command.CommandSender)3