-
Notifications
You must be signed in to change notification settings - Fork 24
Add the ability to add a ⭐ to a message in the #starboard channel as well as the original message #167
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
Closed
Closed
Add the ability to add a ⭐ to a message in the #starboard channel as well as the original message #167
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
737a22d
Starboard Feature Update.
venkat1701 dc12fd7
Starboard Code Update.
venkat1701 167f6f7
Merge branch 'Java-Discord:main' into krish/starboard
venkat1701 260b6fb
Starboard Reaction Counts Updated.
venkat1701 14a38cc
Null Check Updated.
venkat1701 a211ce6
Removed Objects#requireNonNull
venkat1701 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.*; | ||
import net.dv8tion.jda.api.events.message.MessageDeleteEvent; | ||
import net.dv8tion.jda.api.events.message.react.GenericMessageReactionEvent; | ||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; | ||
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; | ||
import net.dv8tion.jda.api.exceptions.ErrorHandler; | ||
|
@@ -19,6 +20,11 @@ | |
import org.bson.Document; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.stream.Stream; | ||
|
||
import static com.mongodb.client.model.Filters.eq; | ||
|
||
@Slf4j | ||
|
@@ -53,18 +59,24 @@ void addToSB(Guild guild, MessageChannel channel, Message message, int starCount | |
e.printStackTrace(); | ||
} | ||
} | ||
msgAction.queue(sbMsg -> db.createStarboardDoc(guildId, channelId, messageId, sbMsg.getId())); | ||
msgAction.queue(sbMsg -> { | ||
db.createStarboardDoc(guildId, channelId, messageId, sbMsg.getId()); | ||
sbMsg.addReaction(config.getEmotes().get(0)).queue(); | ||
}); | ||
} | ||
|
||
void updateSB(Guild guild, String channelId, String messageId, int reactionCount) { | ||
void updateSB(Guild guild, String channelId, String messageId, int reactionCount, Message message) { | ||
Database db = new Database(); | ||
String guildId = guild.getId(); | ||
String sbcEmbedId = db.getStarboardChannelString(guildId, channelId, messageId, "starboard_embed"); | ||
var config = Bot.config.get(guild).getStarBoard(); | ||
if (sbcEmbedId == null) return; | ||
|
||
Bot.config.get(guild).getStarBoard().getChannel() | ||
.retrieveMessageById(sbcEmbedId).queue((sbMsg) -> handleMessage(guild, channelId, messageId, reactionCount, sbMsg), | ||
failure -> this.removeMessageFromStarboard(guild, messageId)); | ||
.retrieveMessageById(sbcEmbedId).queue((sbMsg) -> { | ||
// Adding -1 to the UniqueStarCount since it should not count for the ⭐ of JavaBot. | ||
this.handleMessage(guild, channelId, messageId, this.getUniqueStarCounts(sbMsg, message, config)-1, sbMsg); | ||
}, failure -> this.removeMessageFromStarboard(guild, messageId)); | ||
} | ||
|
||
private void handleMessage(Guild guild, String channelId, String messageId, int reactionCount, Message sbMsg) { | ||
|
@@ -133,7 +145,7 @@ private void processStarboardMessage(Guild guild, String guildId, MongoCollectio | |
if (!db.isMessageOnStarboard(guildId, channelId, messageId) && reactionCount >= config.getReactionThreshold()) { | ||
addToSB(guild, channel, msg, reactionCount); | ||
} else if (db.getStarboardChannelString(guildId, channelId, messageId, "starboard_embed") != null) { | ||
updateSB(guild, channelId, messageId, reactionCount); | ||
updateSB(guild, channelId, messageId, reactionCount, msg); | ||
} | ||
}, failure -> collection.deleteOne(doc)); | ||
} | ||
|
@@ -147,7 +159,7 @@ private int getReactionCountForEmote(String emote, Message msg) { | |
} | ||
|
||
private void updateIfEmpty(Guild guild, String channelId, String messageId, Message msg) { | ||
if (msg.getReactions().isEmpty()) updateSB(guild, channelId, messageId, 0); | ||
if (msg.getReactions().isEmpty()) updateSB(guild, channelId, messageId, 0, msg); | ||
} | ||
|
||
@Override | ||
|
@@ -166,15 +178,22 @@ public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { | |
String channelId = event.getChannel().getId(); | ||
String messageId = event.getMessageId(); | ||
|
||
event.getChannel().retrieveMessageById(messageId).queue(message -> { | ||
int reactionCount = getReactionCountForEmote(config.getEmotes().get(0), message); | ||
if (channelId.equals(config.getChannel().getId())) { | ||
this.updateSbReactionCounts(event, config, messageId, channelId); | ||
} | ||
|
||
if (db.isMessageOnStarboard(guildId, channelId, messageId)) { | ||
updateSB(event.getGuild(), channelId, messageId, reactionCount); | ||
} else if (reactionCount >= config.getReactionThreshold()) { | ||
addToSB(event.getGuild(), event.getChannel(), message, reactionCount); | ||
} | ||
}); | ||
if (!channelId.equals(config.getChannel().getId())) { | ||
event.getChannel().retrieveMessageById(messageId).queue(message -> { | ||
|
||
|
||
int reactionCount = getReactionCountForEmote(config.getEmotes().get(0), message); | ||
if (db.isMessageOnStarboard(guildId, channelId, messageId)) { | ||
updateSB(event.getGuild(), channelId, messageId, reactionCount, message); | ||
} else if (reactionCount >= config.getReactionThreshold()) { | ||
addToSB(event.getGuild(), event.getChannel(), message, reactionCount); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -190,21 +209,55 @@ public void onMessageReactionRemove(@NotNull MessageReactionRemoveEvent event) { | |
String channelId = event.getChannel().getId(); | ||
String messageId = event.getMessageId(); | ||
|
||
event.getChannel().retrieveMessageById(messageId).queue(message -> { | ||
int reactionCount = getReactionCountForEmote(config.getEmotes().get(0), message); | ||
if (channelId.equals(config.getChannel().getId())) { | ||
this.updateSbReactionCounts(event, config, messageId, channelId); | ||
} | ||
|
||
if (db.isMessageOnStarboard(guildId, channelId, messageId)) { | ||
updateSB(event.getGuild(), channelId, messageId, reactionCount); | ||
} else if (reactionCount >= config.getReactionThreshold()) { | ||
addToSB(event.getGuild(), event.getChannel(), message, reactionCount); | ||
} else { | ||
removeMessageFromStarboard(event.getGuild(), messageId); | ||
} | ||
}); | ||
if (!channelId.equals(config.getChannel().getId())) { | ||
|
||
event.getChannel().retrieveMessageById(messageId).queue(message -> { | ||
int reactionCount = getReactionCountForEmote(config.getEmotes().get(0), message); | ||
if (db.isMessageOnStarboard(guildId, channelId, messageId)) { | ||
updateSB(event.getGuild(), channelId, messageId, reactionCount, message); | ||
} else if (reactionCount >= config.getReactionThreshold()) { | ||
addToSB(event.getGuild(), event.getChannel(), message, reactionCount); | ||
} else { | ||
removeMessageFromStarboard(event.getGuild(), messageId); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void onMessageDelete(@NotNull MessageDeleteEvent event) { | ||
removeMessageFromStarboard(event.getGuild(), event.getMessageId()); | ||
} | ||
|
||
private void updateSbReactionCounts(GenericMessageReactionEvent event, StarBoardConfig config, String messageId, String remoteTextChannelId) { | ||
Objects.requireNonNull(event.getGuild().getTextChannelById(remoteTextChannelId)).retrieveMessageById(messageId).queue(message -> { | ||
MessageEmbed me = message.getEmbeds().get(0); | ||
String prevChannelId = Objects.requireNonNull(me.getAuthor().getUrl()).split("/")[5]; | ||
String originalMessageId = me.getAuthor().getUrl().split("/")[6]; | ||
|
||
Objects.requireNonNull(event.getGuild().getTextChannelById(prevChannelId)).retrieveMessageById(originalMessageId).queue(msg -> { | ||
venkat1701 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Adding -1 to the UniqueStarCount since it should not count for the ⭐ of JavaBot. | ||
this.updateMessage( | ||
event.getGuild(), | ||
prevChannelId, | ||
this.getUniqueStarCounts(message, msg, config)-1, | ||
message, config); | ||
}); | ||
}); | ||
} | ||
|
||
private int getUniqueStarCounts(Message starboardMessage, Message originalMessage, StarBoardConfig config){ | ||
try { | ||
List<User> list = originalMessage.retrieveReactionUsers(config.getEmotes().get(0)).takeAsync(this.getReactionCountForEmote(config.getEmotes().get(0), originalMessage)).get(); | ||
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. give these variables a valid name instead so list |
||
List<User> list2 = starboardMessage.retrieveReactionUsers(config.getEmotes().get(0)).takeAsync(this.getReactionCountForEmote(config.getEmotes().get(0), starboardMessage)).get(); | ||
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. same here |
||
|
||
Stream<User> stream = Stream.concat(list.stream(), list2.stream()).distinct(); | ||
|
||
return (int)stream.count(); | ||
}catch(InterruptedException | ExecutionException e){ return 0; } | ||
} | ||
} |
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.
replace
Objects.requireNonNull
with a null checkObjects.requireNonNull
throws exceptions