Skip to content

Commit

Permalink
Auto create threads for suggestions (#469)
Browse files Browse the repository at this point in the history
* Auto create threads for suggestions

* added a condition to general checks

* made a method for thread creation

* added notnull annotation to message argument in createThread method

* removed unnecessary checks

* changed algorithm to generate thread titles

* removed unnecessary variable
  • Loading branch information
Taz03 authored Jul 21, 2022
1 parent 9305cd0 commit 454f1aa
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
public final class SuggestionsUpDownVoter extends MessageReceiverAdapter {
private static final Logger logger = LoggerFactory.getLogger(SuggestionsUpDownVoter.class);
private static final int TITLE_MAX_LENGTH = 60;
private static final String FALLBACK_UP_VOTE = "👍";
private static final String FALLBACK_DOWN_VOTE = "👎";

Expand All @@ -47,10 +48,27 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
Guild guild = event.getGuild();
Message message = event.getMessage();

createThread(message);
reactWith(config.getUpVoteEmoteName(), FALLBACK_UP_VOTE, guild, message);
reactWith(config.getDownVoteEmoteName(), FALLBACK_DOWN_VOTE, guild, message);
}

private static void createThread(@NotNull Message message) {
String title = message.getContentRaw();

if (title.length() >= TITLE_MAX_LENGTH) {
int lastWordEnd = title.lastIndexOf(' ', TITLE_MAX_LENGTH);

if (lastWordEnd == -1) {
lastWordEnd = TITLE_MAX_LENGTH;
}

title = title.substring(0, lastWordEnd);
}

message.createThreadChannel(title).queue();
}

private static void reactWith(@NotNull String emoteName, @NotNull String fallbackUnicodeEmote,
@NotNull Guild guild, @NotNull Message message) {
getEmoteByName(emoteName, guild).map(message::addReaction).orElseGet(() -> {
Expand Down

0 comments on commit 454f1aa

Please sign in to comment.