From ee1760c9bc1ef9c271ef9ab2c2c9b793796ceda0 Mon Sep 17 00:00:00 2001 From: TheFaser Date: Wed, 8 Nov 2023 22:33:17 +0500 Subject: [PATCH] Integrations: add DiscordSRV support --- .../chat/module/commands/CommandBan.java | 4 + .../module/commands/CommandBroadcast.java | 3 + .../chat/module/commands/CommandKick.java | 3 + .../module/commands/CommandMaintenance.java | 3 + .../chat/module/commands/CommandMute.java | 3 + .../chat/module/commands/CommandPoll.java | 3 + .../chat/module/commands/CommandStream.java | 8 +- .../chat/module/commands/CommandWarn.java | 5 + .../chat/module/integrations/FDiscordSRV.java | 381 +++++++---- .../integrations/IntegrationsModule.java | 87 ++- .../advancement/AdvancementListener.java | 2 + .../serverMessage/death/DeathListener.java | 5 +- .../serverMessage/join/JoinListener.java | 6 +- .../serverMessage/quit/QuitListener.java | 6 +- src/main/resources/settings/integrations.yml | 619 +++++++++++++++++- 15 files changed, 1004 insertions(+), 134 deletions(-) diff --git a/src/main/java/net/flectone/chat/module/commands/CommandBan.java b/src/main/java/net/flectone/chat/module/commands/CommandBan.java index eb2d4da5..a8b97a3e 100644 --- a/src/main/java/net/flectone/chat/module/commands/CommandBan.java +++ b/src/main/java/net/flectone/chat/module/commands/CommandBan.java @@ -6,6 +6,7 @@ import net.flectone.chat.model.player.Moderation; import net.flectone.chat.module.FCommand; import net.flectone.chat.module.FModule; +import net.flectone.chat.module.integrations.IntegrationsModule; import net.flectone.chat.util.MessageUtil; import net.flectone.chat.util.TimeUtil; import org.apache.commons.lang.StringUtils; @@ -121,6 +122,9 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command : null; banFPlayer.ban(reason, banTime, moderator); + + IntegrationsModule.sendDiscordBan(banFPlayer.getOfflinePlayer(), banFPlayer.getBan()); + return true; } diff --git a/src/main/java/net/flectone/chat/module/commands/CommandBroadcast.java b/src/main/java/net/flectone/chat/module/commands/CommandBroadcast.java index e95c0e76..20a624ed 100644 --- a/src/main/java/net/flectone/chat/module/commands/CommandBroadcast.java +++ b/src/main/java/net/flectone/chat/module/commands/CommandBroadcast.java @@ -2,6 +2,7 @@ import net.flectone.chat.module.FCommand; import net.flectone.chat.module.FModule; +import net.flectone.chat.module.integrations.IntegrationsModule; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -47,6 +48,8 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command sendGlobalMessage(cmdSettings.getSender(), cmdSettings.getItemStack(), formatString, message, true); + IntegrationsModule.sendDiscordBroadcast(cmdSettings.getSender(), message); + return true; } diff --git a/src/main/java/net/flectone/chat/module/commands/CommandKick.java b/src/main/java/net/flectone/chat/module/commands/CommandKick.java index bf803398..9f1c49ec 100644 --- a/src/main/java/net/flectone/chat/module/commands/CommandKick.java +++ b/src/main/java/net/flectone/chat/module/commands/CommandKick.java @@ -2,6 +2,7 @@ import net.flectone.chat.module.FCommand; import net.flectone.chat.module.FModule; +import net.flectone.chat.module.integrations.IntegrationsModule; import net.flectone.chat.util.MessageUtil; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -100,5 +101,7 @@ public void kick(@NotNull Player toKick, @NotNull CommandSender commandSender, @ playerMessage = MessageUtil.formatAll(cmdSettings.getSender(), playerMessage); toKick.kickPlayer(playerMessage); + + IntegrationsModule.sendDiscordKick(toKick, reason, commandSender.getName()); } } diff --git a/src/main/java/net/flectone/chat/module/commands/CommandMaintenance.java b/src/main/java/net/flectone/chat/module/commands/CommandMaintenance.java index 4fdaca2a..e9925d6e 100644 --- a/src/main/java/net/flectone/chat/module/commands/CommandMaintenance.java +++ b/src/main/java/net/flectone/chat/module/commands/CommandMaintenance.java @@ -3,6 +3,7 @@ import net.flectone.chat.manager.FActionManager; import net.flectone.chat.module.FCommand; import net.flectone.chat.module.FModule; +import net.flectone.chat.module.integrations.IntegrationsModule; import net.flectone.chat.util.MessageUtil; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -86,6 +87,8 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command commands.set(getName() + ".turned-on", isEnabled); commands.save(); + IntegrationsModule.sendDiscordMaintenance(cmdSettings.getSender(), isEnabled ? "turn-on" : "turn-off"); + return true; } diff --git a/src/main/java/net/flectone/chat/module/commands/CommandMute.java b/src/main/java/net/flectone/chat/module/commands/CommandMute.java index 44f4a10b..3f59f7b3 100644 --- a/src/main/java/net/flectone/chat/module/commands/CommandMute.java +++ b/src/main/java/net/flectone/chat/module/commands/CommandMute.java @@ -4,6 +4,7 @@ import net.flectone.chat.model.player.FPlayer; import net.flectone.chat.module.FCommand; import net.flectone.chat.module.FModule; +import net.flectone.chat.module.integrations.IntegrationsModule; import net.flectone.chat.util.MessageUtil; import net.flectone.chat.util.TimeUtil; import org.apache.commons.lang.StringUtils; @@ -84,6 +85,8 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command mutedFPlayer.mute(reason, muteTime, moderator); + IntegrationsModule.sendDiscordMute(mutedFPlayer.getOfflinePlayer(), mutedFPlayer.getMute()); + return true; } diff --git a/src/main/java/net/flectone/chat/module/commands/CommandPoll.java b/src/main/java/net/flectone/chat/module/commands/CommandPoll.java index 41ae2e2e..95a8a653 100644 --- a/src/main/java/net/flectone/chat/module/commands/CommandPoll.java +++ b/src/main/java/net/flectone/chat/module/commands/CommandPoll.java @@ -7,6 +7,7 @@ import net.flectone.chat.model.sound.FSound; import net.flectone.chat.module.FCommand; import net.flectone.chat.module.FModule; +import net.flectone.chat.module.integrations.IntegrationsModule; import net.flectone.chat.module.sounds.SoundsModule; import net.flectone.chat.util.MessageUtil; import net.md_5.bungee.api.chat.ComponentBuilder; @@ -107,6 +108,8 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command commands.set(getName() + ".last-id", poll.getId()); commands.save(); + IntegrationsModule.sendDiscordPoll(cmdSettings.getSender(), poll.getMessage(), poll.getId()); + return true; } diff --git a/src/main/java/net/flectone/chat/module/commands/CommandStream.java b/src/main/java/net/flectone/chat/module/commands/CommandStream.java index a7a932b9..5bb46680 100644 --- a/src/main/java/net/flectone/chat/module/commands/CommandStream.java +++ b/src/main/java/net/flectone/chat/module/commands/CommandStream.java @@ -5,6 +5,7 @@ import net.flectone.chat.model.player.Settings; import net.flectone.chat.module.FCommand; import net.flectone.chat.module.FModule; +import net.flectone.chat.module.integrations.IntegrationsModule; import net.flectone.chat.util.MessageUtil; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -85,6 +86,8 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command } + String urls = MessageUtil.joinArray(args, 1, "\n"); + StringBuilder stringBuilder = new StringBuilder(); locale.getVaultStringList(commandSender,this + ".start.message") @@ -92,7 +95,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command string = string .replace("", commandSender.getName()) - .replace("", MessageUtil.joinArray(args, 1, "\n") + " "); + .replace("", urls + " "); stringBuilder.append(string); stringBuilder.append("\n"); @@ -100,6 +103,9 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command }); sendGlobalMessage(cmdSettings.getSender(), cmdSettings.getItemStack(), stringBuilder.toString(), "", false); + + IntegrationsModule.sendDiscordStream(cmdSettings.getSender(), urls); + return true; } diff --git a/src/main/java/net/flectone/chat/module/commands/CommandWarn.java b/src/main/java/net/flectone/chat/module/commands/CommandWarn.java index 7dd16239..a7df8efd 100644 --- a/src/main/java/net/flectone/chat/module/commands/CommandWarn.java +++ b/src/main/java/net/flectone/chat/module/commands/CommandWarn.java @@ -5,6 +5,7 @@ import net.flectone.chat.model.player.FPlayer; import net.flectone.chat.module.FCommand; import net.flectone.chat.module.FModule; +import net.flectone.chat.module.integrations.IntegrationsModule; import net.flectone.chat.util.MessageUtil; import net.flectone.chat.util.TimeUtil; import org.apache.commons.lang.StringUtils; @@ -96,6 +97,10 @@ public void asyncOnCommand(@NotNull CommandSender commandSender, @NotNull Comman : null; warnedFPlayer.warn(reason, time, moderator); + + IntegrationsModule.sendDiscordWarn(warnedFPlayer.getOfflinePlayer(), + warnedFPlayer.getWarnList().get(warnedFPlayer.getWarnList().size() - 1), + warnedFPlayer.getCountWarns() + 1); } @Override diff --git a/src/main/java/net/flectone/chat/module/integrations/FDiscordSRV.java b/src/main/java/net/flectone/chat/module/integrations/FDiscordSRV.java index f7df5125..097d4bc6 100644 --- a/src/main/java/net/flectone/chat/module/integrations/FDiscordSRV.java +++ b/src/main/java/net/flectone/chat/module/integrations/FDiscordSRV.java @@ -1,26 +1,32 @@ package net.flectone.chat.module.integrations; -import github.scarsz.discordsrv.Debug; import github.scarsz.discordsrv.DiscordSRV; import github.scarsz.discordsrv.api.ListenerPriority; import github.scarsz.discordsrv.api.Subscribe; import github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent; -import github.scarsz.discordsrv.objects.MessageFormat; +import github.scarsz.discordsrv.dependencies.jda.api.EmbedBuilder; +import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel; import github.scarsz.discordsrv.util.DiscordUtil; -import github.scarsz.discordsrv.util.MessageUtil; -import github.scarsz.discordsrv.util.PlaceholderUtil; -import github.scarsz.discordsrv.util.TimeUtil; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.TextChannel; import net.flectone.chat.FlectoneChat; import net.flectone.chat.builder.MessageBuilder; -import org.apache.commons.lang.StringUtils; +import net.flectone.chat.model.advancement.FAdvancement; +import net.flectone.chat.model.damager.PlayerDamager; +import net.flectone.chat.model.player.Moderation; +import net.flectone.chat.util.TimeUtil; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.Listener; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.awt.*; +import java.util.HashMap; import java.util.List; -import java.util.function.BiFunction; +import java.util.Map; import static net.flectone.chat.manager.FileManager.integrations; @@ -53,124 +59,249 @@ public FDiscordSRV() { init(); } - public static void sendDeathMessage(@NotNull Player player, @NotNull String message) { - - message = message.length() > 255 ? message.substring(0, 255) : message; - - String channelName = DiscordSRV.getPlugin().getOptionalChannel("deaths"); - MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerDeathMessage"); - if (messageFormat == null) return; - - String finalDeathMessage = StringUtils.isNotBlank(message) ? message : ""; - 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 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("%deathmessage%", MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(finalDeathMessage) : finalDeathMessage)) - .replace("%deathmessagenoescapes%", MessageUtil.strip(finalDeathMessage)) - .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; - - if (DiscordSRV.getLength(discordMessage) < 3) { - DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Not sending death message, because it's less than three characters long. Message: " + messageFormat); - return; + public void sendDeathMessage(@NotNull Player sender, @NotNull PlayerDamager playerDamager, @NotNull String typeDeath) { + Entity finalEntity = playerDamager.getFinalEntity(); + Material finalBlock = playerDamager.getFinalBlockDamager(); + Entity killer = playerDamager.getKiller(); + ItemStack killerItem = playerDamager.getKillerItem(); + + String message = integrations.getString("DiscordSRV.message.death.type" + typeDeath); + + if (finalEntity != null) message = message + .replace("", finalEntity.getName()) + .replace("", finalEntity.getName()); + + if (finalBlock != null) message = message + .replace("", finalBlock.name()); + + if (killer != null && finalEntity != null && !killer.getType().equals(finalEntity.getType())) { + String dueToMessage = integrations.getString("DiscordSRV.message.death.due-to"); + message = message.replace("", dueToMessage.replace("", killer.getName())); + } + + if (killerItem != null) { + String byItemMessage = integrations.getString("DiscordSRV.message.death.by-item"); + + String itemName = killerItem.getItemMeta() != null && !killerItem.getItemMeta().getDisplayName().isEmpty() + ? net.md_5.bungee.api.ChatColor.ITALIC + killerItem.getItemMeta().getDisplayName() + : killerItem.getType().name(); + + message = message.replace("", byItemMessage.replace("", itemName)); } - TextChannel textChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName); - DiscordUtil.queueMessage(textChannel, discordMessage, true); - } - - public static void sendDiscordMessageToChannel(@NotNull String message) { - sendDiscordMessageToChannel(message, "moderation"); - } - - public static void sendDiscordMessageToChannel(@NotNull String message, @NotNull String nameChannel) { -// message = ObjectUtil.formatString(message, null); -// message = PlaceholderUtil.replacePlaceholdersToDiscord(message); -// -// String channelName = DiscordSRV.getPlugin().getOptionalChannel(nameChannel); -// TextChannel textChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName); -// -// Message messageFormat = new MessageBuilder() -// .setEmbeds(new EmbedBuilder().setColor(Color.RED).setDescription(message).build()) -// .build(); -// -// DiscordUtil.queueMessage(textChannel, messageFormat, true); - } - -// public static void sendAdvancementMessage(@NotNull Player player, @NotNull FAdvancement fAdvancement, @NotNull String lastAdvancement) { -// String channelName = DiscordSRV.getPlugin().getOptionalChannel("awards"); -// -// MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerAchievementMessage"); -// if (messageFormat == null) return; -// -// String advancementTitle = fAdvancement.getTitle(); -// -// lastAdvancement = PlaceholderUtil.replacePlaceholdersToDiscord(lastAdvancement); -// -// lastAdvancement = lastAdvancement.length() > 255 ? lastAdvancement.substring(0, 255) : lastAdvancement; -// -// String finalAchievementName = StringUtils.isNotBlank(advancementTitle) ? advancementTitle : ""; -// 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 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); -// -// MessageEmbed embed = discordMessage.getEmbeds().get(0); -// -// EmbedBuilder embedBuilder = new EmbedBuilder(); -// embedBuilder.setColor(embed.getColor()); -// -// MessageEmbed.AuthorInfo authorInfo = embed.getAuthor(); -// embedBuilder.setAuthor(lastAdvancement, authorInfo.getUrl(), authorInfo.getIconUrl()); -// -// MessageBuilder messageBuilder = new MessageBuilder(); -// messageBuilder.setEmbeds(embedBuilder.build()); -// -// TextChannel textChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName); -// DiscordUtil.queueMessage(textChannel, messageBuilder.build(), true); -// } + message = message.replace("", "") + .replace("", "") + .replace("", "") + .replace("", "") + .replace("", ""); + + Map replacements = new HashMap<>(); + + replacements.put("", message); + replacements.put("", sender.getName()); + + sendMessage(sender, "death", replacements); + } + + public void sendJoinMessage(@NotNull Player sender, @NotNull String type) { + String message = integrations.getString("DiscordSRV.message.join.type." + type) + .replace("", sender.getName()); + + Map replacements = new HashMap<>(); + + replacements.put("", message); + replacements.put("", sender.getName()); + + sendMessage(sender, "join", replacements); + } + + public void sendQuitMessage(@NotNull Player sender, @NotNull String type) { + String message = integrations.getString("DiscordSRV.message.quit.type." + type) + .replace("", sender.getName()); + + Map replacements = new HashMap<>(); + + replacements.put("", message); + replacements.put("", sender.getName()); + + sendMessage(sender, "quit", replacements); + } + + public void sendAdvancementMessage(@NotNull Player sender, @NotNull FAdvancement advancement) { + HashMap replacements = new HashMap<>(); + replacements.put("", sender.getName()); + replacements.put("", advancement.getTitle()); + + sendMessage(sender, "advancement-" + advancement.getType(), replacements); + } + + public void sendStreamMessage(@Nullable Player sender, @NotNull String urls) { + String senderName = sender == null ? "CONSOLE" : sender.getName(); + + HashMap replacements = new HashMap<>(); + replacements.put("", senderName); + replacements.put("", urls); + + sendMessage(sender, "stream", replacements); + } + + public void sendBanMessage(@Nullable OfflinePlayer sender, @NotNull Moderation moderation) { + String type = moderation.getTime() == -1 ? "permanent" : "usually"; + String message = integrations.getString("DiscordSRV.message.ban.type." + type) + .replace("", moderation.getPlayerName()) + .replace("