Skip to content

Commit

Permalink
feat: SquidXTV's changes
Browse files Browse the repository at this point in the history
- Slightly alter UI so that the title is the question and the text input
  field has the right placeholder.
- Rename default application channel
- Move certain variables to the bot's configuration
- Use `Duration` instead of `TimeUnit`
- Let the user know how long they need to wait to send another
  application
  • Loading branch information
christolis committed Oct 14, 2024
1 parent 304fc66 commit 0418341
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
8 changes: 6 additions & 2 deletions application/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@
"pollIntervalInMinutes": 10
},
"applicationForm": {
"applicationChannelPattern": "applications-log"
}
"applicationChannelPattern": "staff-applications",
"defaultQuestion": "What makes you a valuable addition to the team? 😎",
"minimumAnswerLength": 50,
"maximumAnswerLength": 500,
"applicationSubmitCooldownMinutes": 5
},
"memberCountCategoryPattern": "Info"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
* Represents the configuration for an application form, including roles and application channel
* pattern.
*/
public record ApplicationFormConfig(@JsonProperty(value = "applicationChannelPattern",
required = true) String applicationChannelPattern) {
public record ApplicationFormConfig(
@JsonProperty(value = "applicationChannelPattern",
required = true) String applicationChannelPattern,
@JsonProperty(value = "defaultQuestion", required = true) String defaultQuestion,
@JsonProperty(value = "minimumAnswerLength", required = true) int minimumAnswerLength,
@JsonProperty(value = "maximumAnswerLength", required = true) int maximumAnswerLength,
@JsonProperty(value = "applicationSubmitCooldownMinutes",
required = true) int applicationSubmitCooldownMinutes) {

/**
* Constructs an instance of {@link ApplicationFormConfig} with the provided parameters.
*
* @param applicationChannelPattern the pattern used to identify the application channel
* @param defaultQuestion the default question for the form
*/
public ApplicationFormConfig {
Objects.requireNonNull(applicationChannelPattern);
Objects.requireNonNull(defaultQuestion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
Expand All @@ -57,18 +56,15 @@ public class ApplicationCreateCommand extends SlashCommandAdapter {
private static final Logger logger = LoggerFactory.getLogger(ApplicationCreateCommand.class);

private static final Color AMBIENT_COLOR = new Color(24, 221, 136, 255);
private static final int MIN_REASON_LENGTH = 50;
private static final int MAX_REASON_LENGTH = 500;
private static final int APPLICATION_SUBMIT_COOLDOWN = 5;
private static final String DEFAULT_QUESTION =
"What makes you a valuable addition to the team? 😎";
private static final int OPTIONAL_ROLES_AMOUNT = 5;
private static final String ROLE_COMPONENT_ID_HEADER = "application-create";
private static final String VALUE_DELIMITER = "_";

private final Cache<Member, OffsetDateTime> applicationSubmitCooldown;
private final Predicate<String> applicationChannelPattern;

private final ApplicationFormConfig formConfig;

/**
* Constructs a new {@link ApplicationCreateCommand} with the specified configuration.
* <p>
Expand All @@ -80,13 +76,14 @@ public ApplicationCreateCommand(Config config) {
super("application-form", "Generates an application form for members to apply for roles.",
CommandVisibility.GUILD);

final ApplicationFormConfig formConfig = config.getApplicationFormConfig();
formConfig = config.getApplicationFormConfig();
this.applicationChannelPattern =
Pattern.compile(formConfig.applicationChannelPattern()).asMatchPredicate();
final Duration applicationSubmitCooldownDuration =
Duration.ofMinutes(formConfig.applicationSubmitCooldownMinutes());

this.applicationSubmitCooldown = Caffeine.newBuilder()
.expireAfterWrite(APPLICATION_SUBMIT_COOLDOWN, TimeUnit.MINUTES)
.build();
this.applicationSubmitCooldown =
Caffeine.newBuilder().expireAfterWrite(applicationSubmitCooldownDuration).build();

generateRoleOptions(getData());
}
Expand Down Expand Up @@ -147,21 +144,30 @@ public void onStringSelectSelection(StringSelectInteractionEvent event, List<Str
OffsetDateTime timeSentCache = applicationSubmitCooldown.getIfPresent(event.getMember());
if (timeSentCache != null) {
Duration duration = Duration.between(timeSentCache, OffsetDateTime.now());
long remainingMinutes =
formConfig.applicationSubmitCooldownMinutes() - duration.toMinutes();

if (duration.toMinutes() < APPLICATION_SUBMIT_COOLDOWN) {
event.reply("Please wait before sending a new application form.")
if (duration.toMinutes() < formConfig.applicationSubmitCooldownMinutes()) {
event
.reply("Please wait %d minutes before sending a new application form."
.formatted(remainingMinutes))
.setEphemeral(true)
.queue();
return;
}
}

String questionLabel = formConfig.defaultQuestion();
if (questionLabel.length() > TextInput.MAX_LABEL_LENGTH) {
questionLabel = questionLabel.substring(0, TextInput.MAX_LABEL_LENGTH);
}

TextInput body = TextInput
.create(generateComponentId(event.getUser().getId()), "Question",
.create(generateComponentId(event.getUser().getId()), questionLabel,
TextInputStyle.PARAGRAPH)
.setRequired(true)
.setRequiredRange(MIN_REASON_LENGTH, MAX_REASON_LENGTH)
.setPlaceholder(DEFAULT_QUESTION)
.setRequiredRange(formConfig.minimumAnswerLength(), formConfig.maximumAnswerLength())
.setPlaceholder("Enter your answer here")
.build();

EmojiUnion emoji = selectOption.getEmoji();
Expand Down Expand Up @@ -329,7 +335,8 @@ private void sendApplicationResult(final ModalInteractionEvent event, List<Strin
MessageEmbed.Field roleField = new MessageEmbed.Field("Role", roleString, false);
embed.addField(roleField);

MessageEmbed.Field answerField = new MessageEmbed.Field(DEFAULT_QUESTION, answer, false);
MessageEmbed.Field answerField =
new MessageEmbed.Field(formConfig.defaultQuestion(), answer, false);
embed.addField(answerField);

applicationChannel.get().sendMessageEmbeds(embed.build()).queue();
Expand Down

0 comments on commit 0418341

Please sign in to comment.