|
| 1 | +package net.discordjug.javabot.systems.color_roles; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.util.List; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
| 7 | +import net.discordjug.javabot.data.config.BotConfig; |
| 8 | +import net.discordjug.javabot.util.Responses; |
| 9 | +import net.dv8tion.jda.api.entities.Role; |
| 10 | +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; |
| 11 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 12 | +import net.dv8tion.jda.api.interactions.AutoCompleteQuery; |
| 13 | +import net.dv8tion.jda.api.interactions.commands.Command.Choice; |
| 14 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 15 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 16 | +import net.dv8tion.jda.api.interactions.commands.build.Commands; |
| 17 | +import xyz.dynxsty.dih4jda.interactions.AutoCompletable; |
| 18 | +import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand; |
| 19 | +import xyz.dynxsty.dih4jda.util.AutoCompleteUtils; |
| 20 | + |
| 21 | +public class ChangeColorCommand extends SlashCommand implements AutoCompletable { |
| 22 | + |
| 23 | + private static final Pattern COLOR_PATTERN = Pattern.compile("#[A-F0-9]{6}", Pattern.CASE_INSENSITIVE); |
| 24 | + |
| 25 | + private final BotConfig config; |
| 26 | + private final ColorRoleService service; |
| 27 | + |
| 28 | + public ChangeColorCommand(BotConfig botConfig, ColorRoleService service) { |
| 29 | + this.config = botConfig; |
| 30 | + this.service = service; |
| 31 | + setCommandData(Commands.slash("color", "Sets the color of your roles") |
| 32 | + .addOption(OptionType.STRING, "color", "The hex code of the role color", false, true)); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void execute(SlashCommandInteractionEvent event) { |
| 37 | + Role baseRole = config.get(event.getGuild()).getModerationConfig().getColorRoleChangeRole(); |
| 38 | + if (baseRole == null) { |
| 39 | + Responses.error(event, "This feature is not available.").queue(); |
| 40 | + return; |
| 41 | + } |
| 42 | + if (!event.getMember().getRoles().contains(baseRole)) { |
| 43 | + Responses.error(event, "Only users with the " + baseRole.getAsMention() + " role can change their color.") |
| 44 | + .setAllowedMentions(List.of()) |
| 45 | + .queue(); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + String colorArgument = event.getOption("color", "", OptionMapping::getAsString); |
| 50 | + |
| 51 | + Color color; |
| 52 | + if (colorArgument.isEmpty()) { |
| 53 | + color = null; |
| 54 | + } else { |
| 55 | + if (!colorArgument.startsWith("#")) { |
| 56 | + colorArgument = "#" + colorArgument; |
| 57 | + } |
| 58 | + if (!COLOR_PATTERN.matcher(colorArgument).matches()) { |
| 59 | + Responses.error(event, "'%s' is not a valid color", colorArgument).queue(); |
| 60 | + return; |
| 61 | + } |
| 62 | + try { |
| 63 | + color = Color.decode(colorArgument); |
| 64 | + } catch (NumberFormatException e) { |
| 65 | + Responses.error(event, "'%s' is not a valid color", colorArgument).queue(); |
| 66 | + return; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + boolean removed = service.removeColor(event.getMember()); |
| 71 | + if (color == null) { |
| 72 | + if (removed) { |
| 73 | + Responses.success(event, "Color removed", "Your color role has been removed.").queue(); |
| 74 | + } else { |
| 75 | + Responses.error(event, "You don't have a color role.").queue(); |
| 76 | + } |
| 77 | + return; |
| 78 | + } |
| 79 | + service.addColor(event.getMember(), color); |
| 80 | + Responses.success(event, "Color set", "Your color has been set to `%s`.", service.encodeColor(color)).queue(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) { |
| 85 | + Role baseRole = config.get(event.getGuild()).getModerationConfig().getColorRoleChangeRole(); |
| 86 | + if (baseRole == null) { |
| 87 | + event.replyChoice("Feature unavailable", "This feature is not available.").queue(); |
| 88 | + return; |
| 89 | + } |
| 90 | + if (!event.getMember().getRoles().contains(baseRole)) { |
| 91 | + event.replyChoice("Not permitted", "You cannot use this feature.").queue(); |
| 92 | + return; |
| 93 | + } |
| 94 | + List<Choice> choices = service.getColorRoles(event.getGuild()) |
| 95 | + .stream() |
| 96 | + .map(rc -> new Choice(service.encodeColor(rc.color()), service.encodeColor(rc.color()))) |
| 97 | + .toList(); |
| 98 | + event.replyChoices(AutoCompleteUtils.filterChoices(event, choices)).queue(); |
| 99 | + } |
| 100 | +} |
0 commit comments