Added balance command and related queries
This commit introduces a new "balance" command, enabling users to check their in-game balance. This feature includes adding two new query files (`QueriesEconomy` and `QueriesUserDiscordId`), which handle SQL operations for fetching user ID and balance data. Also, number formatting utilities were updated to include a function for double's for proper display of balance.
This commit is contained in:
parent
a3d78c6059
commit
32f0935b4a
|
|
@ -54,7 +54,8 @@ public class CommandManager extends ListenerAdapter {
|
||||||
new CommandRemindMe(jda, this, modalManager),
|
new CommandRemindMe(jda, this, modalManager),
|
||||||
new CommandSoftLock(jda, this, lockedChannel),
|
new CommandSoftLock(jda, this, lockedChannel),
|
||||||
new CommandDataSuggestions(jda, this),
|
new CommandDataSuggestions(jda, this),
|
||||||
new CommandAuction(jda, this, selectMenuManager));
|
new CommandAuction(jda, this, selectMenuManager),
|
||||||
|
new CommandBal());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
package com.alttd.commandManager.commands;
|
||||||
|
|
||||||
|
import com.alttd.commandManager.DiscordCommand;
|
||||||
|
import com.alttd.database.queries.QueriesEconomy;
|
||||||
|
import com.alttd.database.queries.QueriesUserDiscordId;
|
||||||
|
import com.alttd.util.Util;
|
||||||
|
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.build.CommandData;
|
||||||
|
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||||
|
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class CommandBal extends DiscordCommand {
|
||||||
|
|
||||||
|
private final CommandData commandData;
|
||||||
|
|
||||||
|
public CommandBal() {
|
||||||
|
commandData = Commands.slash(getName(), "Get your balance")
|
||||||
|
.setDefaultPermissions(DefaultMemberPermissions.ENABLED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "bal";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(SlashCommandInteractionEvent event) {
|
||||||
|
long userId = event.getInteraction().getUser().getIdLong();
|
||||||
|
ReplyCallbackAction replyCallbackAction = event.deferReply(true);
|
||||||
|
QueriesUserDiscordId.getUUIDById(userId).thenAcceptAsync(optionalUUID -> {
|
||||||
|
if (optionalUUID.isEmpty()) {
|
||||||
|
replyCallbackAction.setEmbeds(Util.genericErrorEmbed("Error", "Unable to find your minecraft account.")).queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
UUID uuid = optionalUUID.get();
|
||||||
|
|
||||||
|
QueriesEconomy.getBalance(uuid).thenAcceptAsync(optionalBalance -> {
|
||||||
|
if (optionalBalance.isEmpty()) {
|
||||||
|
replyCallbackAction.setEmbeds(Util.genericErrorEmbed("Error", "Unable to find a balance for your minecraft account.")).queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String formattedBalance = Util.formatNumber(optionalBalance.get());
|
||||||
|
replyCallbackAction.setEmbeds(Util.genericSuccessEmbed("Balance", "Your balance is: $" + formattedBalance)).queue();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||||
|
event.replyChoices(List.of()).queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getHelpMessage() {
|
||||||
|
return "This command will show you your in game balance";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandData getCommandData() {
|
||||||
|
return commandData;
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/main/java/com/alttd/database/queries/QueriesEconomy.java
Normal file
34
src/main/java/com/alttd/database/queries/QueriesEconomy.java
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.alttd.database.queries;
|
||||||
|
|
||||||
|
import com.alttd.database.Database;
|
||||||
|
import com.alttd.util.Logger;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class QueriesEconomy {
|
||||||
|
|
||||||
|
public static CompletableFuture<Optional<Double>> getBalance(UUID uuid) {
|
||||||
|
String sql = "SELECT Balance FROM grove_balance WHERE player_uuid = ?";
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
try {
|
||||||
|
PreparedStatement preparedStatement = Database.getDatabase().getConnection().prepareStatement(sql);
|
||||||
|
|
||||||
|
preparedStatement.setString(1, uuid.toString());
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next())
|
||||||
|
return Optional.of(resultSet.getDouble("Balance"));
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
Logger.altitudeLogs.error(exception);
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.alttd.database.queries;
|
||||||
|
|
||||||
|
import com.alttd.database.Database;
|
||||||
|
import com.alttd.util.Logger;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class QueriesUserDiscordId {
|
||||||
|
|
||||||
|
public static CompletableFuture<Optional<UUID>> getUUIDById(long userId) {
|
||||||
|
String sql = "SELECT player_uuid FROM linked_accounts WHERE discord_id = ?";
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
try {
|
||||||
|
PreparedStatement preparedStatement = Database.getDatabase().getConnection().prepareStatement(sql);
|
||||||
|
|
||||||
|
preparedStatement.setLong(1, userId);
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next())
|
||||||
|
return Optional.of(UUID.fromString(resultSet.getString("player_uuid")));
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
Logger.altitudeLogs.error(exception);
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,8 @@ import net.dv8tion.jda.api.requests.RestAction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.text.NumberFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -214,14 +216,26 @@ public class Util {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatNumber(int price) {
|
public static String formatNumber(int price) {
|
||||||
String priceString = new StringBuilder(String.valueOf(price)).reverse().toString();
|
return formatIntegerPart(String.valueOf(price));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String formatNumber(double price) {
|
||||||
|
NumberFormat numberFormat = new DecimalFormat("0.00");
|
||||||
|
String priceString = numberFormat.format(price);
|
||||||
|
String[] parts = priceString.split("\\.");
|
||||||
|
String formattedIntegerPart = formatIntegerPart(parts[0]);
|
||||||
|
return formattedIntegerPart + "." + parts[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String formatIntegerPart(String integerPart) {
|
||||||
|
String reversedIntegerPart = new StringBuilder(integerPart).reverse().toString();
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i + 3 < priceString.length()) {
|
while (i + 3 < reversedIntegerPart.length()) {
|
||||||
sb.append(priceString, i, i + 3).append(",");
|
sb.append(reversedIntegerPart, i, i + 3).append(",");
|
||||||
i += 3;
|
i += 3;
|
||||||
}
|
}
|
||||||
sb.append(priceString.substring(i)).reverse();
|
sb.append(reversedIntegerPart.substring(i));
|
||||||
return "" + sb;
|
return sb.reverse().toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user