Search in sources :

Example 11 with MessageFormat

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

the class AchievementMessagePreProcessEvent method setMessage.

@Deprecated
public void setMessage(String message) {
    MessageFormat messageFormat = new MessageFormat();
    messageFormat.setContent(message);
    this.messageFormat = messageFormat;
}
Also used : MessageFormat(github.scarsz.discordsrv.objects.MessageFormat)

Example 12 with MessageFormat

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

the class PlayerAchievementsListener method runAsync.

private void runAsync(Event event, Player player) {
    Enum<?> achievement;
    try {
        achievement = (Enum<?>) event.getClass().getMethod("getAchievement").invoke(event);
        if (achievement == null)
            return;
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        DiscordSRV.error("Failed to get achievement name from " + event.getEventName() + ": " + e.getMessage());
        handlerList.unregister(registeredListener);
        return;
    }
    // turn "ACHIEVEMENT_NAME" into "Achievement Name"
    String channelName = DiscordSRV.getPlugin().getOptionalChannel("awards");
    String achievementName = PrettyUtil.beautify(achievement);
    MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerAchievementMessage");
    if (messageFormat == null)
        return;
    AchievementMessagePreProcessEvent preEvent = DiscordSRV.api.callEvent(new AchievementMessagePreProcessEvent(channelName, messageFormat, player, achievementName, event));
    if (preEvent.isCancelled()) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "AchievementMessagePreProcessEvent was cancelled, message send aborted");
        return;
    }
    // Update from event in case any listeners modified parameters
    achievementName = preEvent.getAchievementName();
    channelName = preEvent.getChannel();
    messageFormat = preEvent.getMessageFormat();
    if (messageFormat == null)
        return;
    String finalAchievementName = StringUtils.isNotBlank(achievementName) ? achievementName : "";
    String avatarUrl = DiscordSRV.getAvatarUrl(player);
    String botAvatarUrl = DiscordUtil.getJda().getSelfUser().getEffectiveAvatarUrl();
    String botName = DiscordSRV.getPlugin().getMainGuild() != null ? DiscordSRV.getPlugin().getMainGuild().getSelfMember().getEffectiveName() : DiscordUtil.getJda().getSelfUser().getName();
    String displayName = StringUtils.isNotBlank(player.getDisplayName()) ? MessageUtil.strip(player.getDisplayName()) : "";
    TextChannel destinationChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName);
    BiFunction<String, Boolean, String> translator = (content, needsEscape) -> {
        if (content == null)
            return null;
        content = content.replaceAll("%time%|%date%", TimeUtil.timeStamp()).replace("%username%", needsEscape ? DiscordUtil.escapeMarkdown(player.getName()) : player.getName()).replace("%displayname%", needsEscape ? DiscordUtil.escapeMarkdown(displayName) : displayName).replace("%usernamenoescapes%", player.getName()).replace("%displaynamenoescapes%", displayName).replace("%world%", player.getWorld().getName()).replace("%achievement%", MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(finalAchievementName) : finalAchievementName)).replace("%embedavatarurl%", avatarUrl).replace("%botavatarurl%", botAvatarUrl).replace("%botname%", botName);
        if (destinationChannel != null)
            content = DiscordUtil.translateEmotes(content, destinationChannel.getGuild());
        content = PlaceholderUtil.replacePlaceholdersToDiscord(content, player);
        return content;
    };
    Message discordMessage = DiscordSRV.translateMessage(messageFormat, translator);
    if (discordMessage == null)
        return;
    String webhookName = translator.apply(messageFormat.getWebhookName(), false);
    String webhookAvatarUrl = translator.apply(messageFormat.getWebhookAvatarUrl(), false);
    AchievementMessagePostProcessEvent postEvent = DiscordSRV.api.callEvent(new AchievementMessagePostProcessEvent(channelName, discordMessage, player, achievementName, event, messageFormat.isUseWebhooks(), webhookName, webhookAvatarUrl, preEvent.isCancelled()));
    if (postEvent.isCancelled()) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "AchievementMessagePostProcessEvent was cancelled, message send aborted");
        return;
    }
    // Update from event in case any listeners modified parameters
    channelName = postEvent.getChannel();
    discordMessage = postEvent.getDiscordMessage();
    TextChannel textChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName);
    if (postEvent.isUsingWebhooks()) {
        WebhookUtil.deliverMessage(textChannel, postEvent.getWebhookName(), postEvent.getWebhookAvatarUrl(), discordMessage.getContentRaw(), discordMessage.getEmbeds().stream().findFirst().orElse(null));
    } else {
        DiscordUtil.queueMessage(textChannel, discordMessage, true);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) HandlerList(org.bukkit.event.HandlerList) AchievementMessagePreProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) BiFunction(java.util.function.BiFunction) Event(org.bukkit.event.Event) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Player(org.bukkit.entity.Player) StringUtils(org.apache.commons.lang3.StringUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) Debug(github.scarsz.discordsrv.Debug) RegisteredListener(org.bukkit.plugin.RegisteredListener) AchievementMessagePostProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) EventPriority(org.bukkit.event.EventPriority) EventExecutor(org.bukkit.plugin.EventExecutor) PlayerEvent(org.bukkit.event.player.PlayerEvent) github.scarsz.discordsrv.util(github.scarsz.discordsrv.util) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) Message(net.dv8tion.jda.api.entities.Message) InvocationTargetException(java.lang.reflect.InvocationTargetException) AchievementMessagePreProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent) TextChannel(net.dv8tion.jda.api.entities.TextChannel) AchievementMessagePostProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent)

Example 13 with MessageFormat

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

the class DeathMessagePreProcessEvent method setMessage.

@Deprecated
public void setMessage(String message) {
    MessageFormat messageFormat = new MessageFormat();
    messageFormat.setContent(message);
    this.messageFormat = messageFormat;
}
Also used : MessageFormat(github.scarsz.discordsrv.objects.MessageFormat)

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