Skip to content

Commit 45876f8

Browse files
Made some requested changes
1 parent fe3d458 commit 45876f8

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/SubmissionManager.java

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.dv8tion.jda.api.entities.MessageEmbed;
99
import net.dv8tion.jda.api.entities.MessageType;
1010
import net.dv8tion.jda.api.entities.User;
11+
import net.dv8tion.jda.api.entities.channel.ChannelType;
1112
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
1213
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
1314
import net.dv8tion.jda.api.interactions.InteractionHook;
@@ -29,9 +30,7 @@
2930

3031
import java.time.Instant;
3132
import java.util.Comparator;
32-
import java.util.HashMap;
3333
import java.util.List;
34-
import java.util.Map;
3534
import java.util.Optional;
3635
import java.util.concurrent.CompletableFuture;
3736
import java.util.concurrent.ExecutorService;
@@ -49,11 +48,6 @@ public class SubmissionManager {
4948
public static final String THREAD_NAME = "%s — %s";
5049
private static final String SUBMISSION_ACCEPTED = "\u2705";
5150
private static final String SUBMISSION_DECLINED = "\u274C";
52-
private static final Map<Long, QOTWSubmission> submissionCache;
53-
54-
static {
55-
submissionCache = new HashMap<>();
56-
}
5751

5852
private final QOTWConfig config;
5953
private final QOTWPointsService pointsService;
@@ -72,11 +66,11 @@ public class SubmissionManager {
7266
public WebhookMessageCreateAction<?> handleSubmission(@NotNull ButtonInteractionEvent event, int questionNumber) {
7367
event.deferEdit().queue();
7468
Member member = event.getMember();
75-
if (!this.canCreateSubmissions(member)) {
69+
if (!canCreateSubmissions(member)) {
7670
return Responses.warning(event.getHook(), "You're not eligible to create a new submission thread.");
7771
}
7872
config.getSubmissionChannel().createThreadChannel(
79-
String.format(THREAD_NAME, questionNumber, member.getEffectiveName()), true).queue(
73+
String.format(THREAD_NAME, questionNumber, member.getId()), true).queue(
8074
thread -> {
8175
thread.addThreadMember(member).queue();
8276
thread.getManager().setInvitable(false).setAutoArchiveDuration(ThreadChannel.AutoArchiveDuration.TIME_1_WEEK).queue();
@@ -91,7 +85,6 @@ public WebhookMessageCreateAction<?> handleSubmission(@NotNull ButtonInteraction
9185
}, err -> ExceptionLogger.capture(err, getClass().getSimpleName()));
9286
QOTWSubmission submission = new QOTWSubmission(thread);
9387
submission.setAuthor(member.getUser());
94-
submissionCache.put(thread.getIdLong(), submission);
9588
} else {
9689
thread.sendMessage("Could not retrieve current QOTW Question. Please contact an Administrator if you think that this is a mistake.")
9790
.queue();
@@ -109,7 +102,7 @@ public WebhookMessageCreateAction<?> handleSubmission(@NotNull ButtonInteraction
109102
public List<QOTWSubmission> getActiveSubmissions() {
110103
return config.getSubmissionChannel().getThreadChannels()
111104
.stream()
112-
.map(this::getOrRetrieveSubmission).toList();
105+
.map(QOTWSubmission::new).toList();
113106
}
114107

115108
/**
@@ -118,14 +111,13 @@ public List<QOTWSubmission> getActiveSubmissions() {
118111
* @param event The {@link ButtonInteractionEvent} that is fired upon use.
119112
*/
120113
public void handleThreadDeletion(@NotNull ButtonInteractionEvent event) {
121-
config.getSubmissionChannel().getThreadChannels()
122-
.stream().filter(t -> t.getIdLong() == event.getChannel().getIdLong())
123-
.map(this::getOrRetrieveSubmission)
124-
.forEach(s -> getOrRetrieveAuthor(s, author -> {
125-
if (event.getUser().getIdLong() == author.getIdLong()) {
126-
s.getThread().delete().queue();
127-
}
128-
}));
114+
if (event.getChannelType() != ChannelType.GUILD_PRIVATE_THREAD) return;
115+
ThreadChannel thread = event.getChannel().asThreadChannel();
116+
getOrRetrieveAuthor(new QOTWSubmission(thread), author -> {
117+
if (event.getUser().getIdLong() == author.getIdLong()) {
118+
thread.delete().queue();
119+
}
120+
});
129121
}
130122

131123
private void getOrRetrieveAuthor(@NotNull QOTWSubmission submission, Consumer<User> onSuccess) {
@@ -134,26 +126,17 @@ private void getOrRetrieveAuthor(@NotNull QOTWSubmission submission, Consumer<Us
134126
} else {
135127
submission.retrieveAuthor(author -> {
136128
submission.setAuthor(author);
137-
submissionCache.put(submission.getThread().getIdLong(), submission);
138129
onSuccess.accept(author);
139130
});
140131
}
141132
}
142133

143-
private QOTWSubmission getOrRetrieveSubmission(@NotNull ThreadChannel thread) {
144-
if (submissionCache.containsKey(thread.getIdLong())) {
145-
return submissionCache.get(thread.getIdLong());
146-
} else {
147-
QOTWSubmission submission = new QOTWSubmission(thread);
148-
submissionCache.put(thread.getIdLong(), submission);
149-
return submission;
150-
}
151-
}
152-
153134
private boolean canCreateSubmissions(Member member) {
154135
if (member == null) return false;
155136
if (member.getUser().isBot() || member.getUser().isSystem()) return false;
156-
return !member.isTimedOut() && !member.isPending();
137+
if (member.isTimedOut() || member.isPending()) return false;
138+
return config.getSubmissionChannel().getThreadChannels()
139+
.stream().noneMatch(p -> p.getName().contains(member.getId()));
157140
}
158141

159142
/**
@@ -170,7 +153,7 @@ public void acceptSubmission(InteractionHook hook, @NotNull ThreadChannel thread
170153
notificationService.withQOTW(thread.getGuild(), author).sendAccountIncrementedNotification();
171154
Responses.success(hook, "Submission Accepted",
172155
"Successfully accepted submission by " + author.getAsMention()).queue();
173-
notificationService.withQOTW(thread.getGuild()).sendSubmissionActionNotification(author, getOrRetrieveSubmission(thread), bestAnswer ? SubmissionStatus.ACCEPT_BEST : SubmissionStatus.ACCEPT);
156+
notificationService.withQOTW(thread.getGuild()).sendSubmissionActionNotification(author, new QOTWSubmission(thread), bestAnswer ? SubmissionStatus.ACCEPT_BEST : SubmissionStatus.ACCEPT);
174157
Optional<ThreadChannel> newestPostOptional = config.getSubmissionsForumChannel().getThreadChannels()
175158
.stream().max(Comparator.comparing(ThreadChannel::getTimeCreated));
176159
if (newestPostOptional.isPresent()) {
@@ -204,7 +187,7 @@ public void declineSubmission(InteractionHook hook, @NotNull ThreadChannel threa
204187
thread.getManager().setName(SUBMISSION_DECLINED + thread.getName().substring(1)).queue();
205188
notificationService.withQOTW(thread.getGuild(), author).sendSubmissionDeclinedEmbed();
206189
Responses.success(hook, "Submission Declined", "Successfully declined submission by " + author.getAsMention()).queue();
207-
notificationService.withQOTW(thread.getGuild()).sendSubmissionActionNotification(author, getOrRetrieveSubmission(thread), SubmissionStatus.DECLINE);
190+
notificationService.withQOTW(thread.getGuild()).sendSubmissionActionNotification(author, new QOTWSubmission(thread), SubmissionStatus.DECLINE);
208191
thread.getManager().setLocked(true).setArchived(true).queue();
209192
}
210193

0 commit comments

Comments
 (0)