Skip to content

Commit

Permalink
Added new translations for /card command & paginated card list
Browse files Browse the repository at this point in the history
  • Loading branch information
Kub94ekCZ committed Dec 14, 2024
1 parent 910af9e commit 40d7d85
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 36 deletions.
67 changes: 67 additions & 0 deletions src/main/java/me/kub94ek/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import me.kub94ek.card.CardType;
import me.kub94ek.command.CommandManager;
import me.kub94ek.command.impl.commands.CardCommand;
import me.kub94ek.command.impl.executors.CardCommandExecutor;
import me.kub94ek.data.database.Database;
import me.kub94ek.data.stats.Stats;
import me.kub94ek.image.CardCreator;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.components.ActionRow;
Expand All @@ -21,8 +24,11 @@
import net.dv8tion.jda.api.interactions.modals.Modal;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.FileUpload;
import org.jetbrains.annotations.NotNull;

import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
Expand All @@ -33,6 +39,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static me.kub94ek.command.impl.executors.CardCommandExecutor.pages;

public class Main extends ListenerAdapter {
private static JDA jda;
private static Database database;
Expand Down Expand Up @@ -147,6 +155,37 @@ private static void spawnCard(String channelId) {
});
}

@Override
public void onStringSelectInteraction(@NotNull StringSelectInteractionEvent event) {
if (event.getComponentId().startsWith("card-list")) {
final CardType[] cardType = {null};
final int[] stats = {0, 0};

StringBuilder messageBuilder = new StringBuilder();
database.getUserCards(event.getMember().getId()).forEach((card) -> {
if (card.getId().equals(event.getInteraction().getSelectedOptions().getFirst().getValue())) {
messageBuilder.append(card);
cardType[0] = card.getType();
stats[0] = card.getAtkBonus();
stats[1] = card.getHpBonus();
}
});

try {
CardCreator.createCardImage(new Card("id", "owner", cardType[0], stats[0], stats[1]));
} catch (IOException | FontFormatException e) {
e.printStackTrace();
}

event.reply(messageBuilder.toString())
.addFiles(FileUpload.fromData(
new File("image.jpg")
))
.setEphemeral(true)
.queue();

}
}

@Override
public void onButtonInteraction(ButtonInteractionEvent e) {
Expand All @@ -169,6 +208,26 @@ public void onButtonInteraction(ButtonInteractionEvent e) {
.build();

e.replyModal(modal).queue();
} else if (e.getComponentId().equals("previous-page")) {
if (pages.containsKey(e.getMember().getId())) {
int page = pages.get(e.getMember().getId()) - 1;
e.getMessage().delete().queue();
var message = CardCommandExecutor.createListMessage(
e.reply("Listing all your cards:"),
database.getUserCards(e.getMember().getId()),
page);
message.setEphemeral(true).queue(sentMessage -> pages.put(sentMessage.getId(), page));
}
} else if (e.getComponentId().equals("next-page")) {
if (pages.containsKey(e.getMember().getId())) {
int page = pages.get(e.getMember().getId()) + 1;
e.getMessage().delete().queue();
var message = CardCommandExecutor.createListMessage(
e.reply("Listing all your cards:"),
database.getUserCards(e.getMember().getId()),
page);
message.setEphemeral(true).queue(sentMessage -> pages.put(e.getMember().getId(), page));
}
}
}

Expand Down Expand Up @@ -237,6 +296,14 @@ 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();
}
});
}));

commandManager.registerCommand(new CardCommand());
}

