-
-
Notifications
You must be signed in to change notification settings - Fork 93
Up- and down-voting on suggestions #385
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.togetherjava.tjbot.commands.basic; | ||
|
||
import net.dv8tion.jda.api.entities.Emote; | ||
import net.dv8tion.jda.api.entities.Guild; | ||
import net.dv8tion.jda.api.entities.Message; | ||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.togetherjava.tjbot.commands.MessageReceiverAdapter; | ||
import org.togetherjava.tjbot.config.Config; | ||
import org.togetherjava.tjbot.config.SuggestionsConfig; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Listener that receives all sent messages from suggestion channels and reacts with an up- and | ||
* down-vote on them to indicate to users that they can vote on the suggestion. | ||
*/ | ||
public final class SuggestionsUpDownVoter extends MessageReceiverAdapter { | ||
private static final Logger logger = LoggerFactory.getLogger(SuggestionsUpDownVoter.class); | ||
private static final String FALLBACK_UP_VOTE = "👍"; | ||
private static final String FALLBACK_DOWN_VOTE = "👎"; | ||
|
||
private final SuggestionsConfig config; | ||
|
||
/** | ||
* Creates a new listener to receive all message sent in suggestion channels. | ||
* | ||
* @param config the config to use for this | ||
*/ | ||
public SuggestionsUpDownVoter(@NotNull Config config) { | ||
super(Pattern.compile(config.getSuggestions().getChannelPattern())); | ||
|
||
this.config = config.getSuggestions(); | ||
} | ||
|
||
@Override | ||
public void onMessageReceived(@NotNull GuildMessageReceivedEvent event) { | ||
if (event.getAuthor().isBot() || event.isWebhookMessage()) { | ||
return; | ||
} | ||
|
||
Guild guild = event.getGuild(); | ||
Message message = event.getMessage(); | ||
|
||
reactWith(config.getUpVoteEmoteName(), FALLBACK_UP_VOTE, guild, message); | ||
reactWith(config.getDownVoteEmoteName(), FALLBACK_DOWN_VOTE, guild, message); | ||
} | ||
|
||
private static void reactWith(@NotNull String emoteName, @NotNull String fallbackUnicodeEmote, | ||
@NotNull Guild guild, @NotNull Message message) { | ||
getEmoteByName(emoteName, guild).map(message::addReaction).orElseGet(() -> { | ||
logger.warn( | ||
"Unable to vote on a suggestion with the configured emote ('{}'), using fallback instead.", | ||
emoteName); | ||
return message.addReaction(fallbackUnicodeEmote); | ||
}).queue(); | ||
} | ||
|
||
private static @NotNull Optional<Emote> getEmoteByName(@NotNull String name, | ||
@NotNull Guild guild) { | ||
return guild.getEmotesByName(name, false).stream().findAny(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
application/src/main/java/org/togetherjava/tjbot/config/SuggestionsConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.togetherjava.tjbot.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonRootName; | ||
|
||
/** | ||
* Configuration for the suggestion system, see | ||
* {@link org.togetherjava.tjbot.commands.basic.SuggestionsUpDownVoter}. | ||
*/ | ||
@SuppressWarnings("ClassCanBeRecord") | ||
@JsonRootName("suggestions") | ||
public final class SuggestionsConfig { | ||
private final String channelPattern; | ||
private final String upVoteEmoteName; | ||
private final String downVoteEmoteName; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
private SuggestionsConfig(@JsonProperty("channelPattern") String channelPattern, | ||
@JsonProperty("upVoteEmoteName") String upVoteEmoteName, | ||
@JsonProperty("downVoteEmoteName") String downVoteEmoteName) { | ||
this.channelPattern = channelPattern; | ||
this.upVoteEmoteName = upVoteEmoteName; | ||
this.downVoteEmoteName = downVoteEmoteName; | ||
} | ||
|
||
/** | ||
* Gets the REGEX pattern used to identify channels that are used for sending suggestions. | ||
* | ||
* @return the channel name pattern | ||
*/ | ||
public String getChannelPattern() { | ||
return channelPattern; | ||
} | ||
|
||
/** | ||
* Gets the name of the emote used to up-vote suggestions. | ||
* | ||
* @return the name of the up-vote emote | ||
*/ | ||
public String getUpVoteEmoteName() { | ||
return upVoteEmoteName; | ||
} | ||
|
||
/** | ||
* Gets the name of the emote used to down-vote suggestions. | ||
* | ||
* @return the name of the down-vote emote | ||
*/ | ||
public String getDownVoteEmoteName() { | ||
return downVoteEmoteName; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.