Skip to content

Auto create threads for suggestions #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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