Skip to content

Commit cf99f6e

Browse files
Further improved logging
1 parent 30b4489 commit cf99f6e

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

src/main/java/net/javadiscord/javabot/systems/notification/QOTWNotificationService.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@
44
import net.dv8tion.jda.api.EmbedBuilder;
55
import net.dv8tion.jda.api.entities.Guild;
66
import net.dv8tion.jda.api.entities.MessageEmbed;
7+
import net.dv8tion.jda.api.entities.ThreadChannel;
78
import net.dv8tion.jda.api.entities.User;
89
import net.javadiscord.javabot.Bot;
10+
import net.javadiscord.javabot.data.h2db.DbHelper;
911
import net.javadiscord.javabot.systems.qotw.dao.QuestionPointsRepository;
1012
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
13+
import net.javadiscord.javabot.systems.qotw.submissions.SubmissionStatus;
14+
import net.javadiscord.javabot.systems.qotw.submissions.dao.QOTWSubmissionRepository;
15+
import net.javadiscord.javabot.systems.qotw.submissions.model.QOTWSubmission;
1116

1217
import javax.annotation.Nonnull;
18+
import javax.annotation.Nullable;
1319
import java.sql.Connection;
1420
import java.sql.SQLException;
1521
import java.time.Instant;
22+
import java.util.Optional;
1623

1724
/**
1825
* Sends Notifications regarding QOTW.
1926
*/
2027
@Slf4j
2128
public non-sealed class QOTWNotificationService extends NotificationService {
2229

30+
@Nullable
2331
private final User user;
2432
private final Guild guild;
2533
private final QOTWAccount account;
@@ -44,18 +52,50 @@ public QOTWNotificationService(User user, Guild guild) {
4452
this.account = account;
4553
}
4654

55+
/**
56+
* The constructor of this class.
57+
*
58+
* @param guild The guild from where the notification was sent.
59+
*/
60+
public QOTWNotificationService(Guild guild) {
61+
this.user = null;
62+
this.guild = guild;
63+
this.account = null;
64+
}
65+
4766
public void sendBestAnswerNotification() {
67+
if (user == null || account == null) throw new UnsupportedOperationException("Can't send private messages with a guild-only constructor!");
4868
this.sendDirectMessageNotification(user, this.buildBestAnswerEmbed(account.getPoints()));
4969
}
5070

5171
public void sendAccountIncrementedNotification() {
72+
if (user == null || account == null) throw new UnsupportedOperationException("Can't send private messages with a guild-only constructor!");
5273
this.sendDirectMessageNotification(user, this.buildAccountIncrementEmbed(account.getPoints()));
5374
}
5475

5576
public void sendSubmissionDeclinedEmbed(@Nonnull String reason) {
77+
if (user == null || account == null) throw new UnsupportedOperationException("Can't send private messages with a guild-only constructor!");
5678
this.sendDirectMessageNotification(user, this.buildSubmissionDeclinedEmbed(reason));
5779
}
5880

81+
/**
82+
* Sends the executed action, performed on a QOTW submission thread, to the {@link Guild}s log channel.
83+
*
84+
* @param reviewedBy The user which reviewed the QOTW submission thread.
85+
* @param submissionThread The submission thread itself.
86+
* @param status The {@link SubmissionStatus}.
87+
* @param reasons The reasons for taking this action.
88+
*/
89+
public void sendSubmissionActionNotification(User reviewedBy, ThreadChannel submissionThread, SubmissionStatus status, @Nullable String... reasons) {
90+
DbHelper.doDaoAction(QOTWSubmissionRepository::new, dao -> {
91+
Optional<QOTWSubmission> submissionOptional = dao.getSubmissionByThreadId(submissionThread.getIdLong());
92+
submissionOptional.ifPresent(submission -> guild.getJDA().retrieveUserById(submission.getAuthorId()).queue(author -> {
93+
new GuildNotificationService(guild).sendLogChannelNotification(this.buildSubmissionActionEmbed(author, submissionThread, reviewedBy, status, reasons));
94+
log.info("{} {} {}'s QOTW Submission{}", reviewedBy.getAsTag(), status.name().toLowerCase(), author.getAsTag(), reasons != null ? " for: " + String.join(", ", reasons) : ".");
95+
}));
96+
});
97+
}
98+
5999
private EmbedBuilder buildQOTWNotificationEmbed() {
60100
return new EmbedBuilder()
61101
.setAuthor(user.getAsTag(), null, user.getEffectiveAvatarUrl())
@@ -97,4 +137,17 @@ private MessageEmbed buildSubmissionDeclinedEmbed(String reasons) {
97137
.build();
98138
}
99139

140+
private MessageEmbed buildSubmissionActionEmbed(User author, ThreadChannel thread, User reviewedBy, SubmissionStatus status, String... reasons) {
141+
EmbedBuilder builder = new EmbedBuilder()
142+
.setAuthor(reviewedBy.getAsTag(), null, reviewedBy.getEffectiveAvatarUrl())
143+
.setTitle(String.format("%s %s %s's QOTW Submission", reviewedBy.getAsTag(), status.name().toLowerCase(), author.getAsTag()))
144+
.setTimestamp(Instant.now());
145+
if (thread != null && status != SubmissionStatus.DELETED) {
146+
builder.addField("Thread", thread.getAsMention(), true);
147+
}
148+
if (reasons != null) {
149+
builder.addField("Reason(s)", String.join(", ", reasons), true);
150+
}
151+
return builder.build();
152+
}
100153
}

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

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
import net.javadiscord.javabot.systems.qotw.dao.QuestionPointsRepository;
1717
import net.javadiscord.javabot.systems.qotw.submissions.dao.QOTWSubmissionRepository;
1818
import net.javadiscord.javabot.systems.qotw.submissions.model.QOTWSubmission;
19-
import net.javadiscord.javabot.systems.notification.GuildNotificationService;
2019

21-
import javax.annotation.Nullable;
2220
import java.sql.Connection;
2321
import java.sql.SQLException;
2422
import java.time.Instant;
@@ -123,7 +121,7 @@ protected void acceptSubmission(ButtonInteractionEvent event, ThreadChannel thre
123121
}
124122
);
125123
this.disableControls(String.format("Accepted by %s", event.getUser().getAsTag()), event.getMessage());
126-
this.sendLogMessage(thread, event.getUser(), SubmissionStatus.ACCEPTED, null);
124+
new QOTWNotificationService(guild).sendSubmissionActionNotification(event.getUser(), thread, SubmissionStatus.ACCEPTED);
127125
}
128126

