|
| 1 | +package dev.demonz.zdiscord.modules; |
| 2 | + |
| 3 | +import dev.demonz.zdiscord.ZDiscord; |
| 4 | +import dev.demonz.zdiscord.util.ColorUtil; |
| 5 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 6 | +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; |
| 7 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 8 | +import org.bukkit.entity.Player; |
| 9 | + |
| 10 | +import java.time.Instant; |
| 11 | +import java.util.concurrent.ConcurrentHashMap; |
| 12 | +import java.util.concurrent.atomic.AtomicInteger; |
| 13 | + |
| 14 | +public class ConfessionModule { |
| 15 | + |
| 16 | + private static final String CONFESSION_TITLE = "\uD83D\uDC8C A new confession"; |
| 17 | + private static final int MAX_MESSAGE_LENGTH = 1500; |
| 18 | + |
| 19 | + private final ZDiscord plugin; |
| 20 | + private final ConcurrentHashMap<String, Long> cooldowns = new ConcurrentHashMap<>(); |
| 21 | + private final AtomicInteger counter = new AtomicInteger(0); |
| 22 | + |
| 23 | + public ConfessionModule(ZDiscord plugin) { |
| 24 | + this.plugin = plugin; |
| 25 | + } |
| 26 | + |
| 27 | + public void postFromDiscord(SlashCommandInteractionEvent event, String message) { |
| 28 | + post( |
| 29 | + "discord:" + event.getUser().getId(), |
| 30 | + message, |
| 31 | + reply -> event.reply(reply).setEphemeral(true).queue()); |
| 32 | + } |
| 33 | + |
| 34 | + public void postFromMinecraft(Player player, String message) { |
| 35 | + post( |
| 36 | + "minecraft:" + player.getUniqueId(), |
| 37 | + message, |
| 38 | + reply -> plugin.getPlatformAdapter().runForEntity( |
| 39 | + player, () -> player.sendMessage(reply))); |
| 40 | + } |
| 41 | + |
| 42 | + private void post(String cooldownKey, String message, ReplyTarget replyTarget) { |
| 43 | + if (plugin.getBotManager() == null |
| 44 | + || plugin.getBotManager().getJda() == null |
| 45 | + || !plugin.getBotManager().isConnected()) { |
| 46 | + replyTarget.reply("Confessions are unavailable because the Discord bot is not connected."); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + String channelId = plugin.getConfigManager() |
| 51 | + .getString("channels.confessions", "").trim(); |
| 52 | + if (channelId.isEmpty() || channelId.startsWith("YOUR_")) { |
| 53 | + replyTarget.reply("Confessions are disabled on this server " |
| 54 | + + "(no channels.confessions is configured)."); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + TextChannel channel = plugin.getBotManager().getJda() |
| 59 | + .getTextChannelById(channelId); |
| 60 | + if (channel == null) { |
| 61 | + replyTarget.reply("Confessions are disabled on this server " |
| 62 | + + "(the configured channel could not be found)."); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + String cleaned = message == null ? "" : message.trim(); |
| 67 | + if (cleaned.isEmpty()) { |
| 68 | + replyTarget.reply("Usage: /confess <message>"); |
| 69 | + return; |
| 70 | + } |
| 71 | + if (cleaned.length() > MAX_MESSAGE_LENGTH) { |
| 72 | + replyTarget.reply("Your confession is too long (max " |
| 73 | + + MAX_MESSAGE_LENGTH + " characters)."); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + long cooldownMs = plugin.getConfigManager() |
| 78 | + .getInt("confessions.cooldown", 300) * 1000L; |
| 79 | + long now = System.currentTimeMillis(); |
| 80 | + Long lastConfession = cooldowns.get(cooldownKey); |
| 81 | + if (lastConfession != null && (now - lastConfession) < cooldownMs) { |
| 82 | + long remaining = Math.max(1L, (cooldownMs - (now - lastConfession) + 999L) / 1000L); |
| 83 | + replyTarget.reply("You can confess again in " + remaining + " seconds."); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + String handle = "Confessor #" + counter.incrementAndGet(); |
| 88 | + String colorHex = plugin.getConfigManager() |
| 89 | + .getString("confessions.color", "#9B59B6"); |
| 90 | + |
| 91 | + EmbedBuilder embed = new EmbedBuilder() |
| 92 | + .setAuthor(CONFESSION_TITLE, null, null) |
| 93 | + .setDescription(ColorUtil.toDiscordMarkdown(cleaned)) |
| 94 | + .setColor(ColorUtil.parseHex(colorHex)) |
| 95 | + .setFooter(handle + " \u00B7 posted at", null) |
| 96 | + .setTimestamp(Instant.now()); |
| 97 | + |
| 98 | + channel.sendMessageEmbeds(embed.build()).queue( |
| 99 | + success -> { |
| 100 | + cooldowns.put(cooldownKey, now); |
| 101 | + replyTarget.reply("Your confession was posted anonymously."); |
| 102 | + }, |
| 103 | + error -> replyTarget.reply("Failed to post your confession: " |
| 104 | + + error.getMessage())); |
| 105 | + } |
| 106 | + |
| 107 | + private interface ReplyTarget { |
| 108 | + void reply(String message); |
| 109 | + } |
| 110 | +} |
0 commit comments