From cb34dfff901b6326f1e1bb05640a9c4864146fb9 Mon Sep 17 00:00:00 2001
From: ChomeNS <en7erman@yandex.com>
Date: Mon, 27 Mar 2023 12:47:53 +0700
Subject: [PATCH] add uuid and time + discord fix idk

---
 pom.xml                                       |  6 ++
 .../chomens_bot/commands/TimeCommand.java     | 66 +++++++++++++
 .../chomens_bot/commands/UUIDCommand.java     | 93 +++++++++++++++++++
 .../plugins/CommandHandlerPlugin.java         |  2 +
 .../chomens_bot/plugins/DiscordPlugin.java    |  9 +-
 5 files changed, 171 insertions(+), 5 deletions(-)
 create mode 100644 src/main/java/me/chayapak1/chomens_bot/commands/TimeCommand.java
 create mode 100644 src/main/java/me/chayapak1/chomens_bot/commands/UUIDCommand.java

diff --git a/pom.xml b/pom.xml
index 78f66d8..1446dcf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -108,6 +108,12 @@
             <artifactId>JDA</artifactId>
             <version>4.4.0_350</version>
         </dependency>
+
+        <dependency>
+            <groupId>joda-time</groupId>
+            <artifactId>joda-time</artifactId>
+            <version>2.12.4</version>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/TimeCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/TimeCommand.java
new file mode 100644
index 0000000..e8b2fa0
--- /dev/null
+++ b/src/main/java/me/chayapak1/chomens_bot/commands/TimeCommand.java
@@ -0,0 +1,66 @@
+package me.chayapak1.chomens_bot.commands;
+
+import me.chayapak1.chomens_bot.command.Command;
+import me.chayapak1.chomens_bot.command.CommandContext;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.format.NamedTextColor;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TimeCommand implements Command {
+    public String name() { return "time"; }
+
+    public String description() {
+        return "Shows the date and time for the specified timezone";
+    }
+
+    public List<String> usage() {
+        final List<String> usages = new ArrayList<>();
+        usages.add("<timezone>");
+
+        return usages;
+    }
+
+    public List<String> alias() {
+        final List<String> aliases = new ArrayList<>();
+        aliases.add("dateandtime");
+        aliases.add("date");
+
+        return aliases;
+    }
+
+    public int trustLevel() {
+        return 0;
+    }
+
+    public Component execute(CommandContext context, String[] args, String[] fullArgs) {
+        final String timezone = args[0];
+
+        DateTimeZone zone;
+        try {
+            zone = DateTimeZone.forID(timezone);
+        } catch (IllegalArgumentException ignored) {
+            return Component.text("Invalid timezone (case-sensitive)").color(NamedTextColor.RED);
+        }
+
+        final DateTime dateTime = new DateTime(zone);
+
+        final DateTimeFormatter formatter = DateTimeFormat.forPattern("EEEE, MMMM d, YYYY, hh:mm:ss a");
+        final String formattedTime = formatter.print(dateTime);
+
+        context.sendOutput(
+                Component.translatable(
+                        "The current date and time for the timezone %s is: %s",
+                        Component.text(timezone).color(NamedTextColor.AQUA),
+                        Component.text(formattedTime).color(NamedTextColor.GREEN)
+                )
+        );
+
+        return Component.text("success");
+    }
+}
diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/UUIDCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/UUIDCommand.java
new file mode 100644
index 0000000..71197da
--- /dev/null
+++ b/src/main/java/me/chayapak1/chomens_bot/commands/UUIDCommand.java
@@ -0,0 +1,93 @@
+package me.chayapak1.chomens_bot.commands;
+
+import me.chayapak1.chomens_bot.Bot;
+import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry;
+import me.chayapak1.chomens_bot.command.Command;
+import me.chayapak1.chomens_bot.command.CommandContext;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.event.ClickEvent;
+import net.kyori.adventure.text.event.HoverEvent;
+import net.kyori.adventure.text.format.NamedTextColor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class UUIDCommand implements Command {
+    public String name() { return "uuid"; }
+
+    public String description() {
+        return "Shows your UUID or other player's UUID";
+    }
+
+    public List<String> usage() {
+        final List<String> usages = new ArrayList<>();
+        usages.add("[{username}]");
+
+        return usages;
+    }
+
+    public List<String> alias() {
+        final List<String> aliases = new ArrayList<>();
+        aliases.add("");
+
+        return aliases;
+    }
+
+    public int trustLevel() {
+        return 0;
+    }
+
+    public Component execute(CommandContext context, String[] args, String[] fullArgs) {
+        final Bot bot = context.bot();
+
+        if (args.length > 0) {
+            final MutablePlayerListEntry entry = bot.players().getEntry(String.join(" ", args));
+
+            if (entry == null) return Component.text("Invalid player name").color(NamedTextColor.RED);
+
+            final String name = entry.profile().getName();
+            final String uuid = entry.profile().getIdAsString();
+
+            context.sendOutput(
+                    Component.translatable(
+                            "%s's UUID: %s",
+                            Component.text(name),
+                            Component
+                                    .text(uuid)
+                                    .hoverEvent(
+                                            HoverEvent.showText(
+                                                    Component.text("Click here to copy the UUID to your clipboard").color(NamedTextColor.GREEN)
+                                            )
+                                    )
+                                    .clickEvent(
+                                            ClickEvent.copyToClipboard(uuid)
+                                    )
+                                    .color(NamedTextColor.AQUA)
+                    ).color(NamedTextColor.GREEN)
+            );
+        } else {
+            final MutablePlayerListEntry entry = context.sender();
+
+            final String uuid = entry.profile().getIdAsString();
+
+            context.sendOutput(
+                    Component.translatable(
+                            "Your UUID: %s",
+                            Component
+                                    .text(uuid)
+                                    .hoverEvent(
+                                            HoverEvent.showText(
+                                                    Component.text("Click here to copy the UUID to your clipboard").color(NamedTextColor.GREEN)
+                                            )
+                                    )
+                                    .clickEvent(
+                                            ClickEvent.copyToClipboard(uuid)
+                                    )
+                                    .color(NamedTextColor.AQUA)
+                    ).color(NamedTextColor.GREEN)
+            );
+        }
+
+        return Component.text("success");
+    }
+}
diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java
index 7340fa8..416af28 100644
--- a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java
+++ b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java
@@ -40,6 +40,8 @@ public class CommandHandlerPlugin {
         registerCommand(new ClearChatCommand());
         registerCommand(new ListCommand());
         registerCommand(new ServerEvalCommand());
+        registerCommand(new UUIDCommand());
+        registerCommand(new TimeCommand());
     }
 
     public void registerCommand (Command command) {
diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java
index 9f5d90c..338e41e 100644
--- a/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java
+++ b/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java
@@ -70,16 +70,17 @@ public class DiscordPlugin {
         for (Bot bot : Main.allBots) {
             String channelId = servers.get(bot.host() + ":" + bot.port());
 
-            boolean channelAlreadyAddedListeners = alreadyAddedListeners.getOrDefault(channelId, false);
-
               bot.addListener(new SessionAdapter() {
                   @Override
                   public void connected(ConnectedEvent event) {
                       boolean channelAlreadyAddedListeners = alreadyAddedListeners.getOrDefault(channelId, false);
-                      if (channelAlreadyAddedListeners) return;
 
                       sendMessageInstantly("Successfully connected to: " + "`" + bot.host() + ":" + bot.port() + "`", channelId);
 
+                      if (channelAlreadyAddedListeners) return;
+
+                      alreadyAddedListeners.put(channelId, true);
+
                       jda.addEventListener(new ListenerAdapter() {
                           @Override
                           public void onMessageReceived(@NotNull MessageReceivedEvent event) {
@@ -162,8 +163,6 @@ public class DiscordPlugin {
                           }
                       });
 
-                      alreadyAddedListeners.put(channelId, true);
-
                       bot.chat().addListener(new ChatPlugin.ChatListener() {
                           @Override
                           public void systemMessageReceived (String ignoredMessage, Component component) {