much stuff
This commit is contained in:
89
src/main/java/cc/fascinated/bat/command/BatCommand.java
Normal file
89
src/main/java/cc/fascinated/bat/command/BatCommand.java
Normal file
@ -0,0 +1,89 @@
|
||||
package cc.fascinated.bat.command;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
|
||||
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Getter @Setter
|
||||
public abstract class BatCommand implements BatCommandExecutor {
|
||||
/**
|
||||
* The name of the command
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* The description of the command
|
||||
*/
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* The category of the command
|
||||
*/
|
||||
private final Category category;
|
||||
|
||||
/**
|
||||
* The command data for the slash command
|
||||
*/
|
||||
private CommandDataImpl commandData;
|
||||
|
||||
/**
|
||||
* The sub commands of the command
|
||||
*/
|
||||
private Map<String, BatSubCommand> subCommands = new HashMap<>();
|
||||
|
||||
public BatCommand(@NonNull String name) {
|
||||
this.name = name;
|
||||
|
||||
// Default values
|
||||
this.description = "No description provided.";
|
||||
this.category = Category.GENERAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a sub command to the command
|
||||
*
|
||||
* @param name The name of the sub command
|
||||
* @param subCommand The sub command
|
||||
*/
|
||||
public BatCommand addSubCommand(@NonNull String name, @NonNull BatSubCommand subCommand) {
|
||||
this.subCommands.put(name.toLowerCase(), subCommand);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the options for the command
|
||||
*
|
||||
* @param interaction The slash command interaction
|
||||
* @return The option strings
|
||||
*/
|
||||
public List<String> getOptions(SlashCommandInteraction interaction) {
|
||||
return interaction.getOptions().stream().map(OptionMapping::getName).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* The category of the command
|
||||
*/
|
||||
@AllArgsConstructor @Getter
|
||||
private enum Category {
|
||||
GENERAL("General"),
|
||||
MODERATION("Moderation"),
|
||||
SERVER("Server");
|
||||
|
||||
/**
|
||||
* The name of the category
|
||||
*/
|
||||
private final String name;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package cc.fascinated.bat.command;
|
||||
|
||||
import cc.fascinated.bat.model.BatGuild;
|
||||
import cc.fascinated.bat.model.user.BatUser;
|
||||
import cc.fascinated.bat.service.GuildService;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
public interface BatCommandExecutor {
|
||||
|
||||
/**
|
||||
* Executes the command using a slash command interaction.
|
||||
*
|
||||
* @param guild the bat guild the command was executed in
|
||||
* @param user the bat user that executed the command
|
||||
* @param channel the channel the command was executed in
|
||||
* @param member the member that executed the command
|
||||
* @param interaction the slash command interaction
|
||||
* @param option the option that was used in the command, or null if no option was used
|
||||
*/
|
||||
default void execute(
|
||||
@NonNull BatGuild guild,
|
||||
@NonNull BatUser user,
|
||||
@NonNull TextChannel channel,
|
||||
@NonNull Member member,
|
||||
@NonNull SlashCommandInteraction interaction,
|
||||
OptionMapping option
|
||||
) {}
|
||||
}
|
18
src/main/java/cc/fascinated/bat/command/BatSubCommand.java
Normal file
18
src/main/java/cc/fascinated/bat/command/BatSubCommand.java
Normal file
@ -0,0 +1,18 @@
|
||||
package cc.fascinated.bat.command;
|
||||
|
||||
import cc.fascinated.bat.model.BatGuild;
|
||||
import cc.fascinated.bat.model.user.BatUser;
|
||||
import cc.fascinated.bat.service.GuildService;
|
||||
import cc.fascinated.bat.service.UserService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@AllArgsConstructor @Getter
|
||||
public class BatSubCommand implements BatCommandExecutor { }
|
@ -0,0 +1,64 @@
|
||||
package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber;
|
||||
|
||||
import cc.fascinated.bat.command.BatSubCommand;
|
||||
import cc.fascinated.bat.common.Profile;
|
||||
import cc.fascinated.bat.model.BatGuild;
|
||||
import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken;
|
||||
import cc.fascinated.bat.model.user.BatUser;
|
||||
import cc.fascinated.bat.model.user.profiles.ScoreSaberProfile;
|
||||
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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Component
|
||||
public class LinkSubCommand extends BatSubCommand {
|
||||
|
||||
private final ScoreSaberService scoreSaberService;
|
||||
private final UserService userService;
|
||||
|
||||
@Autowired
|
||||
public LinkSubCommand(@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) {
|
||||
if (option == null) {
|
||||
interaction.reply("You must provide a ScoreSaber profile link to link your profile").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
String link = option.getAsString();
|
||||
if (!link.contains("scoresaber.com/u/")) {
|
||||
interaction.reply("Invalid ScoreSaber profile link").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
String id = link.split("scoresaber.com/u/")[1];
|
||||
if (id.contains("/")) {
|
||||
id = id.split("/")[0];
|
||||
}
|
||||
|
||||
ScoreSaberAccountToken account = scoreSaberService.getAccount(id);
|
||||
if (account == null) {
|
||||
interaction.reply("Invalid ScoreSaber profile link").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
((ScoreSaberProfile) user.getProfile(ScoreSaberProfile.class)).setId(id);
|
||||
userService.saveUser(user);
|
||||
interaction.reply("Successfully linked your ScoreSaber profile").queue();
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package cc.fascinated.bat.command.impl.global.beatsaber.scoresaber;
|
||||
|
||||
import cc.fascinated.bat.command.BatCommand;
|
||||
import cc.fascinated.bat.model.BatGuild;
|
||||
import cc.fascinated.bat.model.beatsaber.scoresaber.ScoreSaberAccountToken;
|
||||
import cc.fascinated.bat.model.user.BatUser;
|
||||
import cc.fascinated.bat.model.user.profiles.ScoreSaberProfile;
|
||||
import cc.fascinated.bat.service.GuildService;
|
||||
import cc.fascinated.bat.service.ScoreSaberService;
|
||||
import cc.fascinated.bat.service.UserService;
|
||||
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;
|
||||
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
||||
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Component
|
||||
public class ScoreSaberCommand extends BatCommand {
|
||||
private final ScoreSaberService scoreSaberService;
|
||||
|
||||
@Autowired
|
||||
public ScoreSaberCommand(@NonNull ScoreSaberService scoreSaberService) {
|
||||
super("scoresaber");
|
||||
this.scoreSaberService = scoreSaberService;
|
||||
|
||||
super.setDescription("View a user's ScoreSaber profile");
|
||||
super.setCommandData(new CommandDataImpl(this.getName(), this.getDescription())
|
||||
.addOptions(new OptionData(OptionType.STRING, "link", "Link your ScoreSaber profile", false))
|
||||
.addOptions(new OptionData(OptionType.USER, "user", "The user to view the ScoreSaber profile of", false))
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction, OptionMapping option) {
|
||||
ScoreSaberProfile profile = user.getProfile(ScoreSaberProfile.class);
|
||||
if (profile.getId() == null) {
|
||||
interaction.reply("You must link your ScoreSaber profile first").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();
|
||||
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()
|
||||
.addField("Name", account.getName(), true)
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
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.GuildService;
|
||||
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.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Component
|
||||
public class UserSubCommand extends BatSubCommand {
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user