Expand Down
25 changes: 15 additions & 10 deletions src/main/java/me/kub94ek/command/impl/commands/CardCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ public CommandExecutor getExecutor() {
@Override
public SlashCommandData getCommand() {
SlashCommandData command = Commands.slash("card", "Command for card manipulation");
command.setDescriptionLocalizations(getDescriptionTranslations());
//command.setDescriptionLocalizations(getDescriptionTranslations());
command.addSubcommands(
new SubcommandData("list", "List your cards")
.setDescriptionLocalizations(Map.of(
DiscordLocale.CZECH, "Ukáže seznam vašich karet"
DiscordLocale.CZECH, "Ukáže seznam vašich karet",
DiscordLocale.SPANISH, "Muestra una lista de sus tarjetas"
)),
new SubcommandData("give", "Give your card to somebody")
.addOptions(
Expand All @@ -38,7 +39,8 @@ public SlashCommandData getCommand() {
true
).setDescriptionLocalizations(
Map.of(
DiscordLocale.CZECH, "Komu kartu darovat"
DiscordLocale.CZECH, "Komu kartu darovat",
DiscordLocale.SPANISH, "A quién regalar la tarjeta"
)
),
new OptionData(
Expand All @@ -47,28 +49,31 @@ public SlashCommandData getCommand() {
true
).setDescriptionLocalizations(
Map.of(
DiscordLocale.CZECH, "ID karty"
DiscordLocale.CZECH, "ID karty",
DiscordLocale.SPANISH, "Tarjeta ID"
)
)
)
.setDescriptionLocalizations(Map.of(
DiscordLocale.CZECH, "Darujte někomu vaši kartu"
DiscordLocale.CZECH, "Darujte někomu vaši kartu",
DiscordLocale.SPANISH, "Regale su tarjeta a alguien"
)),
new SubcommandData("last", "Shows you the last caught card")
.setDescriptionLocalizations(Map.of(
DiscordLocale.CZECH, "Ukáže seznam vašich karet"
DiscordLocale.CZECH, "Ukáže vaši poslední získanou kartu",
DiscordLocale.SPANISH, "Muestra su última tarjeta repartida"
))
);

return command;
}

private Map<DiscordLocale, String> getDescriptionTranslations() {
/*private Map<DiscordLocale, String> getDescriptionTranslations() {
return Map.of(
DiscordLocale.CZECH, "Příkaz pro manipulaci s kartami",
DiscordLocale.GERMAN, "Command für Kartenmanipulation"
//DiscordLocale.SPANISH, ""
DiscordLocale.GERMAN, "Command für Kartenmanipulation",
DiscordLocale.SPANISH, "Comando para manipulación de tarjetas"
);
}
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
import net.dv8tion.jda.api.utils.FileUpload;

import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class CardCommandExecutor implements CommandExecutor {
public class CardCommandExecutor extends ListenerAdapter implements CommandExecutor {

public static final HashMap<String, Integer> pages = new HashMap<>();


@Override
public void executeCommand(SlashCommandInteractionEvent event) {
Expand All @@ -32,30 +40,13 @@ public void executeCommand(SlashCommandInteractionEvent event) {
return;
}

StringSelectMenu.Builder builder = StringSelectMenu.create("card-list");
var message = event.reply("Listing all your cards:");
List<Card> cards = database.getUserCards(memberId);
int idx = 0;
if (cards.size() >= 25) {
while (cards.size() > 25) {
for (int i = 0; i < 25; i++) {
builder.addOption(cards.getFirst().getListString(), cards.getFirst().getId(),
Emoji.fromCustom(cards.getFirst().getType().emoji));
cards.removeFirst();
}
message.addActionRow(builder.build());
idx++;
builder = StringSelectMenu.create("card-list" + idx);
}
}

StringSelectMenu.Builder finalBuilder = builder;
cards.forEach((card) -> {
finalBuilder.addOption(card.getListString(), card.getId(),
Emoji.fromCustom(card.getType().emoji));
});
message.addActionRow(builder.build());
message.setEphemeral(true).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();
Expand All @@ -66,6 +57,11 @@ public void executeCommand(SlashCommandInteractionEvent event) {
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))) {
Expand Down Expand Up @@ -102,8 +98,8 @@ public void executeCommand(SlashCommandInteractionEvent event) {

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();
event.getChannel().sendMessage(event.getMember().getAsMention() + " gave the card `"
+ database.getCard(cardId).getListString() + "` to " + user.getAsMention()).queue();

}
case "last" -> {
Expand Down Expand Up @@ -140,4 +136,33 @@ public void executeCommand(SlashCommandInteractionEvent event) {
}
}

public static ReplyCallbackAction createListMessage(ReplyCallbackAction message, List<Card> cards, int page) {

StringSelectMenu.Builder builder = StringSelectMenu.create("card-list");

for (int i = page*25; i < (Math.min(cards.size(), 25*(page+1))); i++) {
builder.addOption(
cards.get(i).getListString(),
cards.get(i).getId(),
Emoji.fromCustom(cards.get(i).getType().emoji)
);
}

message.addActionRow(builder.build());

List<Button> buttons = new ArrayList<>();
if (page > 0) {
buttons.add(Button.secondary("previous-page", "⮜"));
}
if (25*(page+1) < cards.size()) {
buttons.add(Button.secondary("next-page", "⮞"));
}

if (!buttons.isEmpty()) {
message.addActionRow(buttons);
}

return message;
}

}

0 comments on commit 40d7d85

Please sign in to comment.