Start working on CommandSeen.java
This commit is contained in:
parent
70f4bf13d6
commit
5519ea0ad4
|
|
@ -42,7 +42,9 @@ public class CommandManager extends ListenerAdapter {
|
|||
new CommandUpdateCommands(jda, this),
|
||||
new CommandEvidence(jda, modalManager, this),
|
||||
new CommandFlag(jda, this),
|
||||
new CommandHistory(jda, this));
|
||||
new CommandHistory(jda, this),
|
||||
new CommandSeen(jda, this)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
134
src/main/java/com/alttd/commandManager/commands/CommandSeen.java
Normal file
134
src/main/java/com/alttd/commandManager/commands/CommandSeen.java
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
package com.alttd.commandManager.commands;
|
||||
|
||||
import com.alttd.commandManager.CommandManager;
|
||||
import com.alttd.commandManager.DiscordCommand;
|
||||
import com.alttd.config.MessagesConfig;
|
||||
import com.alttd.database.queries.QueriesUserUUID;
|
||||
import com.alttd.database.queries.queriesSeen.PlaytimeSeen;
|
||||
import com.alttd.database.queries.queriesSeen.SeenQueries;
|
||||
import com.alttd.util.Util;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
import net.dv8tion.jda.api.requests.RestAction;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CommandSeen extends DiscordCommand {
|
||||
|
||||
private final CommandData commandData;
|
||||
|
||||
public CommandSeen(JDA jda, CommandManager commandManager) {
|
||||
commandData = Commands.slash(getName(), "Check when a player was last online.")
|
||||
.addOption(OptionType.STRING, "playername", "The playername or uuid you want to check.", true, true)
|
||||
.setGuildOnly(true)
|
||||
.setDefaultPermissions(DefaultMemberPermissions.ENABLED);
|
||||
|
||||
Util.registerCommand(commandManager, jda, commandData, getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "seen";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
OptionMapping option = event.getOption("playername");
|
||||
if (option == null) {
|
||||
return;
|
||||
}
|
||||
String playerName = option.getAsString();
|
||||
UUID uuid;
|
||||
try{
|
||||
uuid = UUID.fromString(playerName);
|
||||
playerName = QueriesUserUUID.getUsernameByUUID(uuid);
|
||||
} catch (IllegalArgumentException exception){
|
||||
uuid = QueriesUserUUID.getUUIDByUsername(playerName);
|
||||
}
|
||||
|
||||
if (uuid == null) {
|
||||
error(event);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO Determine if a player is online or not? currently not possible?
|
||||
PlaytimeSeen lastSeen = SeenQueries.getLastSeen(uuid);
|
||||
if (lastSeen == null || lastSeen.getLastSeen() == 0) {
|
||||
error(event);
|
||||
return;
|
||||
}
|
||||
|
||||
String SEEN_FORMAT = "Player <gold>%player%</gold> has been %online/offline% for %time% on %server%.</white>";
|
||||
event.replyEmbeds(Util.genericSuccessEmbed("Success",
|
||||
SEEN_FORMAT
|
||||
.replaceAll("%player%", playerName)
|
||||
// .replaceAll("%online/offline%", Config.Messages.SEEN_OFFLINE_FORMAT.getMessage())
|
||||
.replaceAll("%time%", getPassedTime(lastSeen.getLastSeen()))
|
||||
.replaceAll("%server%", lastSeen.getServer())
|
||||
))
|
||||
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||
event.replyChoices(Collections.emptyList())
|
||||
.queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return MessagesConfig.HELP_SEEN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getCommandData() {
|
||||
return commandData;
|
||||
}
|
||||
|
||||
private static String getPassedTime(Long time) {
|
||||
return Util.convertTime(new Date().getTime() - time);
|
||||
}
|
||||
|
||||
private void error(SlashCommandInteractionEvent event) {
|
||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "No recorded time."))
|
||||
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
||||
}
|
||||
|
||||
// private static Component getOnlineSeen(String playerName, Player player) {
|
||||
// UUID uuid = player.getUniqueId();
|
||||
// PlaytimePlayer playtimePlayer = Queries.getPlaytimePlayer(uuid);
|
||||
//
|
||||
// if (playtimePlayer == null) return MiniMessage.get().parse(Config.Messages.PLAYER_NOT_FOUND.getMessage().replaceAll("%player%", playerName));
|
||||
//
|
||||
// return getOnlineSeen(playtimePlayer, player);
|
||||
// }
|
||||
// private static Component getOfflineSeen(String playerName) {
|
||||
// UUID uuid = Utilities.getPlayerUUID(playerName);
|
||||
//
|
||||
// if (uuid == null) return MiniMessage.get().parse(Config.Messages.PLAYER_NOT_FOUND.getMessage().replaceAll("%player%", playerName));
|
||||
//
|
||||
// return getOfflineSeen(uuid);
|
||||
// }
|
||||
//
|
||||
// private static Component getOfflineSeen(UUID uuid) {
|
||||
// if (uuid == null) return Component.empty();
|
||||
//
|
||||
// PlaytimeSeen lastSeen = Queries.getLastSeen(uuid);
|
||||
//
|
||||
// if (lastSeen == null || lastSeen.getLastSeen() == 0) return MiniMessage.get().parse(Config.Messages.SEEN_TIME_NULL.getMessage());
|
||||
//
|
||||
// return MiniMessage.get().parse(Config.Messages.SEEN_FORMAT.getMessage()
|
||||
// .replaceAll("%player%", Utilities.getPlayerName(uuid))
|
||||
// .replaceAll("%online/offline%", Config.Messages.SEEN_OFFLINE_FORMAT.getMessage())
|
||||
// .replaceAll("%time%", getPassedTime(lastSeen.getLastSeen()))
|
||||
// .replaceAll("%server%", lastSeen.getServer()));
|
||||
// }
|
||||
}
|
||||
|
|
@ -17,10 +17,12 @@ public class MessagesConfig extends AbstractConfig {
|
|||
public static String HELP_HELP = "`/help`: Shows help menu";
|
||||
public static String HELP_SUGGESTION = "`/suggestion`: Opens suggestion form";
|
||||
public static String HELP_MESSAGE_TEMPLATE = "<commands>";
|
||||
public static String HELP_SEEN = "<commands>";
|
||||
private static void loadHelp() {
|
||||
HELP_HELP = messagesConfig.getString("help.help", HELP_HELP);
|
||||
HELP_SUGGESTION = messagesConfig.getString("help.suggestion", HELP_SUGGESTION);
|
||||
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
|
||||
HELP_SEEN = messagesConfig.getString("help.seen", HELP_SEEN);
|
||||
}
|
||||
private static void loadPollHelp() {
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,6 @@ public class QueriesUserUUID {
|
|||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
return "user not found";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
package com.alttd.database.queries.queriesSeen;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlaytimeSeen {
|
||||
private final UUID uuid;
|
||||
private String server;
|
||||
private Long lastSeen;
|
||||
|
||||
public PlaytimeSeen(UUID uuid, String server, Long lastSeen) {
|
||||
this.uuid = uuid;
|
||||
this.server = server;
|
||||
this.lastSeen = lastSeen;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public String getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public Long getLastSeen() {
|
||||
return lastSeen;
|
||||
}
|
||||
|
||||
public void setLastSeen(String server, long lastSeen) {
|
||||
this.server = server;
|
||||
this.lastSeen = lastSeen;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.alttd.database.queries.queriesSeen;
|
||||
|
||||
import com.alttd.database.Database;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SeenQueries {
|
||||
|
||||
public static PlaytimeSeen getLastSeen(UUID uuid) {
|
||||
|
||||
try {
|
||||
PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement("SELECT server_name, last_seen FROM playtime WHERE uuid = ? AND last_seen IS NOT NULL ORDER BY last_seen DESC LIMIT 1");
|
||||
statement.setString(1, uuid.toString());
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
PlaytimeSeen playtimeSeen = new PlaytimeSeen(uuid, resultSet.getString("server_name"), resultSet.getLong("last_seen"));
|
||||
return playtimeSeen;
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ import java.awt.*;
|
|||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Util {
|
||||
|
|
@ -186,4 +187,27 @@ public class Util {
|
|||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
public static String convertTime(long timeInMillis){
|
||||
return convertTime((int) TimeUnit.MILLISECONDS.toMinutes(timeInMillis));
|
||||
}
|
||||
|
||||
private static String convertTime(int timeInMinutes) {
|
||||
int days = (int) TimeUnit.MINUTES.toDays(timeInMinutes);
|
||||
int hours = (int) (TimeUnit.MINUTES.toHours(timeInMinutes) - TimeUnit.DAYS.toHours(days));
|
||||
int minutes = (int) (TimeUnit.MINUTES.toMinutes(timeInMinutes) - TimeUnit.HOURS.toMinutes(hours)
|
||||
- TimeUnit.DAYS.toMinutes(days));
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
if (days != 0) {
|
||||
stringBuilder.append(days).append(days == 1 ? " day, " : " days, ");
|
||||
}
|
||||
if (hours != 0) {
|
||||
stringBuilder.append(hours).append(hours == 1 ? " hour, " : " hours, ");
|
||||
}
|
||||
stringBuilder.append(minutes).append(minutes == 1 ? " minute, " : " minutes, ");
|
||||
|
||||
return stringBuilder.substring(0, stringBuilder.length() - 2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user