From 4a7e7de6a2f37b0642482d6dab2226fa0480ddfa Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 27 Jun 2024 13:13:37 +0100 Subject: [PATCH] add rate limit check for scoresaber api --- .../command/scoresaber/ScoreSaberCommand.java | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ScoreSaberCommand.java b/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ScoreSaberCommand.java index 9bb4566..5641379 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ScoreSaberCommand.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ScoreSaberCommand.java @@ -5,6 +5,7 @@ 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.exception.RateLimitException; import cc.fascinated.bat.features.scoresaber.profile.UserScoreSaberProfile; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; @@ -65,32 +66,37 @@ public class ScoreSaberCommand extends BatCommand { return; } - // todo: handle rate limits - ScoreSaberAccountToken account = scoreSaberService.getAccount(profile.getSteamId()); - if (account == null) { - if (!isSelf) { + try { + ScoreSaberAccountToken account = scoreSaberService.getAccount(profile.getSteamId()); + if (account == null) { + if (!isSelf) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("%s has an invalid ScoreSaber account linked, please ask them to re-link their account" + .formatted(user.getDiscordUser().getAsMention())) + .build()).queue(); + } interaction.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("%s has an invalid ScoreSaber account linked, please ask them to re-link their account" - .formatted(user.getDiscordUser().getAsMention())) + .setDescription("You have an invalid ScoreSaber account linked, please re-link your account") .build()).queue(); + return; } - interaction.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("You have an invalid ScoreSaber account linked, please re-link your account") - .build()).queue(); - return; - } - 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", "#" + 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()) - .setColor(Colors.DEFAULT) - .build()).queue(); + 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", "#" + 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()) + .setColor(Colors.DEFAULT) + .build()).queue(); + } catch (RateLimitException ex) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("The ScoreSaber API is currently rate our limiting requests, please try again later") + .build()).queue(); + } } }