From a018539fa5ce5acbe4c72a83e22f3aa48e2f7f05 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Tue, 7 Mar 2023 16:03:29 +0530 Subject: [PATCH 01/10] Set up config and create reaction listener --- application/config.json.template | 6 ++++ .../org/togetherjava/tjbot/config/Config.java | 8 ++++- .../tjbot/config/OofsAndLmaosConfig.java | 30 +++++++++++++++++++ .../features/basic/OofsAndLmaosStarboard.java | 27 +++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/config/OofsAndLmaosConfig.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/features/basic/OofsAndLmaosStarboard.java diff --git a/application/config.json.template b/application/config.json.template index a1aec8f470..7736c546b4 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -96,6 +96,12 @@ "baseUrl": "", "rateLimitWindowSeconds": 10, "rateLimitRequestsInWindow": 3 + } + "oofsAndLmaos": { + "oofEmojiName": ":oof:" + "lmaoEmojiName": ":lmao" + "starboardChannelId": + } }, "featureBlacklist": { "normal": [ diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index e819f8e7d1..41093d2078 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -46,6 +46,7 @@ public final class Config { private final RSSFeedsConfig rssFeedsConfig; private final String selectRolesChannelPattern; private final String memberCountCategoryPattern; + private final OofsAndLmaosConfig oofsAndLmaos; @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) @@ -94,7 +95,8 @@ private Config(@JsonProperty(value = "token", required = true) String token, required = true) FeatureBlacklistConfig featureBlacklistConfig, @JsonProperty(value = "rssConfig", required = true) RSSFeedsConfig rssFeedsConfig, @JsonProperty(value = "selectRolesChannelPattern", - required = true) String selectRolesChannelPattern) { + required = true) String selectRolesChannelPattern, + @JsonProperty(value = "oofsAndLmaos", required = true) OofsAndLmaosConfig oofsAndLmaos) { this.token = Objects.requireNonNull(token); this.githubApiKey = Objects.requireNonNull(githubApiKey); this.databasePath = Objects.requireNonNull(databasePath); @@ -127,6 +129,7 @@ private Config(@JsonProperty(value = "token", required = true) String token, this.featureBlacklistConfig = Objects.requireNonNull(featureBlacklistConfig); this.rssFeedsConfig = Objects.requireNonNull(rssFeedsConfig); this.selectRolesChannelPattern = Objects.requireNonNull(selectRolesChannelPattern); + this.oofsAndLmaos = Objects.requireNonNull(oofsAndLmaos); } /** @@ -380,6 +383,9 @@ public String getSourceCodeBaseUrl() { */ public JShellConfig getJshell() { return jshell; + } + public OofsAndLmaosConfig getOofsAndLmaos() { + return oofsAndLmaos; } /** diff --git a/application/src/main/java/org/togetherjava/tjbot/config/OofsAndLmaosConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/OofsAndLmaosConfig.java new file mode 100644 index 0000000000..cc00ec3970 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/config/OofsAndLmaosConfig.java @@ -0,0 +1,30 @@ +package org.togetherjava.tjbot.config; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonRootName; + +@JsonRootName("oofsAndLmaos") +public final class OofsAndLmaosConfig { + private final String oofEmojiName; + private final String lmaoEmojiName; + private final long starboardChannelId; + + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public OofsAndLmaosConfig(String oofEmojiName, String lmaoEmojiName, long starboardChannelId) { + this.oofEmojiName = oofEmojiName; + this.lmaoEmojiName = lmaoEmojiName; + this.starboardChannelId = starboardChannelId; + } + + public String getOofEmojiName() { + return oofEmojiName; + } + + public String getLmaoEmojiName() { + return lmaoEmojiName; + } + + public long getStarboardChannelId() { + return starboardChannelId; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/OofsAndLmaosStarboard.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/OofsAndLmaosStarboard.java new file mode 100644 index 0000000000..c08638f150 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/OofsAndLmaosStarboard.java @@ -0,0 +1,27 @@ +package org.togetherjava.tjbot.features.basic; + +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; + +import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.config.OofsAndLmaosConfig; +import org.togetherjava.tjbot.features.EventReceiver; + +public class OofsAndLmaosStarboard extends ListenerAdapter implements EventReceiver { + + private final OofsAndLmaosConfig config; + + public OofsAndLmaosStarboard(Config config) { + this.config = config.getOofsAndLmaos(); + } + + @Override + public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { + String emojiName = event.getReaction().getEmoji().asCustom().getName(); + MessageChannel channel = event.getChannel(); + // TODO + + } +} From fdd7bce1fdf1336efd6522ab157a28c098b53060 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Fri, 6 Oct 2023 18:23:20 +0530 Subject: [PATCH 02/10] squashed everything(kinda) --- application/config.json.template | 20 +++- .../org/togetherjava/tjbot/config/Config.java | 21 +++-- .../tjbot/config/HelperPruneConfig.java | 19 ++++ .../tjbot/config/OofsAndLmaosConfig.java | 30 ------ .../tjbot/config/StarboardConfig.java | 53 +++++++++++ .../togetherjava/tjbot/features/Features.java | 2 + .../features/basic/OofsAndLmaosStarboard.java | 27 ------ .../tjbot/features/basic/Starboard.java | 91 +++++++++++++++++++ .../main/resources/db/V14__Add_Starboard.sql | 4 + 9 files changed, 200 insertions(+), 67 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/config/HelperPruneConfig.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/config/OofsAndLmaosConfig.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/config/StarboardConfig.java delete mode 100644 application/src/main/java/org/togetherjava/tjbot/features/basic/OofsAndLmaosStarboard.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java create mode 100644 application/src/main/resources/db/V14__Add_Starboard.sql diff --git a/application/config.json.template b/application/config.json.template index 7736c546b4..927ed1d79f 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -96,11 +96,23 @@ "baseUrl": "", "rateLimitWindowSeconds": 10, "rateLimitRequestsInWindow": 3 + }, + "helperPruneConfig": { + "roleFullLimit": 100, + "roleFullThreshold": 95, + "pruneMemberAmount": 7, + "inactivateAfterDays": 90, + "recentlyJoinedDays": 4 + }, + "featureBlacklist": { + "normal": [ + ], + "special": [ + ] } - "oofsAndLmaos": { - "oofEmojiName": ":oof:" - "lmaoEmojiName": ":lmao" - "starboardChannelId": + "starboard": { + "emojiNames" : ["star"], + "channelPattern": "starboard" } }, "featureBlacklist": { diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 41093d2078..cf98de6e8a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -42,11 +42,12 @@ public final class Config { private final String openaiApiKey; private final String sourceCodeBaseUrl; private final JShellConfig jshell; + private final StarboardConfig starboard; private final FeatureBlacklistConfig featureBlacklistConfig; private final RSSFeedsConfig rssFeedsConfig; private final String selectRolesChannelPattern; private final String memberCountCategoryPattern; - private final OofsAndLmaosConfig oofsAndLmaos; + @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) @@ -96,7 +97,7 @@ private Config(@JsonProperty(value = "token", required = true) String token, @JsonProperty(value = "rssConfig", required = true) RSSFeedsConfig rssFeedsConfig, @JsonProperty(value = "selectRolesChannelPattern", required = true) String selectRolesChannelPattern, - @JsonProperty(value = "oofsAndLmaos", required = true) OofsAndLmaosConfig oofsAndLmaos) { + @JsonProperty(value = "starboard", required = true) StarboardConfig starboard) { this.token = Objects.requireNonNull(token); this.githubApiKey = Objects.requireNonNull(githubApiKey); this.databasePath = Objects.requireNonNull(databasePath); @@ -129,7 +130,7 @@ private Config(@JsonProperty(value = "token", required = true) String token, this.featureBlacklistConfig = Objects.requireNonNull(featureBlacklistConfig); this.rssFeedsConfig = Objects.requireNonNull(rssFeedsConfig); this.selectRolesChannelPattern = Objects.requireNonNull(selectRolesChannelPattern); - this.oofsAndLmaos = Objects.requireNonNull(oofsAndLmaos); + this.starboard = Objects.requireNonNull(starboard); } /** @@ -383,9 +384,6 @@ public String getSourceCodeBaseUrl() { */ public JShellConfig getJshell() { return jshell; - } - public OofsAndLmaosConfig getOofsAndLmaos() { - return oofsAndLmaos; } /** @@ -397,6 +395,17 @@ public FeatureBlacklistConfig getFeatureBlacklistConfig() { return featureBlacklistConfig; } + /** + * Gets the config for the Starboard. The starboard displays certain messages in a special + * emojis{@link StarboardConfig#emojiNames()} + * channel {@link StarboardConfig#channelPattern()} if a user reacts with one of the recognized + * + * @return the config of the Starboard + */ + public StarboardConfig getStarboard() { + return starboard; + } + /** * Gets the REGEX pattern used to identify the channel in which users can select their helper * roles. diff --git a/application/src/main/java/org/togetherjava/tjbot/config/HelperPruneConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/HelperPruneConfig.java new file mode 100644 index 0000000000..6f451b491f --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/config/HelperPruneConfig.java @@ -0,0 +1,19 @@ +package org.togetherjava.tjbot.config; + + +/** + * Config for automatic pruning of helper roles, see + * {@link org.togetherjava.tjbot.features.help.AutoPruneHelperRoutine}. + * + * @param roleFullLimit if a helper role contains that many users, it is considered full and pruning + * must occur + * @param roleFullThreshold if a helper role contains that many users, pruning will start to occur + * to prevent reaching the limit + * @param pruneMemberAmount amount of users to remove from helper roles during a prune + * @param inactivateAfterDays after how many days of inactivity a user is eligible for pruning + * @param recentlyJoinedDays if a user is with the server for just this amount of days, they are + * protected from pruning + */ +public record HelperPruneConfig(int roleFullLimit, int roleFullThreshold, int pruneMemberAmount, + int inactivateAfterDays, int recentlyJoinedDays) { +} diff --git a/application/src/main/java/org/togetherjava/tjbot/config/OofsAndLmaosConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/OofsAndLmaosConfig.java deleted file mode 100644 index cc00ec3970..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/config/OofsAndLmaosConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.togetherjava.tjbot.config; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonRootName; - -@JsonRootName("oofsAndLmaos") -public final class OofsAndLmaosConfig { - private final String oofEmojiName; - private final String lmaoEmojiName; - private final long starboardChannelId; - - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - public OofsAndLmaosConfig(String oofEmojiName, String lmaoEmojiName, long starboardChannelId) { - this.oofEmojiName = oofEmojiName; - this.lmaoEmojiName = lmaoEmojiName; - this.starboardChannelId = starboardChannelId; - } - - public String getOofEmojiName() { - return oofEmojiName; - } - - public String getLmaoEmojiName() { - return lmaoEmojiName; - } - - public long getStarboardChannelId() { - return starboardChannelId; - } -} diff --git a/application/src/main/java/org/togetherjava/tjbot/config/StarboardConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/StarboardConfig.java new file mode 100644 index 0000000000..e55f00ed4e --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/config/StarboardConfig.java @@ -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 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()}. + *

