From ac3529ed8867e979b1217d9f33560c319ed2449c Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 24 Jun 2024 14:58:57 +0100 Subject: [PATCH] cleanup and finish the current scoresaber command args --- .../beatsaber/scoresaber/LinkSubCommand.java | 12 +++-- .../scoresaber/ScoreSaberCommand.java | 46 ++++++++++++------- .../beatsaber/scoresaber/UserSubCommand.java | 19 +++++++- .../java/cc/fascinated/bat/common/Colors.java | 10 ++++ .../cc/fascinated/bat/common/EmbedUtils.java | 6 +-- .../cc/fascinated/bat/model/user/BatUser.java | 2 +- 6 files changed, 68 insertions(+), 27 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/Colors.java diff --git a/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/LinkSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/LinkSubCommand.java index 5bfdee8..3cfbc25 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/LinkSubCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/LinkSubCommand.java @@ -1,6 +1,7 @@ package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber; import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken; import cc.fascinated.bat.model.user.BatUser; @@ -20,7 +21,6 @@ import org.springframework.stereotype.Component; */ @Component public class LinkSubCommand extends BatSubCommand { - private final ScoreSaberService scoreSaberService; private final UserService userService; @@ -33,13 +33,13 @@ public class LinkSubCommand extends BatSubCommand { @Override public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction, OptionMapping option) { if (option == null) { - interaction.reply("You must provide a ScoreSaber profile link to link your profile").queue(); + interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("Please provide a ScoreSaber profile link").build()).queue(); return; } String link = option.getAsString(); if (!link.contains("scoresaber.com/u/")) { - interaction.reply("Invalid ScoreSaber profile link").queue(); + interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("Invalid ScoreSaber profile link").build()).queue(); return; } @@ -50,12 +50,14 @@ public class LinkSubCommand extends BatSubCommand { ScoreSaberAccountToken account = scoreSaberService.getAccount(id); if (account == null) { - interaction.reply("Invalid ScoreSaber profile link").queue(); + interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("Invalid ScoreSaber profile link").build()).queue(); return; } ((ScoreSaberProfile) user.getProfile(ScoreSaberProfile.class)).setId(id); userService.saveUser(user); - interaction.reply("Successfully linked your ScoreSaber profile").queue(); + interaction.replyEmbeds(EmbedUtils.buildSuccessEmbed("Successfully linked your [ScoreSaber](%s) profile".formatted( + "https://scoresaber.com/u/%s".formatted(id) + )).build()).queue(); } } diff --git a/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/ScoreSaberCommand.java b/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/ScoreSaberCommand.java index 0545d64..169990a 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/ScoreSaberCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/ScoreSaberCommand.java @@ -1,7 +1,9 @@ package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber; import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.common.Colors; import cc.fascinated.bat.common.DateUtils; +import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.NumberUtils; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken; @@ -11,7 +13,6 @@ import cc.fascinated.bat.service.ScoreSaberService; import lombok.NonNull; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; @@ -45,39 +46,50 @@ public class ScoreSaberCommand extends BatCommand { @Override public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction, OptionMapping option) { + sendProfileEmbed(true, user, scoreSaberService, interaction); + } + + /** + * Builds the profile embed for the ScoreSaber profile + * + * @param user The user to build the profile embed for + * @param scoreSaberService The ScoreSaber service + * @param interaction The interaction + */ + public static void sendProfileEmbed(boolean isSelf, BatUser user, ScoreSaberService scoreSaberService, SlashCommandInteraction interaction) { ScoreSaberProfile profile = user.getProfile(ScoreSaberProfile.class); if (profile.getId() == null) { - interaction.reply("You must link your ScoreSaber profile first").queue(); + if (!isSelf) { + interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("%s does not have a linked ScoreSaber account" + .formatted(user.getDiscordUser().getAsMention())).build()).queue(); + } + interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("You do not have a linked ScoreSaber account").build()).queue(); return; } // todo: handle rate limits ScoreSaberAccountToken account = scoreSaberService.getAccount(profile.getId()); if (account == null) { - interaction.reply("Invalid ScoreSaber profile, please re-link your account.").queue(); + if (!isSelf) { + interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("%s has an invalid ScoreSaber account linked, please ask them to re-link their account" + .formatted(user.getDiscordUser().getAsMention())).build()).queue(); + } + interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("You have an invalid ScoreSaber account linked, please re-link your account") + .build()).queue(); return; } - interaction.replyEmbeds(buildProfileEmbed(account)).queue(); - } - - /** - * Builds the profile embed for the ScoreSaber profile - * - * @param account The account to build the embed for - * @return The built embed - */ - public static MessageEmbed buildProfileEmbed(ScoreSaberAccountToken account) { - return new EmbedBuilder() + interaction.replyEmbeds(new EmbedBuilder() .setAuthor(account.getName() + "'s Profile", "https://scoresaber.com/u/%s".formatted(account.getId()), "https://cdn.scoresaber.com/avatars/%s.jpg".formatted(account.getId())) .addField("Name", account.getName(), true) .addField("Country", account.getCountry(), true) - .addField("Rank", "#" + account.getRank(), true) - .addField("Country Rank", "#" + account.getCountryRank(), true) + .addField("Rank", "#" + NumberUtils.formatNumberCommas(account.getRank()), true) + .addField("Country Rank", "#" + NumberUtils.formatNumberCommas(account.getCountryRank()), true) .addField("PP", NumberUtils.formatNumberCommas(account.getPp()), true) .addField("Joined", "".formatted(DateUtils.getDateFromString(account.getFirstSeen()).toInstant().toEpochMilli()/1000), true) .setTimestamp(LocalDateTime.now()) - .build(); + .setColor(Colors.DEFAULT) + .build()).queue(); } } diff --git a/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/UserSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/UserSubCommand.java index b597d21..a647e82 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/UserSubCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/global/beatsaber/scoresaber/UserSubCommand.java @@ -3,11 +3,14 @@ package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber; import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.user.BatUser; +import cc.fascinated.bat.service.ScoreSaberService; +import cc.fascinated.bat.service.UserService; import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** @@ -15,9 +18,23 @@ import org.springframework.stereotype.Component; */ @Component public class UserSubCommand extends BatSubCommand { + private final ScoreSaberService scoreSaberService; + private final UserService userService; + + @Autowired + public UserSubCommand(@NonNull ScoreSaberService scoreSaberService, @NonNull UserService userService) { + this.scoreSaberService = scoreSaberService; + this.userService = userService; + } @Override public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction, OptionMapping option) { - interaction.reply("view someone elses profile").queue(); + BatUser target = userService.getUser(option.getAsUser().getId()); + if (target == null) { + interaction.reply("User not found").queue(); + return; + } + + ScoreSaberCommand.sendProfileEmbed(false, target, scoreSaberService, interaction); } } diff --git a/src/main/java/cc/fascinated/bat/common/Colors.java b/src/main/java/cc/fascinated/bat/common/Colors.java new file mode 100644 index 0000000..97f38d1 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/Colors.java @@ -0,0 +1,10 @@ +package cc.fascinated.bat.common; + +/** + * @author Fascinated (fascinated7) + */ +public class Colors { + public static final int DEFAULT = 0x3498DB; // Blue + public static final int ERROR = 0xFF0000; // Red + public static final int SUCCESS = 0x00FF00; // Green +} diff --git a/src/main/java/cc/fascinated/bat/common/EmbedUtils.java b/src/main/java/cc/fascinated/bat/common/EmbedUtils.java index f326acc..536483d 100644 --- a/src/main/java/cc/fascinated/bat/common/EmbedUtils.java +++ b/src/main/java/cc/fascinated/bat/common/EmbedUtils.java @@ -19,7 +19,7 @@ public class EmbedUtils { return new EmbedBuilder() .setDescription(description) .setTimestamp(LocalDateTime.now()) - .setColor(0x2F3136); + .setColor(Colors.DEFAULT); } /** @@ -32,7 +32,7 @@ public class EmbedUtils { return new EmbedBuilder() .setDescription(description) .setTimestamp(LocalDateTime.now()) - .setColor(0xFF0000); + .setColor(Colors.ERROR); } /** @@ -45,6 +45,6 @@ public class EmbedUtils { return new EmbedBuilder() .setDescription(description) .setTimestamp(LocalDateTime.now()) - .setColor(0x00FF00); + .setColor(Colors.SUCCESS); } } diff --git a/src/main/java/cc/fascinated/bat/model/user/BatUser.java b/src/main/java/cc/fascinated/bat/model/user/BatUser.java index ea3ca0b..87002fa 100644 --- a/src/main/java/cc/fascinated/bat/model/user/BatUser.java +++ b/src/main/java/cc/fascinated/bat/model/user/BatUser.java @@ -60,7 +60,7 @@ public class BatUser { * * @return the guild */ - private User getDiscordUser() { + public User getDiscordUser() { return DiscordService.JDA.getUserById(id); } }