clean up scoresaber feature

This commit is contained in:
Lee 2024-06-25 11:14:12 +01:00
parent aa6ab7d3d8
commit 39c3d471c7
15 changed files with 93 additions and 54 deletions

@ -1,5 +1,6 @@
package cc.fascinated.bat.common; package cc.fascinated.bat.common;
import lombok.experimental.UtilityClass;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -7,6 +8,7 @@ import java.time.LocalDateTime;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@UtilityClass
public class EmbedUtils { public class EmbedUtils {
/** /**

@ -1,10 +1,13 @@
package cc.fascinated.bat.common; package cc.fascinated.bat.common;
import lombok.experimental.UtilityClass;
import java.text.NumberFormat; import java.text.NumberFormat;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@UtilityClass
public class NumberUtils { public class NumberUtils {
/** /**
* Formats a number with commas. * Formats a number with commas.

@ -1,8 +1,11 @@
package cc.fascinated.bat.common; package cc.fascinated.bat.common;
import lombok.experimental.UtilityClass;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@UtilityClass
public class ScoreSaberUtils { public class ScoreSaberUtils {
/** /**
* Gets the formatted difficulty of a song. * Gets the formatted difficulty of a song.

@ -1,10 +1,12 @@
package cc.fascinated.bat.common; package cc.fascinated.bat.common;
import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.DiscordService;
import lombok.experimental.UtilityClass;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@UtilityClass
public class TextChannelUtils { public class TextChannelUtils {
/** /**
* Checks if a channel is valid * Checks if a channel is valid

@ -12,21 +12,23 @@ import cc.fascinated.bat.model.guild.BatGuild;
import cc.fascinated.bat.model.guild.profiles.ScoreSaberUserScoreFeedProfile; import cc.fascinated.bat.model.guild.profiles.ScoreSaberUserScoreFeedProfile;
import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.DiscordService;
import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.GuildService;
import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component @Log4j2
public class ScoreSaberScoreFeedListener implements EventListener { public class UserScoreFeedListener implements EventListener {
private final GuildService guildService; private final GuildService guildService;
@Autowired @Autowired
public ScoreSaberScoreFeedListener(GuildService guildService) { public UserScoreFeedListener(GuildService guildService) {
this.guildService = guildService; this.guildService = guildService;
} }
@ -38,19 +40,19 @@ public class ScoreSaberScoreFeedListener implements EventListener {
if (batGuild == null) { if (batGuild == null) {
continue; continue;
} }
ScoreSaberUserScoreFeedProfile profile = batGuild.getProfile(ScoreSaberUserScoreFeedProfile.class); ScoreSaberUserScoreFeedProfile profile = batGuild.getProfile(ScoreSaberUserScoreFeedProfile.class);
if (profile == null) { if (profile == null || profile.getChannelId() == null || !profile.getTrackedUsers().contains(player.getId())) {
continue;
}
if (profile.getChannelId() == null) {
continue;
}
if (!profile.getTrackedUsers().contains(player.getId())) {
continue; continue;
} }
profile.getAsTextChannel().sendMessageEmbeds(this.buildScoreEmbed(score)).queue(); TextChannel channel = profile.getAsTextChannel();
if (channel == null) {
log.error("Scoresaber user feed channel is null for guild {}, removing the stored channel.", guild.getId());
profile.setChannelId(null);
guildService.saveGuild(batGuild);
continue;
}
channel.sendMessageEmbeds(this.buildScoreEmbed(score)).queue();
} }
} }
@ -78,7 +80,7 @@ public class ScoreSaberScoreFeedListener implements EventListener {
leaderboardToken.getMaxScore() == 0 ? "N/A" : NumberUtils.formatNumberCommas(((double) scoreToken.getBaseScore() / leaderboardToken.getMaxScore()) * 100) leaderboardToken.getMaxScore() == 0 ? "N/A" : NumberUtils.formatNumberCommas(((double) scoreToken.getBaseScore() / leaderboardToken.getMaxScore()) * 100)
), true) ), true)
.addField("Raw PP", scoreToken.getPp() == 0 ? "Unranked" : NumberUtils.formatNumberCommas(scoreToken.getPp()), true) .addField("Raw PP", scoreToken.getPp() == 0 ? "Unranked" : NumberUtils.formatNumberCommas(scoreToken.getPp()), true)
.addField("Global Rank", "#%s".formatted(NumberUtils.formatNumberCommas(scoreToken.getRank())), true) .addField("Rank", "#%s".formatted(NumberUtils.formatNumberCommas(scoreToken.getRank())), true)
.addField("Misses", "%s".formatted(scoreToken.getMissedNotes()), true) .addField("Misses", "%s".formatted(scoreToken.getMissedNotes()), true)
.addField("Bad Cuts", "%s".formatted(scoreToken.getBadCuts()), true) .addField("Bad Cuts", "%s".formatted(scoreToken.getBadCuts()), true)
.addField("Max Combo", "%s %s".formatted( .addField("Max Combo", "%s %s".formatted(

@ -19,7 +19,7 @@ import org.springframework.stereotype.Component;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component("scoresaber:link.sub")
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;

@ -0,0 +1,30 @@
package cc.fascinated.bat.features.scoresaber.command.scoresaber;
import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.model.guild.BatGuild;
import cc.fascinated.bat.model.user.BatUser;
import cc.fascinated.bat.service.ScoreSaberService;
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.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author Fascinated (fascinated7)
*/
@Component("scoresaber:me.sub")
public class MeSubCommand extends BatSubCommand {
private final ScoreSaberService scoreSaberService;
@Autowired
public MeSubCommand(@NonNull ScoreSaberService scoreSaberService) {
this.scoreSaberService = scoreSaberService;
}
@Override
public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction) {
ScoreSaberCommand.sendProfileEmbed(true, user, scoreSaberService, interaction);
}
}

@ -45,6 +45,7 @@ public class ScoreSaberCommand extends BatCommand {
.addSubcommands(new SubcommandData("user", "View a user's ScoreSaber profile") .addSubcommands(new SubcommandData("user", "View a user's ScoreSaber profile")
.addOptions(new OptionData(OptionType.USER, "user", "The user to view the ScoreSaber profile of", true)) .addOptions(new OptionData(OptionType.USER, "user", "The user to view the ScoreSaber profile of", true))
) )
.addSubcommands(new SubcommandData("me", "View your ScoreSaber profile"))
); );
} }