129127
/**
@@ -133,17 +131,16 @@ protected void acceptSubmission(ButtonInteractionEvent event, ThreadChannel thre
133131
* @param thread The submission's {@link ThreadChannel}.
134132
*/
135133
protected void declineSelectSubmission(SelectMenuInteractionEvent event, ThreadChannel thread) {
136-
String reasons = String.join(", ", event.getValues());
137134
DbHelper.doDaoAction(QOTWSubmissionRepository::new, dao -> dao.updateStatus(thread.getIdLong(), SubmissionStatus.DECLINED));
138135
thread.getManager().setName(SUBMISSION_DECLINED + thread.getName().substring(1)).queue();
139136
event.getJDA().retrieveUserById(submission.getAuthorId()).queue(user -> {
140-
new QOTWNotificationService(user, event.getGuild()).sendSubmissionDeclinedEmbed(reasons);
137+
new QOTWNotificationService(user, event.getGuild()).sendSubmissionDeclinedEmbed(String.join(", ", event.getValues()));
141138
Responses.success(event.getHook(), "Submission Declined",
142-
String.format("Successfully declined submission by %s for the following reasons:\n`%s`", user.getAsMention(), reasons)).queue();
139+
String.format("Successfully declined submission by %s for the following reasons:\n`%s`", user.getAsMention(), String.join(", ", event.getValues()))).queue();
143140
}
144141
);
145142
this.disableControls(String.format("Declined by %s", event.getUser().getAsTag()), event.getMessage());
146-
this.sendLogMessage(thread, event.getUser(), SubmissionStatus.DECLINED, reasons);
143+
new QOTWNotificationService(guild).sendSubmissionActionNotification(event.getUser(), thread, SubmissionStatus.DECLINED, event.getValues().toArray(new String[0]));
147144
}
148145

149146
/**
@@ -156,35 +153,11 @@ protected void deleteSubmission(ButtonInteractionEvent event, ThreadChannel thre
156153
DbHelper.doDaoAction(QOTWSubmissionRepository::new, dao -> dao.deleteSubmission(thread.getIdLong()));
157154
event.getHook().sendMessage("This Submission will be deleted in 10 seconds.").setEphemeral(true).queue();
158155
this.disableControls(String.format("Deleted by %s", event.getUser().getAsTag()), event.getMessage());
159-
this.sendLogMessage(thread, event.getUser(), SubmissionStatus.DELETED, null);
156+
new QOTWNotificationService(guild).sendSubmissionActionNotification(event.getUser(), thread, SubmissionStatus.DELETED);
160157
thread.delete().queueAfter(10, TimeUnit.SECONDS);
161158
}
162159

163-
private void sendLogMessage(ThreadChannel thread, User reviewedBy, SubmissionStatus status, @Nullable String reason) {
164-
DbHelper.doDaoAction(QOTWSubmissionRepository::new, dao -> {
165-
Optional<QOTWSubmission> submissionOptional = dao.getSubmissionByThreadId(thread.getIdLong());
166-
submissionOptional.ifPresent(submission -> guild.getJDA().retrieveUserById(submission.getAuthorId()).queue(author -> {
167-
new GuildNotificationService(guild).sendLogChannelNotification(this.buildLogEmbed(thread, author, reviewedBy, status, reason));
168-
log.info("{} {} {}'s QOTW Submission{}", reviewedBy.getAsTag(), status.name().toLowerCase(), author.getAsTag(), reason != null ? " for: " + reason : ".");
169-
}));
170-
});
171-
}
172-
173160
private void disableControls(String buttonLabel, Message message) {
174161
message.editMessageComponents(ActionRow.of(Button.secondary("qotw-submission:controls:dummy", buttonLabel).asDisabled())).queue();
175162
}
176-
177-
private MessageEmbed buildLogEmbed(ThreadChannel thread, User threadOwner, User reviewedBy, SubmissionStatus status, @Nullable String reason) {
178-
EmbedBuilder builder = new EmbedBuilder()
179-
.setAuthor(reviewedBy.getAsTag(), null, reviewedBy.getEffectiveAvatarUrl())
180-
.setTitle(String.format("%s %s %s's QOTW Submission", reviewedBy.getAsTag(), status.name().toLowerCase(), threadOwner.getAsTag()))
181-
.setTimestamp(Instant.now());
182-
if (thread != null && status != SubmissionStatus.DELETED) {
183-
builder.addField("Thread", thread.getAsMention(), true);
184-
}
185-
if (reason != null) {
186-
builder.addField("Reason(s)", reason, true);
187-
}
188-
return builder.build();
189-
}
190163
}

0 commit comments

Comments
 (0)