+ * Empty to deactivate the feature. + * + * @return The List of emojis recognized by the starboard + */ + @Override + public List 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; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/features/Features.java b/application/src/main/java/org/togetherjava/tjbot/features/Features.java index 9b837799ef..6fdf45a63a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -10,6 +10,7 @@ import org.togetherjava.tjbot.features.basic.PingCommand; import org.togetherjava.tjbot.features.basic.RoleSelectCommand; import org.togetherjava.tjbot.features.basic.SlashCommandEducator; +import org.togetherjava.tjbot.features.basic.Starboard; import org.togetherjava.tjbot.features.basic.SuggestionsUpDownVoter; import org.togetherjava.tjbot.features.bookmarks.BookmarksCommand; import org.togetherjava.tjbot.features.bookmarks.BookmarksSystem; @@ -131,6 +132,7 @@ public static Collection createFeatures(JDA jda, Database database, Con features.add(new GuildLeaveCloseThreadListener(config)); features.add(new LeftoverBookmarksListener(bookmarksSystem)); features.add(new HelpThreadCreatedListener(helpSystemHelper)); + features.add(new Starboard(config, database)); // Message context commands features.add(new TransferQuestionCommand(config)); diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/OofsAndLmaosStarboard.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/OofsAndLmaosStarboard.java deleted file mode 100644 index c08638f150..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/OofsAndLmaosStarboard.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.togetherjava.tjbot.features.basic; - -import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; -import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; -import net.dv8tion.jda.api.hooks.ListenerAdapter; -import org.jetbrains.annotations.NotNull; - -import org.togetherjava.tjbot.config.Config; -import org.togetherjava.tjbot.config.OofsAndLmaosConfig; -import org.togetherjava.tjbot.features.EventReceiver; - -public class OofsAndLmaosStarboard extends ListenerAdapter implements EventReceiver { - - private final OofsAndLmaosConfig config; - - public OofsAndLmaosStarboard(Config config) { - this.config = config.getOofsAndLmaos(); - } - - @Override - public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { - String emojiName = event.getReaction().getEmoji().asCustom().getName(); - MessageChannel channel = event.getChannel(); - // TODO - - } -} diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java new file mode 100644 index 0000000000..0a3f03667d --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java @@ -0,0 +1,91 @@ +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.Guild; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.User; +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.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 messageCache; + + public Starboard(Config config, Database database) { + 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().asCustom().getName(); + Guild guild = event.getGuild(); + long messageId = event.getMessageIdLong(); + if (shouldIgnoreMessage(emojiName, guild, event.getGuildChannel(), messageId)) { + return; + } + Optional 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(); + } + + private boolean shouldIgnoreMessage(String emojiName, Guild guild, GuildChannel channel, + long messageId) { + return !config.emojiNames().contains(emojiName) + || !guild.getPublicRole().hasPermission(channel, Permission.VIEW_CHANNEL) + || messageCache.getIfPresent(messageId) != null + || database + .read(context -> context.fetchExists(context.selectFrom(STARBOARD_MESSAGES) + .where(STARBOARD_MESSAGES.MESSAGE_ID.eq(messageId)))); + } + + private Optional 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()) + .build(); // TODO make footer with link and reacted emojis + } +} diff --git a/application/src/main/resources/db/V14__Add_Starboard.sql b/application/src/main/resources/db/V14__Add_Starboard.sql new file mode 100644 index 0000000000..d683cab67b --- /dev/null +++ b/application/src/main/resources/db/V14__Add_Starboard.sql @@ -0,0 +1,4 @@ +CREATE TABLE starboard_messages +( + message_id BIGINT NOT NULL PRIMARY KEY +) From 1aa16c6eeef47ea9c58adb05d51b0108f9709fb6 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 8 Oct 2023 13:45:11 +0530 Subject: [PATCH 03/10] oops --- application/config.json.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/config.json.template b/application/config.json.template index 927ed1d79f..c35183610b 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -109,7 +109,7 @@ ], "special": [ ] - } + }, "starboard": { "emojiNames" : ["star"], "channelPattern": "starboard" From e165c8c1d24b91f47f6ba7e2468142ebc10a7397 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 8 Oct 2023 20:24:23 +0530 Subject: [PATCH 04/10] uh do stuff that need to be done --- .../java/org/togetherjava/tjbot/features/basic/Starboard.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java index 0a3f03667d..3abdcea683 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java @@ -86,6 +86,7 @@ private static MessageEmbed formEmbed(Message message) { User author = message.getAuthor(); return new EmbedBuilder().setAuthor(author.getName(), null, author.getAvatarUrl()) .setDescription(message.getContentDisplay()) - .build(); // TODO make footer with link and reacted emojis + .appendDescription(" [Link](%s)".formatted(message.getJumpUrl())) + .build(); } } From eda96c5d1411bdd28799c624387ab7975b3048be Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 5 Nov 2023 16:51:46 +0530 Subject: [PATCH 05/10] some necessary stuff --- application/config.json.template | 2 +- .../java/org/togetherjava/tjbot/features/basic/Starboard.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/application/config.json.template b/application/config.json.template index c35183610b..6a12c88922 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -111,7 +111,7 @@ ] }, "starboard": { - "emojiNames" : ["star"], + "emojiNames" : ["⭐"], "channelPattern": "starboard" } }, diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java index 3abdcea683..7e1d10a0a0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java @@ -45,7 +45,7 @@ public Starboard(Config config, Database database) { @Override public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { - String emojiName = event.getEmoji().asCustom().getName(); + String emojiName = event.getEmoji().getName(); Guild guild = event.getGuild(); long messageId = event.getMessageIdLong(); if (shouldIgnoreMessage(emojiName, guild, event.getGuildChannel(), messageId)) { From 5d4770bfa81b93110c5ff4d7a26576341fd725a4 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 10 Mar 2024 11:27:58 +0530 Subject: [PATCH 06/10] started working --- .../java/org/togetherjava/tjbot/features/basic/Starboard.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java index 7e1d10a0a0..dbd85f6d7d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java @@ -86,7 +86,7 @@ private static MessageEmbed formEmbed(Message message) { User author = message.getAuthor(); return new EmbedBuilder().setAuthor(author.getName(), null, author.getAvatarUrl()) .setDescription(message.getContentDisplay()) - .appendDescription(" [Link](%s)".formatted(message.getJumpUrl())) + .appendDescription("%n [Link](%s)".formatted(message.getJumpUrl())) .build(); } } From 4417dd1797907b9d20b10d8d3911d3c880cd7abd Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 31 Mar 2024 20:00:28 +0530 Subject: [PATCH 07/10] Added negative effect, bit spaghetti rn --- .../tjbot/features/basic/Starboard.java | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java index dbd85f6d7d..5689021eee 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/Starboard.java @@ -4,13 +4,11 @@ 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.Guild; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.MessageEmbed; -import net.dv8tion.jda.api.entities.User; +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; @@ -48,7 +46,7 @@ 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)) { + if (shouldIgnoreMessage(emojiName, guild, event.getGuildChannel(), messageId, true)) { return; } Optional starboardChannel = getStarboardChannel(guild); @@ -65,14 +63,35 @@ public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { .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) { + long messageId, boolean addingMessage) { return !config.emojiNames().contains(emojiName) || !guild.getPublicRole().hasPermission(channel, Permission.VIEW_CHANNEL) - || messageCache.getIfPresent(messageId) != null - || database + || (addingMessage == (messageCache.getIfPresent(messageId) != null || database .read(context -> context.fetchExists(context.selectFrom(STARBOARD_MESSAGES) - .where(STARBOARD_MESSAGES.MESSAGE_ID.eq(messageId)))); + .where(STARBOARD_MESSAGES.MESSAGE_ID.eq(messageId)))))); } private Optional getStarboardChannel(Guild guild) { From 6de1451e0858d68e6f70080bec977d87390bf325 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Mon, 1 Apr 2024 11:52:12 +0530 Subject: [PATCH 08/10] some scrambling unsrcambled --- .../src/main/java/org/togetherjava/tjbot/config/Config.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index cf98de6e8a..3317251adf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -397,9 +397,9 @@ public FeatureBlacklistConfig getFeatureBlacklistConfig() { /** * Gets the config for the Starboard. The starboard displays certain messages in a special - * emojis{@link StarboardConfig#emojiNames()} - * channel {@link StarboardConfig#channelPattern()} if a user reacts with one of the recognized - * + * emojis{@link StarboardConfig#emojiNames()} channel {@link StarboardConfig#channelPattern()} + * if a user reacts with one of the recognized + * * @return the config of the Starboard */ public StarboardConfig getStarboard() { From 2127d4f6e6c27ab917eb8d2be1a734d895fe1d0b Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Tue, 7 May 2024 13:30:52 +0530 Subject: [PATCH 09/10] fix template --- application/config.json.template | 8 -------- .../db/{V14__Add_Starboard.sql => V15__Add_Starboard.sql} | 0 2 files changed, 8 deletions(-) rename application/src/main/resources/db/{V14__Add_Starboard.sql => V15__Add_Starboard.sql} (100%) diff --git a/application/config.json.template b/application/config.json.template index 6a12c88922..7965e35add 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -97,13 +97,6 @@ "rateLimitWindowSeconds": 10, "rateLimitRequestsInWindow": 3 }, - "helperPruneConfig": { - "roleFullLimit": 100, - "roleFullThreshold": 95, - "pruneMemberAmount": 7, - "inactivateAfterDays": 90, - "recentlyJoinedDays": 4 - }, "featureBlacklist": { "normal": [ ], @@ -114,7 +107,6 @@ "emojiNames" : ["⭐"], "channelPattern": "starboard" } - }, "featureBlacklist": { "normal": [ ], diff --git a/application/src/main/resources/db/V14__Add_Starboard.sql b/application/src/main/resources/db/V15__Add_Starboard.sql similarity index 100% rename from application/src/main/resources/db/V14__Add_Starboard.sql rename to application/src/main/resources/db/V15__Add_Starboard.sql From 59dc7bee7d8d70a83497117d026f5ddaa5858c5e Mon Sep 17 00:00:00 2001 From: java-coding-prodigy <85149944+java-coding-prodigy@users.noreply.github.com> Date: Tue, 7 May 2024 22:33:19 +0530 Subject: [PATCH 10/10] update... --- application/config.json.template | 6 ------ .../tjbot/config/HelperPruneConfig.java | 19 ------------------- 2 files changed, 25 deletions(-) delete mode 100644 application/src/main/java/org/togetherjava/tjbot/config/HelperPruneConfig.java diff --git a/application/config.json.template b/application/config.json.template index 7965e35add..c53ceccf00 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -106,12 +106,6 @@ "starboard": { "emojiNames" : ["⭐"], "channelPattern": "starboard" - } - "featureBlacklist": { - "normal": [ - ], - "special": [ - ] }, "selectRolesChannelPattern": "select-your-roles", "rssConfig": { diff --git a/application/src/main/java/org/togetherjava/tjbot/config/HelperPruneConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/HelperPruneConfig.java deleted file mode 100644 index 6f451b491f..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/config/HelperPruneConfig.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.togetherjava.tjbot.config; - - -/** - * Config for automatic pruning of helper roles, see - * {@link org.togetherjava.tjbot.features.help.AutoPruneHelperRoutine}. - * - * @param roleFullLimit if a helper role contains that many users, it is considered full and pruning - * must occur - * @param roleFullThreshold if a helper role contains that many users, pruning will start to occur - * to prevent reaching the limit - * @param pruneMemberAmount amount of users to remove from helper roles during a prune - * @param inactivateAfterDays after how many days of inactivity a user is eligible for pruning - * @param recentlyJoinedDays if a user is with the server for just this amount of days, they are - * protected from pruning - */ -public record HelperPruneConfig(int roleFullLimit, int roleFullThreshold, int pruneMemberAmount, - int inactivateAfterDays, int recentlyJoinedDays) { -}