@ -17,7 +17,7 @@ import org.springframework.stereotype.Component;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component("scoresaber:user.sub")
public class UserSubCommand extends BatSubCommand { public class UserSubCommand extends BatSubCommand {
private final ScoreSaberService scoreSaberService; private final ScoreSaberService scoreSaberService;
private final UserService userService; private final UserService userService;

@ -20,12 +20,12 @@ import org.springframework.stereotype.Component;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component("scoresaber-userfeed:channel.sub")
public class ScoreFeedChannelCommand extends BatSubCommand { public class ChannelSubCommand extends BatSubCommand {
private final GuildService guildService; private final GuildService guildService;
@Autowired @Autowired
public ScoreFeedChannelCommand(GuildService guildService) { public ChannelSubCommand(GuildService guildService) {
this.guildService = guildService; this.guildService = guildService;
} }

@ -17,12 +17,12 @@ import org.springframework.stereotype.Component;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component("scoresaber-userfeed:clearusers.sub")
public class ScoreFeedClearUsersCommand extends BatSubCommand { public class ClearUsersSubCommand extends BatSubCommand {
private final GuildService guildService; private final GuildService guildService;
@Autowired @Autowired
public ScoreFeedClearUsersCommand(GuildService guildService, UserService userService) { public ClearUsersSubCommand(GuildService guildService, UserService userService) {
this.guildService = guildService; this.guildService = guildService;
} }

@ -12,9 +12,9 @@ import org.springframework.stereotype.Component;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component("scoresaber-userfeed:feed.sub")
public class ScoreSaberUserFeedCommand extends BatCommand { public class UserFeedCommand extends BatCommand {
public ScoreSaberUserFeedCommand() { public UserFeedCommand() {
super("scoresaber-userfeed"); super("scoresaber-userfeed");
super.setCategory(Category.BEAT_SABER); super.setCategory(Category.BEAT_SABER);

@ -20,13 +20,13 @@ import org.springframework.stereotype.Component;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component("scoresaber-userfeed:user.sub")
public class ScoreFeedUserCommand extends BatSubCommand { public class UserSubCommand extends BatSubCommand {
private final GuildService guildService; private final GuildService guildService;
private final UserService userService; private final UserService userService;
@Autowired @Autowired
public ScoreFeedUserCommand(GuildService guildService, UserService userService) { public UserSubCommand(GuildService guildService, UserService userService) {
this.guildService = guildService; this.guildService = guildService;
this.userService = userService; this.userService = userService;
} }

@ -4,12 +4,12 @@ import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.features.scoresaber.command.scoresaber.LinkSubCommand; import cc.fascinated.bat.features.scoresaber.command.scoresaber.LinkSubCommand;
import cc.fascinated.bat.features.scoresaber.command.scoresaber.MeSubCommand;
import cc.fascinated.bat.features.scoresaber.command.scoresaber.ScoreSaberCommand; import cc.fascinated.bat.features.scoresaber.command.scoresaber.ScoreSaberCommand;
import cc.fascinated.bat.features.scoresaber.command.scoresaber.UserSubCommand; import cc.fascinated.bat.features.scoresaber.command.userfeed.ChannelSubCommand;
import cc.fascinated.bat.features.scoresaber.command.userfeed.ScoreFeedChannelCommand; import cc.fascinated.bat.features.scoresaber.command.userfeed.ClearUsersSubCommand;
import cc.fascinated.bat.features.scoresaber.command.userfeed.ScoreFeedClearUsersCommand; import cc.fascinated.bat.features.scoresaber.command.userfeed.UserFeedCommand;
import cc.fascinated.bat.features.scoresaber.command.userfeed.ScoreFeedUserCommand; import cc.fascinated.bat.features.scoresaber.command.userfeed.UserSubCommand;
import cc.fascinated.bat.features.scoresaber.command.userfeed.ScoreSaberUserFeedCommand;
import cc.fascinated.bat.model.guild.BatGuild; import cc.fascinated.bat.model.guild.BatGuild;
import cc.fascinated.bat.model.user.BatUser; import cc.fascinated.bat.model.user.BatUser;
import lombok.NonNull; import lombok.NonNull;
@ -60,12 +60,13 @@ public class CommandService extends ListenerAdapter {
// Global commands // Global commands
registerCommand(context.getBean(ScoreSaberCommand.class) registerCommand(context.getBean(ScoreSaberCommand.class)
.addSubCommand("link", context.getBean(LinkSubCommand.class)) .addSubCommand("link", context.getBean(LinkSubCommand.class))
.addSubCommand("user", context.getBean(UserSubCommand.class)) .addSubCommand("user", context.getBean(cc.fascinated.bat.features.scoresaber.command.scoresaber.UserSubCommand.class))
.addSubCommand("me", context.getBean(MeSubCommand.class))
); );
registerCommand(context.getBean(ScoreSaberUserFeedCommand.class) registerCommand(context.getBean(UserFeedCommand.class)
.addSubCommand("user", context.getBean(ScoreFeedUserCommand.class)) .addSubCommand("user", context.getBean(UserSubCommand.class))
.addSubCommand("channel", context.getBean(ScoreFeedChannelCommand.class)) .addSubCommand("channel", context.getBean(ChannelSubCommand.class))
.addSubCommand("clear-users", context.getBean(ScoreFeedClearUsersCommand.class)) .addSubCommand("clear-users", context.getBean(ClearUsersSubCommand.class))
); );
registerSlashCommands(); // Register all slash commands registerSlashCommands(); // Register all slash commands
@ -106,13 +107,7 @@ public class CommandService extends ListenerAdapter {
@Override @Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
Guild discordGuild = event.getGuild(); Guild discordGuild = event.getGuild();
if (discordGuild == null) { if (discordGuild == null || event.getUser().isBot() || event.getMember() == null) {
return;
}
if (event.getUser().isBot()) {
return;
}
if (event.getMember() == null) {
return; return;
} }
@ -124,19 +119,20 @@ public class CommandService extends ListenerAdapter {
BatGuild guild = guildService.getGuild(discordGuild.getId()); BatGuild guild = guildService.getGuild(discordGuild.getId());
BatUser user = userService.getUser(event.getUser().getId()); BatUser user = userService.getUser(event.getUser().getId());
// No args provided, use the main command executor
try { try {
// No args provided, use the main command executor
if (event.getInteraction().getSubcommandName() == null) { if (event.getInteraction().getSubcommandName() == null) {
command.execute(guild, user, event.getChannel().asTextChannel(), event.getMember(), event.getInteraction()); command.execute(guild, user, event.getChannel().asTextChannel(), event.getMember(), event.getInteraction());
} else { return;
}
// Subcommand provided, use the subcommand executor
for (Map.Entry<String, BatSubCommand> subCommand : command.getSubCommands().entrySet()) { for (Map.Entry<String, BatSubCommand> subCommand : command.getSubCommands().entrySet()) {
if (subCommand.getKey().equalsIgnoreCase(event.getInteraction().getSubcommandName())) { if (subCommand.getKey().equalsIgnoreCase(event.getInteraction().getSubcommandName())) {
subCommand.getValue().execute(guild, user, event.getChannel().asTextChannel(), event.getMember(), event.getInteraction()); subCommand.getValue().execute(guild, user, event.getChannel().asTextChannel(), event.getMember(), event.getInteraction());
break; break;
} }
} }
}
} catch (Exception ex) { } catch (Exception ex) {
log.error("An error occurred while executing command \"{}\"", commandName, ex); log.error("An error occurred while executing command \"{}\"", commandName, ex);
event.replyEmbeds(EmbedUtils.buildErrorEmbed("An error occurred while executing the command\n\n" + event.replyEmbeds(EmbedUtils.buildErrorEmbed("An error occurred while executing the command\n\n" +

@ -1,7 +1,7 @@
package cc.fascinated.bat.service; package cc.fascinated.bat.service;
import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.features.scoresaber.ScoreSaberScoreFeedListener; import cc.fascinated.bat.features.scoresaber.UserScoreFeedListener;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -24,7 +24,7 @@ public class EventService {
@Autowired @Autowired
public EventService(@NonNull ApplicationContext context) { public EventService(@NonNull ApplicationContext context) {
registerListeners( registerListeners(
context.getBean(ScoreSaberScoreFeedListener.class) context.getBean(UserScoreFeedListener.class)
); );
log.info("Registered {} listeners.", LISTENERS.size()); log.info("Registered {} listeners.", LISTENERS.size());
} }