Skip to content

Commit

Permalink
Added coin command and revamped command manager (a bit)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kub94ekCZ committed Dec 14, 2024
1 parent 40d7d85 commit 11f139e
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 110 deletions.
15 changes: 7 additions & 8 deletions src/main/java/me/kub94ek/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.kub94ek.card.CardType;
import me.kub94ek.command.CommandManager;
import me.kub94ek.command.impl.commands.CardCommand;
import me.kub94ek.command.impl.commands.CoinCommand;
import me.kub94ek.command.impl.executors.CardCommandExecutor;
import me.kub94ek.data.database.Database;
import me.kub94ek.data.stats.Stats;
Expand Down Expand Up @@ -296,15 +297,13 @@ public void onModalInteraction(ModalInteractionEvent event) {
}

private void registerGuildCommands() {
jda.getGuilds().forEach(guild -> guild.retrieveCommands().queue(commands -> {
commands.forEach(command -> {
if (command.getApplicationId().equals("1274368616179175485")) {
command.delete().queue();
}
});
}));
jda.getGuilds().forEach(guild -> guild.retrieveCommands().queue(commands -> commands.forEach(command -> {
if (command.getApplicationId().equals("1274368616179175485")) {
command.delete().queue();
}
})));

commandManager.registerCommand(new CardCommand());
commandManager.registerCommands(List.of(new CardCommand(), new CoinCommand()));
}

