cleanup commands and fix cmds in dms(???????)

This commit is contained in:
Lee 2024-06-26 00:31:16 +01:00
parent 175dfde8f7
commit ac760f84be
23 changed files with 161 additions and 117 deletions

@ -4,7 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter; import lombok.Setter;
import net.dv8tion.jda.api.Permission;
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.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.dv8tion.jda.internal.interactions.CommandDataImpl; import net.dv8tion.jda.internal.interactions.CommandDataImpl;
@ -25,40 +27,69 @@ public abstract class BatCommand implements BatCommandExecutor {
/** /**
* The description of the command * The description of the command
*/ */
private String description; private final String description;
/**
* The command data for the slash command
*/
private final CommandDataImpl commandData;
/**
* The required permissions for the command
*/
private final List<Permission> requiredPermissions;
/**
* The sub commands of the command
*/
private final Map<String, BatSubCommand> subCommands = new HashMap<>();
/** /**
* The category of the command * The category of the command
*/ */
private Category category; private Category category;
/** public BatCommand(@NonNull String name, @NonNull String description, boolean guildOnly, Permission... permissions) {
* 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; this.name = name;
this.description = description;
this.requiredPermissions = List.of(permissions);
// Default values // Default values
this.description = "No description provided.";
this.category = Category.GENERAL; this.category = Category.GENERAL;
this.commandData = new CommandDataImpl(this.name, description)
.setGuildOnly(guildOnly);
}
public BatCommand(@NonNull String name) {
this(name, "No description provided.", false);
}
public BatCommand(@NonNull String name, @NonNull String description) {
this(name, description, false);
} }
/** /**
* Adds a sub command to the command * Adds a sub command to the command
* *
* @param name The name of the sub command * @param name The name of the sub command
* @param subCommand The sub command * @param subCommand The sub command
*/ */
public BatCommand addSubCommand(@NonNull String name, @NonNull BatSubCommand subCommand) { public void addSubCommand(@NonNull String name, @NonNull BatSubCommand subCommand) {
this.subCommands.put(name.toLowerCase(), subCommand); this.subCommands.put(name.toLowerCase(), subCommand);
return this; this.commandData.addSubcommands(subCommand.getCommandData());
}
/**
* Adds an option to the sub command
*
* @param optionType the type of the option
* @param name the name of the option
* @param description the description of the option
* @param required whether the option is required
*/
private void addOption(OptionType optionType, String name, String description, boolean required) {
this.commandData.addOption(optionType, name, description, required);
} }
/** /**

@ -11,18 +11,17 @@ import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
public interface BatCommandExecutor { public interface BatCommandExecutor {
/** /**
* Executes the command using a slash command interaction. * Executes the command using a slash command interaction.
* *
* @param guild the bat guild the command was executed in * @param guild the bat guild the command was executed in (null if the command was executed in a DM)
* @param user the bat user that executed the command * @param user the bat user that executed the command
* @param channel the channel the command was executed in * @param channel the channel the command was executed in
* @param member the member that executed the command * @param member the member that executed the command
* @param interaction the slash command interaction * @param interaction the slash command interaction
*/ */
default void execute( default void execute(
@NonNull BatGuild guild, BatGuild guild,
@NonNull BatUser user, @NonNull BatUser user,
@NonNull TextChannel channel, @NonNull TextChannel channel,
@NonNull Member member, @NonNull Member member,

@ -1,10 +1,41 @@
package cc.fascinated.bat.command; package cc.fascinated.bat.command;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import java.util.List;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@AllArgsConstructor @Getter @Getter
public class BatSubCommand implements BatCommandExecutor { } public class BatSubCommand implements BatCommandExecutor {
/**
* The command data for the slash command
*/
private final SubcommandData commandData;
/**
* The required permissions for the command
*/
private final List<Permission> requiredPermissions;
public BatSubCommand(String name, String description, Permission... permissions) {
this.commandData = new SubcommandData(name, description);
this.requiredPermissions = List.of(permissions);
}
/**
* Adds an option to the sub command
*
* @param optionType the type of the option
* @param name the name of the option
* @param description the description of the option
* @param required whether the option is required
*/
public void addOption(OptionType optionType, String name, String description, boolean required) {
this.commandData.addOption(optionType, name, description, required);
}
}

@ -8,7 +8,6 @@ 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.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -18,9 +17,7 @@ import org.springframework.stereotype.Component;
public class PingCommand extends BatCommand { public class PingCommand extends BatCommand {
public PingCommand() { public PingCommand() {
super("ping"); super("ping", "Gets the ping of the bot");
super.setDescription("Gets the ping of the bot");
super.setCommandData(new CommandDataImpl(this.getName(), this.getDescription()));
} }
@Override @Override

@ -12,6 +12,7 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.Role;
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.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -24,7 +25,9 @@ public class AddSubCommand extends BatSubCommand {
private final GuildService guildService; private final GuildService guildService;
@Autowired @Autowired
public AddSubCommand(GuildService guildService) { public AddSubCommand(@NonNull GuildService guildService) {
super("add", "Adds a role to the auto roles list");
super.addOption(OptionType.ROLE, "role", "The role to add", true);
this.guildService = guildService; this.guildService = guildService;
} }

@ -3,11 +3,6 @@ package cc.fascinated.bat.features.autorole.command;
import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.BatCommand;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -17,26 +12,11 @@ import org.springframework.stereotype.Component;
@Component("autoroles.command") @Component("autoroles.command")
public class AutoRoleCommand extends BatCommand { public class AutoRoleCommand extends BatCommand {
public AutoRoleCommand(@NonNull ApplicationContext context) { public AutoRoleCommand(@NonNull ApplicationContext context) {
super("autorole"); super("autorole", "Set up the automatic role system for members on join", true, Permission.MANAGE_SERVER);
super.setDescription("Set up the automatic role system for members on join");
super.addSubCommand("list", context.getBean(ListSubCommand.class)); super.addSubCommand("list", context.getBean(ListSubCommand.class));
super.addSubCommand("add", context.getBean(AddSubCommand.class)); super.addSubCommand("add", context.getBean(AddSubCommand.class));
super.addSubCommand("remove", context.getBean(RemoveSubCommand.class)); super.addSubCommand("remove", context.getBean(RemoveSubCommand.class));
super.addSubCommand("clear", context.getBean(ClearSubCommand.class)); super.addSubCommand("clear", context.getBean(ClearSubCommand.class));
super.setCommandData(new CommandDataImpl(this.getName(), this.getDescription())
.setGuildOnly(true)
.addSubcommands(new SubcommandData("list", "Show the current automatic roles"))
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
.addSubcommands(new SubcommandData("add", "Add an automatic role")
.addOptions(new OptionData(OptionType.ROLE, "role", "The role to add", true))
).setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
.addSubcommands(new SubcommandData("remove", "Remove an automatic role")
.addOptions(new OptionData(OptionType.ROLE, "role", "The role to remove", true))
).setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
.addSubcommands(new SubcommandData("clear", "Clears all automatic roles"))
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
);
} }
} }

@ -22,6 +22,7 @@ public class ClearSubCommand extends BatSubCommand {
@Autowired @Autowired
public ClearSubCommand(GuildService guildService) { public ClearSubCommand(GuildService guildService) {
super("clear", "Clears all auto roles");
this.guildService = guildService; this.guildService = guildService;
} }

@ -18,6 +18,10 @@ import org.springframework.stereotype.Component;
@Component("autoroles:list.sub") @Component("autoroles:list.sub")
public class ListSubCommand extends BatSubCommand { public class ListSubCommand extends BatSubCommand {
public ListSubCommand() {
super("list", "Lists all auto roles");
}
@Override @Override
public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction) { public void execute(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull TextChannel channel, @NonNull Member member, @NonNull SlashCommandInteraction interaction) {
AutoRoleProfile profile = guild.getProfile(AutoRoleProfile.class); AutoRoleProfile profile = guild.getProfile(AutoRoleProfile.class);

@ -11,6 +11,7 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.Role;
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.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -24,6 +25,8 @@ public class RemoveSubCommand extends BatSubCommand {
@Autowired @Autowired
public RemoveSubCommand(GuildService guildService) { public RemoveSubCommand(GuildService guildService) {
super("remove", "Removes a role from the auto roles list");
super.addOption(OptionType.ROLE, "role", "The role to remove", true);
this.guildService = guildService; this.guildService = guildService;
} }

@ -1,9 +1,6 @@
package cc.fascinated.bat.features.scoresaber; package cc.fascinated.bat.features.scoresaber;
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.common.ScoreSaberUtils;
import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.features.scoresaber.profile.GuildNumberOneScoreFeedProfile; import cc.fascinated.bat.features.scoresaber.profile.GuildNumberOneScoreFeedProfile;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
@ -14,7 +11,6 @@ import cc.fascinated.bat.service.DiscordService;
import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.GuildService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
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.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

@ -1,9 +1,5 @@
package cc.fascinated.bat.features.scoresaber; package cc.fascinated.bat.features.scoresaber;
import cc.fascinated.bat.common.DateUtils;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.NumberUtils;
import cc.fascinated.bat.common.ScoreSaberUtils;
import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.features.scoresaber.profile.GuildUserScoreFeedProfile; import cc.fascinated.bat.features.scoresaber.profile.GuildUserScoreFeedProfile;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
@ -14,7 +10,6 @@ import cc.fascinated.bat.service.DiscordService;
import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.GuildService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
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.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

@ -13,6 +13,7 @@ import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion; import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
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.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -26,6 +27,8 @@ public class ChannelSubCommand extends BatSubCommand {
@Autowired @Autowired
public ChannelSubCommand(GuildService guildService) { public ChannelSubCommand(GuildService guildService) {
super("channel", "Sets the feed channel");
super.addOption(OptionType.CHANNEL, "channel", "The channel scores are sent in", false);
this.guildService = guildService; this.guildService = guildService;
} }

@ -3,11 +3,6 @@ package cc.fascinated.bat.features.scoresaber.command.numberone;
import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.BatCommand;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -17,20 +12,10 @@ import org.springframework.stereotype.Component;
@Component("scoresaber-number-one-feed") @Component("scoresaber-number-one-feed")
public class NumberOneFeedCommand extends BatCommand { public class NumberOneFeedCommand extends BatCommand {
public NumberOneFeedCommand(@NonNull ApplicationContext context) { public NumberOneFeedCommand(@NonNull ApplicationContext context) {
super("scoresaber-number-one-feed"); super("scoresaber-number-one-feed", "Modifies the settings for the feed.", true, Permission.MANAGE_SERVER);
super.setCategory(Category.BEAT_SABER); super.setCategory(Category.BEAT_SABER);
super.addSubCommand("channel", context.getBean(ChannelSubCommand.class)); super.addSubCommand("channel", context.getBean(ChannelSubCommand.class));
super.addSubCommand("reset", context.getBean(ResetSubCommand.class)); super.addSubCommand("reset", context.getBean(ResetSubCommand.class));
super.setDescription("Modifies the settings for the feed.");
super.setCommandData(new CommandDataImpl(this.getName(), this.getDescription())
.setGuildOnly(true)
.addSubcommands(new SubcommandData("channel", "Change the channel the score feed is sent in")
.addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel scores are sent in", false))
).setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
.addSubcommands(new SubcommandData("reset", "Resets the feed settings to default"))
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
);
} }
} }

@ -22,6 +22,7 @@ public class ResetSubCommand extends BatSubCommand {
@Autowired @Autowired
public ResetSubCommand(GuildService guildService) { public ResetSubCommand(GuildService guildService) {
super("reset", "Resets the settings");
this.guildService = guildService; this.guildService = guildService;
} }

@ -12,6 +12,7 @@ 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.OptionType;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -26,6 +27,8 @@ public class LinkSubCommand extends BatSubCommand {
@Autowired @Autowired
public LinkSubCommand(@NonNull ScoreSaberService scoreSaberService, @NonNull UserService userService) { public LinkSubCommand(@NonNull ScoreSaberService scoreSaberService, @NonNull UserService userService) {
super("link", "Links your ScoreSaber profile");
super.addOption(OptionType.STRING, "link", "Link your ScoreSaber profile", true);
this.scoreSaberService = scoreSaberService; this.scoreSaberService = scoreSaberService;
this.userService = userService; this.userService = userService;
} }

@ -20,6 +20,7 @@ public class MeSubCommand extends BatSubCommand {
@Autowired @Autowired
public MeSubCommand(@NonNull ScoreSaberService scoreSaberService) { public MeSubCommand(@NonNull ScoreSaberService scoreSaberService) {
super("me", "Gets your ScoreSaber profile");
this.scoreSaberService = scoreSaberService; this.scoreSaberService = scoreSaberService;
} }

@ -14,11 +14,7 @@ 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.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -34,24 +30,13 @@ public class ScoreSaberCommand extends BatCommand {
@Autowired @Autowired
public ScoreSaberCommand(@NonNull ScoreSaberService scoreSaberService, @NonNull ApplicationContext context) { public ScoreSaberCommand(@NonNull ScoreSaberService scoreSaberService, @NonNull ApplicationContext context) {
super("scoresaber"); super("scoresaber", "General ScoreSaber commands");
super.setCategory(Category.BEAT_SABER); super.setCategory(Category.BEAT_SABER);
this.scoreSaberService = scoreSaberService; this.scoreSaberService = scoreSaberService;
super.addSubCommand("link", context.getBean(LinkSubCommand.class)); super.addSubCommand("link", context.getBean(LinkSubCommand.class));
super.addSubCommand("user", context.getBean(UserSubCommand.class)); super.addSubCommand("user", context.getBean(UserSubCommand.class));
super.addSubCommand("me", context.getBean(MeSubCommand.class)); super.addSubCommand("me", context.getBean(MeSubCommand.class));
super.setDescription("General ScoreSaber commands");
super.setCommandData(new CommandDataImpl(this.getName(), this.getDescription())
.addSubcommands(new SubcommandData("link", "Link your ScoreSaber profile")
.addOptions(new OptionData(OptionType.STRING, "link", "Link your ScoreSaber profile", true))
)
.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))
)
.addSubcommands(new SubcommandData("me", "View your ScoreSaber profile"))
);
} }
@Override @Override

@ -10,6 +10,7 @@ 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.OptionType;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -24,6 +25,8 @@ public class UserSubCommand extends BatSubCommand {
@Autowired @Autowired
public UserSubCommand(@NonNull ScoreSaberService scoreSaberService, @NonNull UserService userService) { public UserSubCommand(@NonNull ScoreSaberService scoreSaberService, @NonNull UserService userService) {
super("user", "Gets a ScoreSaber profile");
super.addOption(OptionType.USER, "user", "The user to view the ScoreSaber profile of", true);
this.scoreSaberService = scoreSaberService; this.scoreSaberService = scoreSaberService;
this.userService = userService; this.userService = userService;
} }

@ -13,6 +13,7 @@ import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion; import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
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.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -26,6 +27,8 @@ public class ChannelSubCommand extends BatSubCommand {
@Autowired @Autowired
public ChannelSubCommand(GuildService guildService) { public ChannelSubCommand(GuildService guildService) {
super("channel", "Sets the feed channel");
super.addOption(OptionType.CHANNEL, "channel", "The channel scores are sent in", false);
this.guildService = guildService; this.guildService = guildService;
} }

@ -22,6 +22,7 @@ public class ResetSubCommand extends BatSubCommand {
@Autowired @Autowired
public ResetSubCommand(GuildService guildService) { public ResetSubCommand(GuildService guildService) {
super("reset", "Resets the settings");
this.guildService = guildService; this.guildService = guildService;
} }

@ -3,11 +3,6 @@ package cc.fascinated.bat.features.scoresaber.command.userfeed;
import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.BatCommand;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -17,26 +12,11 @@ import org.springframework.stereotype.Component;
@Component("scoresaber-user-feed.command") @Component("scoresaber-user-feed.command")
public class UserFeedCommand extends BatCommand { public class UserFeedCommand extends BatCommand {
public UserFeedCommand(@NonNull ApplicationContext context) { public UserFeedCommand(@NonNull ApplicationContext context) {
super("scoresaber-user-feed"); super("scoresaber-user-feed", "Modifies the settings for the feed.", true, Permission.MANAGE_SERVER);
super.setCategory(Category.BEAT_SABER); super.setCategory(Category.BEAT_SABER);
super.addSubCommand("user", context.getBean(UserSubCommand.class)); super.addSubCommand("user", context.getBean(UserSubCommand.class));
super.addSubCommand("channel", context.getBean(ChannelSubCommand.class)); super.addSubCommand("channel", context.getBean(ChannelSubCommand.class));
super.addSubCommand("reset", context.getBean(ResetSubCommand.class)); super.addSubCommand("reset", context.getBean(ResetSubCommand.class));
super.setDescription("Modifies the settings for the feed.");
super.setCommandData(new CommandDataImpl(this.getName(), this.getDescription())
.setGuildOnly(true)
.addSubcommands(new SubcommandData("user", "Edit the users in the score feed")
.addOptions(new OptionData(OptionType.USER, "user", "Add or remove a user from the score feed", false))
).setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
.addSubcommands(new SubcommandData("channel", "Change the channel the score feed is sent in")
.addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel scores are sent in", false))
).setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
.addSubcommands(new SubcommandData("reset", "Resets the feed settings to default"))
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER))
);
} }
} }

@ -13,6 +13,7 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
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.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -27,6 +28,8 @@ public class UserSubCommand extends BatSubCommand {
@Autowired @Autowired
public UserSubCommand(GuildService guildService, UserService userService) { public UserSubCommand(GuildService guildService, UserService userService) {
super("user", "Adds or removes a user from the feed");
super.addOption(OptionType.USER, "user", "Add or remove a user from the score feed", false);
this.guildService = guildService; this.guildService = guildService;
this.userService = userService; this.userService = userService;
} }

@ -1,6 +1,7 @@
package cc.fascinated.bat.service; package cc.fascinated.bat.service;
import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.BatCommandExecutor;
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.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
@ -8,6 +9,7 @@ import cc.fascinated.bat.model.BatUser;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
@ -16,7 +18,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -96,22 +100,54 @@ public class CommandService extends ListenerAdapter {
return; return;
} }
BatGuild guild = guildService.getGuild(discordGuild.getId()); boolean ranInGuild = event.isFromGuild();
BatGuild guild = ranInGuild ? guildService.getGuild(discordGuild.getId()) : null;
BatUser user = userService.getUser(event.getUser().getId()); BatUser user = userService.getUser(event.getUser().getId());
try { try {
BatCommandExecutor executor = null;
List<Permission> requiredPermissions = new ArrayList<>();
// No args provided, use the main command executor // 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()); executor = command;
requiredPermissions.addAll(command.getRequiredPermissions());
} else {
// Subcommand provided, use the subcommand executor
for (Map.Entry<String, BatSubCommand> subCommand : command.getSubCommands().entrySet()) {
if (subCommand.getKey().equalsIgnoreCase(event.getInteraction().getSubcommandName())) {
executor = subCommand.getValue();
requiredPermissions.addAll(subCommand.getValue().getRequiredPermissions());
requiredPermissions.addAll(command.getRequiredPermissions()); // not sure if we'd want this, but it's here for now
break;
}
}
}
if (executor == null) {
event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("Unable to find a command executor the command name ):")
.build()).queue();
return; return;
} }
// Subcommand provided, use the subcommand executor // Check if the user has the required permissions
for (Map.Entry<String, BatSubCommand> subCommand : command.getSubCommands().entrySet()) { if (ranInGuild) {
if (subCommand.getKey().equalsIgnoreCase(event.getInteraction().getSubcommandName())) { List<Permission> missingPermissions = new ArrayList<>();
subCommand.getValue().execute(guild, user, event.getChannel().asTextChannel(), event.getMember(), event.getInteraction()); for (Permission permission : requiredPermissions) {
break; if (!event.getMember().hasPermission(permission)) {
missingPermissions.add(permission);
}
}
if (!missingPermissions.isEmpty()) {
event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("You are missing the following permissions to execute this command: " + missingPermissions)
.build()).queue();
return;
} }
} }
executor.execute(guild, user, event.getChannel().asTextChannel(), event.getMember(), event.getInteraction());
} 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);