-
-
Notifications
You must be signed in to change notification settings - Fork 89
Add Starboard support #881
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
Open
java-coding-prodigy
wants to merge
10
commits into
develop
Choose a base branch
from
oofs_and_lmaos
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a018539
Set up config and create reaction listener
java-coding-prodigy fdd7bce
squashed everything(kinda)
java-coding-prodigy 1aa16c6
oops
java-coding-prodigy e165c8c
uh do stuff that need to be done
java-coding-prodigy eda96c5
some necessary stuff
java-coding-prodigy 5d4770b
started working
java-coding-prodigy 4417dd1
Added negative effect, bit spaghetti rn
java-coding-prodigy 6de1451
some scrambling unsrcambled
java-coding-prodigy 2127d4f
fix template
java-coding-prodigy 59dc7be
update...
java-coding-prodigy 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
53 changes: 53 additions & 0 deletions
53
application/src/main/java/org/togetherjava/tjbot/config/StarboardConfig.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.JsonRootName; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Starboard Config | ||
* | ||
* @param emojiNames the List of emojis which are recognized by the starboard | ||
* @param channelPattern the pattern of the channel with the starboard | ||
*/ | ||
@JsonRootName("starboard") | ||
public record StarboardConfig(List<String> emojiNames, Pattern channelPattern) { | ||
/** | ||
* Creates a Starboard config. | ||
* | ||
* @param emojiNames the List of emojis which are recognized by the starboard | ||
* @param channelPattern the pattern of the channel with the starboard | ||
*/ | ||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public StarboardConfig { | ||
Objects.requireNonNull(emojiNames); | ||
Objects.requireNonNull(channelPattern); | ||
} | ||
|
||
/** | ||
* Gets the list of emotes that are recognized by the starboard feature. A message that is | ||
* reacted on with an emote in this list will be reposted in a special channel | ||
* {@link #channelPattern()}. | ||
* <p> | ||
* Empty to deactivate the feature. | ||
* | ||
* @return The List of emojis recognized by the starboard | ||
*/ | ||
@Override | ||
public List<String> emojiNames() { | ||
return emojiNames; | ||
} | ||
|
||
/** | ||
* Gets the pattern of the channel with the starboard. Deactivate by using a non-existent | ||
* channel name. | ||
* | ||
* @return the pattern of the channel with the starboard | ||
*/ | ||
public Pattern channelPattern() { | ||
return channelPattern; | ||
} | ||
} |
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
111 changes: 111 additions & 0 deletions
111
application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.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,111 @@ | ||
package org.togetherjava.tjbot.features.basic; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.Permission; | ||
import net.dv8tion.jda.api.entities.*; | ||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; | ||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; | ||
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.togetherjava.tjbot.config.Config; | ||
import org.togetherjava.tjbot.config.StarboardConfig; | ||
import org.togetherjava.tjbot.db.Database; | ||
import org.togetherjava.tjbot.features.EventReceiver; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.togetherjava.tjbot.db.generated.tables.StarboardMessages.STARBOARD_MESSAGES; | ||
|
||
public class Starboard extends ListenerAdapter implements EventReceiver { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Starboard.class); | ||
private final StarboardConfig config; | ||
private final Database database; | ||
|
||
private final Cache<Long, Object> messageCache; | ||
|
||
public Starboard(Config config, Database database) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing javadoc |
||
this.config = config.getStarboard(); | ||
this.database = database; | ||
this.messageCache = Caffeine.newBuilder() | ||
.maximumSize(100) | ||
.expireAfterAccess(24, TimeUnit.HOURS) // TODO make these constants | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { | ||
String emojiName = event.getEmoji().getName(); | ||
Guild guild = event.getGuild(); | ||
long messageId = event.getMessageIdLong(); | ||
if (shouldIgnoreMessage(emojiName, guild, event.getGuildChannel(), messageId, true)) { | ||
return; | ||
} | ||
Optional<TextChannel> starboardChannel = getStarboardChannel(guild); | ||
if (starboardChannel.isEmpty()) { | ||
logger.warn("There is no channel for the starboard in the guild with the name {}", | ||
config.channelPattern()); | ||
return; | ||
} | ||
database.write(context -> context.newRecord(STARBOARD_MESSAGES).setMessageId(messageId)); | ||
messageCache.put(messageId, new Object()); | ||
event.retrieveMessage() | ||
.flatMap( | ||
message -> starboardChannel.orElseThrow().sendMessageEmbeds(formEmbed(message))) | ||
.queue(); | ||
} | ||
|
||
@Override | ||
public void onMessageReactionRemove(@NotNull MessageReactionRemoveEvent event) { | ||
String emojiName = event.getEmoji().getName(); | ||
Guild guild = event.getGuild(); | ||
long messageId = event.getMessageIdLong(); | ||
if (shouldIgnoreMessage(emojiName, guild, event.getGuildChannel(), messageId, false)) { | ||
return; | ||
} | ||
event.retrieveMessage() | ||
.map(m -> m.getReactions() | ||
.stream() | ||
.map(reaction -> reaction.getEmoji().getName()) | ||
.noneMatch(config.emojiNames()::contains)) | ||
.onSuccess(noGoodReactions -> { | ||
if (noGoodReactions) { | ||
database.write(context -> context.data().remove(messageId)); | ||
messageCache.invalidate(messageId); | ||
} | ||
}) | ||
.queue(); | ||
} | ||
|
||
private boolean shouldIgnoreMessage(String emojiName, Guild guild, GuildChannel channel, | ||
long messageId, boolean addingMessage) { | ||
return !config.emojiNames().contains(emojiName) | ||
|| !guild.getPublicRole().hasPermission(channel, Permission.VIEW_CHANNEL) | ||
|| (addingMessage == (messageCache.getIfPresent(messageId) != null || database | ||
.read(context -> context.fetchExists(context.selectFrom(STARBOARD_MESSAGES) | ||
.where(STARBOARD_MESSAGES.MESSAGE_ID.eq(messageId)))))); | ||
} | ||
|
||
private Optional<TextChannel> getStarboardChannel(Guild guild) { | ||
return guild.getTextChannels() | ||
.stream() | ||
.filter(channel -> config.channelPattern().matcher(channel.getName()).find()) | ||
.findFirst(); | ||
} | ||
|
||
private static MessageEmbed formEmbed(Message message) { | ||
User author = message.getAuthor(); | ||
return new EmbedBuilder().setAuthor(author.getName(), null, author.getAvatarUrl()) | ||
.setDescription(message.getContentDisplay()) | ||
.appendDescription("%n [Link](%s)".formatted(message.getJumpUrl())) | ||
.build(); | ||
} | ||
} |
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,4 @@ | ||
CREATE TABLE starboard_messages | ||
( | ||
message_id BIGINT NOT NULL PRIMARY KEY | ||
) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing javadoc, class should be final