cleanup and finish the current scoresaber command args

This commit is contained in:
Lee 2024-06-24 14:58:57 +01:00
parent 82e241f6ac
commit ac3529ed88
6 changed files with 68 additions and 27 deletions

@ -1,6 +1,7 @@
package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber; package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber;
import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken; import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken;
import cc.fascinated.bat.model.user.BatUser; import cc.fascinated.bat.model.user.BatUser;
@ -20,7 +21,6 @@ import org.springframework.stereotype.Component;
*/ */
@Component @Component
public class LinkSubCommand extends BatSubCommand { public class LinkSubCommand extends BatSubCommand {
private final ScoreSaberService scoreSaberService; private final ScoreSaberService scoreSaberService;
private final UserService userService; private final UserService userService;
@ -33,13 +33,13 @@ public class LinkSubCommand extends BatSubCommand {
@Override @Override
public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction, OptionMapping option) { public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction, OptionMapping option) {
if (option == null) { 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; return;
} }
String link = option.getAsString(); String link = option.getAsString();
if (!link.contains("scoresaber.com/u/")) { if (!link.contains("scoresaber.com/u/")) {
interaction.reply("Invalid ScoreSaber profile link").queue(); interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("Invalid ScoreSaber profile link").build()).queue();
return; return;
} }
@ -50,12 +50,14 @@ public class LinkSubCommand extends BatSubCommand {
ScoreSaberAccountToken account = scoreSaberService.getAccount(id); ScoreSaberAccountToken account = scoreSaberService.getAccount(id);
if (account == null) { if (account == null) {
interaction.reply("Invalid ScoreSaber profile link").queue(); interaction.replyEmbeds(EmbedUtils.buildErrorEmbed("Invalid ScoreSaber profile link").build()).queue();
return; return;
} }
((ScoreSaberProfile) user.getProfile(ScoreSaberProfile.class)).setId(id); ((ScoreSaberProfile) user.getProfile(ScoreSaberProfile.class)).setId(id);
userService.saveUser(user); 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();
} }
} }

@ -1,7 +1,9 @@
package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber; package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber;
import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.common.Colors;
import cc.fascinated.bat.common.DateUtils; import cc.fascinated.bat.common.DateUtils;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.NumberUtils; import cc.fascinated.bat.common.NumberUtils;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken; import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken;
@ -11,7 +13,6 @@ import cc.fascinated.bat.service.ScoreSaberService;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member; 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.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.OptionType;
@ -45,39 +46,50 @@ public class ScoreSaberCommand extends BatCommand {
@Override @Override
public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction, OptionMapping option) { 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); ScoreSaberProfile profile = user.getProfile(ScoreSaberProfile.class);
if (profile.getId() == null) { 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; return;
} }
// todo: handle rate limits // todo: handle rate limits
ScoreSaberAccountToken account = scoreSaberService.getAccount(profile.getId()); ScoreSaberAccountToken account = scoreSaberService.getAccount(profile.getId());
if (account == null) { 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; return;
} }
interaction.replyEmbeds(buildProfileEmbed(account)).queue(); interaction.replyEmbeds(new EmbedBuilder()
}
/**
* 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()
.setAuthor(account.getName() + "'s Profile", "https://scoresaber.com/u/%s".formatted(account.getId()), .setAuthor(account.getName() + "'s Profile", "https://scoresaber.com/u/%s".formatted(account.getId()),
"https://cdn.scoresaber.com/avatars/%s.jpg".formatted(account.getId())) "https://cdn.scoresaber.com/avatars/%s.jpg".formatted(account.getId()))
.addField("Name", account.getName(), true) .addField("Name", account.getName(), true)
.addField("Country", account.getCountry(), true) .addField("Country", account.getCountry(), true)
.addField("Rank", "#" + account.getRank(), true) .addField("Rank", "#" + NumberUtils.formatNumberCommas(account.getRank()), true)
.addField("Country Rank", "#" + account.getCountryRank(), true) .addField("Country Rank", "#" + NumberUtils.formatNumberCommas(account.getCountryRank()), true)
.addField("PP", NumberUtils.formatNumberCommas(account.getPp()), true) .addField("PP", NumberUtils.formatNumberCommas(account.getPp()), true)
.addField("Joined", "<t:%s>".formatted(DateUtils.getDateFromString(account.getFirstSeen()).toInstant().toEpochMilli()/1000), true) .addField("Joined", "<t:%s>".formatted(DateUtils.getDateFromString(account.getFirstSeen()).toInstant().toEpochMilli()/1000), true)
.setTimestamp(LocalDateTime.now()) .setTimestamp(LocalDateTime.now())
.build(); .setColor(Colors.DEFAULT)
.build()).queue();
} }
} }

@ -3,11 +3,14 @@ package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber;
import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.user.BatUser; import cc.fascinated.bat.model.user.BatUser;
import cc.fascinated.bat.service.ScoreSaberService;
import cc.fascinated.bat.service.UserService;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -15,9 +18,23 @@ import org.springframework.stereotype.Component;
*/ */
@Component @Component
public class UserSubCommand extends BatSubCommand { 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 @Override
public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction, OptionMapping option) { 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);
} }
} }

@ -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
}

@ -19,7 +19,7 @@ public class EmbedUtils {
return new EmbedBuilder() return new EmbedBuilder()
.setDescription(description) .setDescription(description)
.setTimestamp(LocalDateTime.now()) .setTimestamp(LocalDateTime.now())
.setColor(0x2F3136); .setColor(Colors.DEFAULT);
} }
/** /**
@ -32,7 +32,7 @@ public class EmbedUtils {
return new EmbedBuilder() return new EmbedBuilder()
.setDescription(description) .setDescription(description)
.setTimestamp(LocalDateTime.now()) .setTimestamp(LocalDateTime.now())
.setColor(0xFF0000); .setColor(Colors.ERROR);
} }
/** /**
@ -45,6 +45,6 @@ public class EmbedUtils {
return new EmbedBuilder() return new EmbedBuilder()
.setDescription(description) .setDescription(description)
.setTimestamp(LocalDateTime.now()) .setTimestamp(LocalDateTime.now())
.setColor(0x00FF00); .setColor(Colors.SUCCESS);
} }
} }

@ -60,7 +60,7 @@ public class BatUser {
* *
* @return the guild * @return the guild
*/ */
private User getDiscordUser() { public User getDiscordUser() {
return DiscordService.JDA.getUserById(id); return DiscordService.JDA.getUserById(id);
} }
} }