public static JDA getJda() {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/me/kub94ek/command/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public CommandManager(JDA jda) {
this.jda = jda;
}

public void registerCommand(Command command) {
registerCommand(command.getCommand(), command.getExecutor());
}

public void registerCommand(SlashCommandData command, CommandExecutor commandExecutor) {
jda.getGuilds().forEach(guild -> jda.updateCommands().addCommands(command).queue());
commandExecutors.put(command.getName(), commandExecutor);
public void registerCommands(List<Command> commands) {
List<SlashCommandData> slashCommandData = new ArrayList<>();
commands.forEach(command -> {
slashCommandData.add(command.getCommand());
commandExecutors.put(command.getCommand().getName(), command.getExecutor());
});

jda.getGuilds().forEach(guild -> jda.updateCommands().addCommands(slashCommandData).queue());
}

@Override
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/me/kub94ek/command/impl/commands/CoinCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.kub94ek.command.impl.commands;

import me.kub94ek.command.Command;
import me.kub94ek.command.CommandExecutor;
import me.kub94ek.command.impl.executors.CoinCommandExecutor;
import net.dv8tion.jda.api.interactions.DiscordLocale;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;

import java.util.Map;

public class CoinCommand implements Command {
private static final CoinCommandExecutor executor = new CoinCommandExecutor();

@Override
public CommandExecutor getExecutor() {
return executor;
}

@Override
public SlashCommandData getCommand() {
SlashCommandData command = Commands.slash("coin", "Command for managing coins");
command.addSubcommands(
new SubcommandData("bal", "Shows you your coin balance")
.setDescriptionLocalizations(
Map.of(
DiscordLocale.CZECH, "Ukáže vám váš zůstatek mincí",
DiscordLocale.SPANISH, "Muestra el saldo de monedas"
)
),
new SubcommandData("give", "Gives coins to somebody")
.addOptions(
new OptionData(
OptionType.USER,
"user", "The user to give the coins to",
true
).setDescriptionLocalizations(
Map.of(
DiscordLocale.CZECH, "Komu mince darovat",
DiscordLocale.SPANISH, "A quién regalar las monedas"
)
),
new OptionData(
OptionType.INTEGER,
"coins", "How many coins to give",
true
).setDescriptionLocalizations(
Map.of(
DiscordLocale.CZECH, "Kolik mincí chcete darovat",
DiscordLocale.SPANISH, "Cuántas monedas desea regalar"
)
)
)
.setDescriptionLocalizations(
Map.of(
DiscordLocale.CZECH, "Darujte někomu vaše mince",
DiscordLocale.SPANISH, "Regala sus monedas a alguien"
)
)
);


return command;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,106 +34,106 @@ public void executeCommand(SlashCommandInteractionEvent event) {
Database database = Main.getDatabase();

switch (Objects.requireNonNull(event.getSubcommandName())) {
case "list" -> {
if (database.getUserCards(memberId).isEmpty()) {
event.reply("You don't have any card yet.").setEphemeral(true).queue();
return;
}


var message = createListMessage(
event.reply("Listing all your cards:"),
database.getUserCards(memberId),
0
);
message.setEphemeral(true).queue(sentMessage -> pages.put(memberId, 0));
case "list" -> {
if (database.getUserCards(memberId).isEmpty()) {
event.reply("You don't have any card yet.").setEphemeral(true).queue();
return;
}
case "give" -> {
User user = Objects.requireNonNull(event.getOption("user")).getAsUser();
String cardId = Objects.requireNonNull(event.getOption("id")).getAsString();

if (!database.cardExists(cardId)) {
event.reply("Unknown card").setEphemeral(true).queue();
return;
}

if (user.isBot() || user.isSystem()) {
event.reply("Invalid user").setEphemeral(true).queue();
return;
}

/*if ((battleData.containsKey(memberId) && battleData.get(memberId).containsCard(cardId))
|| (battles.containsKey(memberId)
&& startedBattleData.get(battles.get(memberId)).cardHealth.containsKey(cardId))) {
event.reply("This card is currently not available").setEphemeral(true).queue();
return;
}*/

boolean[] owns = {false};

database.getUserCards(memberId).forEach(card -> {
if (card.getId().equals(cardId)) {
owns[0] = true;
}
});

if (!owns[0]) {
event.reply("You don't own this card").setEphemeral(true).queue();
return;
}

try {
database.moveCard(cardId, user.getId());
} catch (SQLException e) {
e.printStackTrace();
event.reply("""
Unexpected database error occurred.\
Contact <@1064940406560788540> if this continues happening. \
Don't forget to provide the error ID.\
Error ID: jurhOd4zf9gUaAUP5X\s"""
).setEphemeral(true).queue();
}

event.reply("You gave the card " + database.getCard(cardId).getListString() +
" to " + user.getAsMention()).setEphemeral(true).queue();
event.getChannel().sendMessage(event.getMember().getAsMention() + " gave the card `"
+ database.getCard(cardId).getListString() + "` to " + user.getAsMention()).queue();



var message = createListMessage(
event.reply("Listing all your cards:"),
database.getUserCards(memberId),
0
);
message.setEphemeral(true).queue(sentMessage -> pages.put(memberId, 0));
}
case "give" -> {
User user = Objects.requireNonNull(event.getOption("user")).getAsUser();
String cardId = Objects.requireNonNull(event.getOption("id")).getAsString();

if (!database.cardExists(cardId)) {
event.reply("Unknown card").setEphemeral(true).queue();
return;
}
case "last" -> {
if (database.getUserCards(memberId).isEmpty()) {
event.reply("You don't have any card yet.").setEphemeral(true).queue();
return;
}

Card card = database.getUserCards(memberId).getLast();
event.deferReply(true).queue();

try {
CardCreator.createCardImage(new Card("id", "owner", card.getType(),
card.getAtkBonus(), card.getHpBonus()));
} catch (IOException | FontFormatException e) {
e.printStackTrace();
event.reply("""
Unexpected database error occurred.\
Contact <@1064940406560788540> if this continues happening. \
Don't forget to provide the error ID.\
Error ID: papsq6Z8VTFCZKjmst\s"""
).setEphemeral(true).queue();

if (user.isBot() || user.isSystem()) {
event.reply("Invalid user").setEphemeral(true).queue();
return;
}

/*if ((battleData.containsKey(memberId) && battleData.get(memberId).containsCard(cardId))
|| (battles.containsKey(memberId)
&& startedBattleData.get(battles.get(memberId)).cardHealth.containsKey(cardId))) {
event.reply("This card is currently not available").setEphemeral(true).queue();
return;
}*/

boolean[] owns = {false};

database.getUserCards(memberId).forEach(card -> {
if (card.getId().equals(cardId)) {
owns[0] = true;
}

event.getHook().sendMessage(card.toString())
.addFiles(FileUpload.fromData(
new File("image.jpg")
))
.queue();

});

if (!owns[0]) {
event.reply("You don't own this card").setEphemeral(true).queue();
return;
}

try {
database.moveCard(cardId, user.getId());
} catch (SQLException e) {
e.printStackTrace();
event.reply("""
Unexpected database error occurred.\
Contact <@1064940406560788540> if this continues happening. \
Don't forget to provide the error ID.\
Error ID: jurhOd4zf9gUaAUP5X\s"""
).setEphemeral(true).queue();
}

event.reply("You gave the card " + database.getCard(cardId).getListString() +
" to " + user.getAsMention()).setEphemeral(true).queue();
event.getChannel().sendMessage(event.getMember().getAsMention() + " gave the card `"
+ database.getCard(cardId).getListString() + "` to " + user.getAsMention()).queue();

}
case "last" -> {
if (database.getUserCards(memberId).isEmpty()) {
event.reply("You don't have any card yet.").setEphemeral(true).queue();
return;
}

Card card = database.getUserCards(memberId).getLast();
event.deferReply(true).queue();

try {
CardCreator.createCardImage(new Card("id", "owner", card.getType(),
card.getAtkBonus(), card.getHpBonus()));
} catch (IOException | FontFormatException e) {
e.printStackTrace();
event.reply("""
Unexpected database error occurred.\
Contact <@1064940406560788540> if this continues happening. \
Don't forget to provide the error ID.\
Error ID: papsq6Z8VTFCZKjmst\s"""
).setEphemeral(true).queue();
}

event.getHook().sendMessage(card.toString())
.addFiles(FileUpload.fromData(
new File("image.jpg")
))
.queue();

}
}
}

public static ReplyCallbackAction createListMessage(ReplyCallbackAction message, List<Card> cards, int page) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package me.kub94ek.command.impl.executors;

import me.kub94ek.Main;
import me.kub94ek.command.CommandExecutor;
import me.kub94ek.data.database.Database;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;

public class CoinCommandExecutor implements CommandExecutor {
@Override
public void executeCommand(SlashCommandInteractionEvent event) {
String memberId = event.getMember().getId();
Database database = Main.getDatabase();

switch (event.getSubcommandName()) {
case "bal" -> event.reply(
"Your balance: " + database.getCoins(memberId) + " coins"
).setEphemeral(true).queue();
case "give" -> {
OptionMapping userOption = event.getOption("user");
OptionMapping coinsOption = event.getOption("coins");
User user = userOption.getAsUser();
int coins = coinsOption.getAsInt();

if (coins > database.getCoins(memberId)) {
event.reply("You don't have enough coins!").setEphemeral(true).queue();
return;
}

if (user.isBot() || user.isSystem() || user.getId().equals(memberId)) {
event.reply("Invalid user!").setEphemeral(true).queue();
return;
}

database.giveCoins(memberId, user.getId(), coins);
event.reply("Gave " + coins + " coins to <@" + user.getId() + ">").setEphemeral(true).queue();

}
}
}

}

0 comments on commit 11f139e

Please sign in to comment.