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;
}
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);
}
}
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;
}
Aggregations