add uuid and time + discord fix idk

This commit is contained in:
ChomeNS 2023-03-27 12:47:53 +07:00
parent 5c3e0efef3
commit cb34dfff90
5 changed files with 171 additions and 5 deletions

View file

@ -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>

View file

@ -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");
}
}

View file

@ -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");
}
}

View file

@ -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) {

View file

@ -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) {